Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a simple tree widget used for asking questions
#!/usr/bin/env python2
import os
import sys
import re
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QString
class TheUI(QtGui.QDialog):
def __init__(self, args=None, parent=None):
super(TheUI, self).__init__(parent)
self.layout1 = QtGui.QVBoxLayout(self)
treeWidget = QtGui.QTreeWidget()
# treeWidget.setSelectionMode( QtGui.QAbstractItemView.ExtendedSelection )
button1 = QtGui.QPushButton('Add')
button2 = QtGui.QPushButton('Add Child')
self.layout1.addWidget(treeWidget)
self.layout2 = QtGui.QHBoxLayout()
self.layout2.addWidget(button1)
self.layout2.addWidget(button2)
self.layout1.addLayout(self.layout2)
treeWidget.setHeaderHidden(True)
self.treeWidget = treeWidget
self.button1 = button1
self.button2 = button2
self.button1.clicked.connect(lambda *x: self.addCmd())
self.button2.clicked.connect(lambda *x: self.addChildCmd())
HEADERS = ("script", "chunksize", "mem")
self.treeWidget.setHeaderLabels(HEADERS)
self.treeWidget.setColumnCount(len(HEADERS))
self.treeWidget.setColumnWidth(0, 160)
self.treeWidget.header().show()
self.treeWidget.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.resize(500, 500)
for i in xrange(6):
item = self.addCmd(i)
if i in (3, 4):
self.addChildCmd()
if i == 4:
self.addCmd('%s-2' % i, parent=item)
self.treeWidget.expandAll()
self.setStyleSheet("QTreeWidget::item{ height: 30px; }")
def addChildCmd(self):
parent = self.treeWidget.currentItem()
self.addCmd(parent=parent)
self.treeWidget.setCurrentItem(parent)
def addCmd(self, i=None, parent=None):
'add a level to tree widget'
root = self.treeWidget.invisibleRootItem()
if not parent:
parent = root
if i is None:
if parent == root:
i = self.treeWidget.topLevelItemCount()
else:
i = str(parent.text(0))[7:]
i = '%s-%s' % (i, parent.childCount() + 1)
item = QtGui.QTreeWidgetItem(parent, ['script %s' % i, '1', '150'])
self.treeWidget.setCurrentItem(item)
self.treeWidget.expandAll()
return item
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
gui = TheUI()
gui.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment