# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Tree2.ui'
#
# Created: Thu Jun 18 17:54:24 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt4 import QtCore, QtGui
try:
_fromUtf8
= QtCore.
QString.
fromUtf8except AttributeError:
def _fromUtf8(s):
return s
try:
def _translate(context, text, disambig):
return QtGui.
QApplication.
translate(context, text, disambig, _encoding
) except AttributeError:
def _translate(context, text, disambig):
return QtGui.
QApplication.
translate(context, text, disambig
)
data1 = [("Name", [("a",[]), ("b",[]), ("c", [("e",[]), ("f", []) ])])]
def __init__(self):
self.setupUi(self)
self.indyTracker = 1
self. stringBuilder = None
self.boole = False
self.treeBuilder = {}
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(437, 529)
self.
centralwidget = QtGui.
QWidget(MainWindow
) self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.
verticalLayout_2 = QtGui.
QVBoxLayout(self.
centralwidget) self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.
treeView = QtGui.
QTreeView(self.
centralwidget) self.treeView.setObjectName(_fromUtf8("treeView"))
self.addItems(self.model, data1)
self.treeView.setModel(self.model)
self.model.setHorizontalHeaderLabels([self.tr("ROOT")])
self.verticalLayout.addWidget(self.treeView)
self.
lineEdit_2 = QtGui.
QLineEdit(self.
centralwidget) self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.verticalLayout.addWidget(self.lineEdit_2)
self.
lineEdit = QtGui.
QLineEdit(self.
centralwidget) self.lineEdit.setText(_fromUtf8(""))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.
label = QtGui.
QLabel(self.
centralwidget) sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setAutoFillBackground(True)
self.
label.
setFrameShape(QtGui.
QFrame.
Box) self.
label.
setFrameShadow(QtGui.
QFrame.
Raised) self.label.setLineWidth(3)
self.label.setMidLineWidth(1)
self.label.setText(_fromUtf8(""))
self.label.setWordWrap(True)
self.label.setOpenExternalLinks(True)
self.label.setObjectName(_fromUtf8("label"))
self.verticalLayout.addWidget(self.label)
self.verticalLayout_2.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.
menubar = QtGui.
QMenuBar(MainWindow
) self.
menubar.
setGeometry(QtCore.
QRect(0,
0,
437,
21)) self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.lineEdit_2.setPlaceholderText(_translate("MainWindow", "Parent/Child - Press return to Add", None))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "Root Index - Press Return to Add", None))
self.lineEdit.returnPressed.connect(self.addAROOT)
self.treeView.clicked.connect(self.addParents)
#self.treeView.clicked.connect(self.modelIndexDefiner)
self.treeView.clicked.connect(self.createIndex)
def addAROOT(self):
parent = self.lineEdit.text()
def addParents(self, index):
selectedItem = index.model().itemFromIndex(index)
if self.lineEdit_2.text() == "":
errorHandle()
else:
selectedItem.
appendRow(QtGui.
QStandardItem(self.
lineEdit_2.
text())) self.lineEdit.clear()
self.lineEdit_2.clear()
def modelIndexDefiner(self, index):
childFlag = index.model().hasChildren(index)
currentIndex = index.parent()
currentItem = index.data()
originalItem = currentItem
def createIndex(self, index):
selectedItem1 = index.model().itemFromIndex(index)
selectedItem2 = index.parent()
getRow = selectedItem1.rowCount()
getColumn = selectedItem1.columnCount()
if not selectedItem2.isValid():
self.label.setText(("\t" + selectedItem1.text() + "\t" + str(getRow) + "\t" + str(getColumn))
+ "\n" + "\tROOT")
else:
self.label.setText(("\t" + selectedItem1.text() + "\t" + str(getRow) + "\t" + str(getColumn))
+ "\n\tPARENT")
def createRoot(self, index):
self.selectedItem = index.model().itemFromIndex(index)
self.parentCheck = index.parent()
if not self.parentCheck.isValid():
print("ROOT")
def addItems(self, parent, elements):
for text, children in elements:
parent.appendRow(item)
if children:
self.addItems(item, children)
class errorHandle():
def __init__(self):
pass
if __name__ == '__main__':
mainWin = Ui_MainWindow()
mainWin.show()
sys.exit(app.exec())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Tree2.ui'
#
# Created: Thu Jun 18 17:54:24 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
data1 = [("Name", [("a",[]), ("b",[]), ("c", [("e",[]), ("f", []) ])])]
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.indyTracker = 1
self. stringBuilder = None
self.boole = False
self.treeBuilder = {}
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(437, 529)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.treeView = QtGui.QTreeView(self.centralwidget)
self.treeView.setObjectName(_fromUtf8("treeView"))
self.model = QtGui.QStandardItemModel()
self.addItems(self.model, data1)
self.treeView.setModel(self.model)
self.model.setHorizontalHeaderLabels([self.tr("ROOT")])
self.verticalLayout.addWidget(self.treeView)
self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.verticalLayout.addWidget(self.lineEdit_2)
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setText(_fromUtf8(""))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout.addWidget(self.lineEdit)
self.label = QtGui.QLabel(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setAutoFillBackground(True)
self.label.setFrameShape(QtGui.QFrame.Box)
self.label.setFrameShadow(QtGui.QFrame.Raised)
self.label.setLineWidth(3)
self.label.setMidLineWidth(1)
self.label.setText(_fromUtf8(""))
self.label.setWordWrap(True)
self.label.setOpenExternalLinks(True)
self.label.setObjectName(_fromUtf8("label"))
self.verticalLayout.addWidget(self.label)
self.verticalLayout_2.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 437, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.lineEdit_2.setPlaceholderText(_translate("MainWindow", "Parent/Child - Press return to Add", None))
self.lineEdit.setPlaceholderText(_translate("MainWindow", "Root Index - Press Return to Add", None))
self.lineEdit.returnPressed.connect(self.addAROOT)
self.treeView.clicked.connect(self.addParents)
#self.treeView.clicked.connect(self.modelIndexDefiner)
self.treeView.clicked.connect(self.createIndex)
def addAROOT(self):
parent = self.lineEdit.text()
self.model.appendRow(QtGui.QStandardItem(parent))
@QtCore.pyqtSlot(QtCore.QAbstractItemModel)
def addParents(self, index):
selectedItem = index.model().itemFromIndex(index)
if self.lineEdit_2.text() == "":
errorHandle()
else:
selectedItem.appendRow(QtGui.QStandardItem(self.lineEdit_2.text()))
self.lineEdit.clear()
self.lineEdit_2.clear()
@QtCore.pyqtSlot(QtCore.QAbstractItemModel)
def modelIndexDefiner(self, index):
childFlag = index.model().hasChildren(index)
currentIndex = index.parent()
currentItem = index.data()
originalItem = currentItem
@QtCore.pyqtSlot(QtCore.QAbstractItemModel)
def createIndex(self, index):
selectedItem1 = index.model().itemFromIndex(index)
selectedItem2 = index.parent()
getRow = selectedItem1.rowCount()
getColumn = selectedItem1.columnCount()
if not selectedItem2.isValid():
self.label.setText(("\t" + selectedItem1.text() + "\t" + str(getRow) + "\t" + str(getColumn))
+ "\n" + "\tROOT")
else:
self.label.setText(("\t" + selectedItem1.text() + "\t" + str(getRow) + "\t" + str(getColumn))
+ "\n\tPARENT")
@QtCore.pyqtSlot(QtCore.QAbstractItemModel)
def createRoot(self, index):
self.selectedItem = index.model().itemFromIndex(index)
self.parentCheck = index.parent()
if not self.parentCheck.isValid():
print("ROOT")
def addItems(self, parent, elements):
for text, children in elements:
item = QtGui.QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
class errorHandle():
def __init__(self):
pass
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWin = Ui_MainWindow()
mainWin.show()
sys.exit(app.exec())
To copy to clipboard, switch view to plain text mode
Bookmarks