Created
February 15, 2016 01:38
-
-
Save oglops/c12208af940ed5668080 to your computer and use it in GitHub Desktop.
QTreeView + QStandardItemModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
data = {'level1': ['1', '2', '3'], | |
'level2': ['4', '5', '6'], | |
'level3': ['7', '8', '9']} | |
class Window(QMainWindow): | |
'This time I try to put the table populating code in main ui' | |
def __init__(self, parent=None): | |
super(Window, self).__init__(parent) | |
self.model = QStandardItemModel() | |
# self.model.setRootPath(QDir.currentPath()) | |
tree = QTreeView() | |
tree.setModel(self.model) | |
for n, key in enumerate(sorted(data.keys())): | |
parent = QStandardItem(key) | |
for m, item in enumerate(data[key]): | |
child = QStandardItem(item) | |
parent.appendRow([child]) | |
self.model.appendRow(parent) | |
# expand third container | |
index = self.model.indexFromItem(parent) | |
tree.expand(index) | |
# select last row | |
selmod = tree.selectionModel() | |
index2 = self.model.indexFromItem(child) | |
selmod.select( | |
index2, QItemSelectionModel.Select | QItemSelectionModel.Rows) | |
self.model.setHeaderData(0, Qt.Horizontal, "Tree View") | |
self.setCentralWidget(tree) | |
self.resize(350, 220) | |
def main(args): | |
app = QApplication(args) | |
window = Window() | |
window.show() | |
sys.exit(app.exec_()) | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment