源代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'qt_treeview.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import sys
from collections import deque
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.treeView = QtWidgets.QTreeView(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(10, 10, 391, 511))
self.treeView.setObjectName("treeView")
self.treeView.setAlternatingRowColors(True)
self.treeModel = QtGui.QStandardItemModel()
self.treeModel.setHorizontalHeaderLabels(['Item', 'Level', 'Sequence'])
self.treeView.setModel(self.treeModel)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(470, 70, 211, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.initTree)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(470, 120, 211, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.getCurrentNode)
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(470, 170, 211, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.insertNewRow)
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(470, 220, 211, 28))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.clicked.connect(self.appendNewRow)
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(470, 270, 211, 28))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.deleteRow)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Init Tree"))
self.pushButton_2.setText(_translate("MainWindow", "Get Current Node"))
self.pushButton_3.setText(_translate("MainWindow", "Insert Node Before Current Node"))
self.pushButton_4.setText(_translate("MainWindow", "Insert Node After Current Node"))
self.pushButton_5.setText(_translate("MainWindow", "Delete Current Node"))
def show(self, Form):
Form.show()
def initTree(self):
root=self.treeModel.invisibleRootItem()
self.treeModel.insertRow(0)
self.treeModel.setData(self.treeModel.index(0,0), "Line 1")
self.treeModel.setData(self.treeModel.index(0,1), "1")
self.treeModel.setData(self.treeModel.index(0,2), "1")
self.treeModel.insertRow(1)
self.treeModel.setData(self.treeModel.index(1,0), "Line 2")
self.treeModel.setData(self.treeModel.index(1,1), "1")
self.treeModel.setData(self.treeModel.index(1,2), "2")
self.treeModel.insertRow(2)
self.treeModel.setData(self.treeModel.index(2,0), "Line 3")
self.treeModel.setData(self.treeModel.index(2,1), "1")
self.treeModel.setData(self.treeModel.index(2,2), "3")
parent=self.treeModel.item(0)
parent.appendRow([
QStandardItem('Line 1-1'),
QStandardItem('2'),
QStandardItem('1'),
])
parent.appendRow([
QStandardItem('Line 1-2'),
QStandardItem('2'),
QStandardItem('2'),
])
parent=self.treeModel.item(1)
parent.appendRow([
QStandardItem('Line 2-1'),
QStandardItem('2'),
QStandardItem('3'),
])
def getCurrentNode(self):
select_item=self.treeView.selectedIndexes()
v1=select_item[0].data()
v2=select_item[1].data()
v3=select_item[2].data()
msg="Curent Node:\r\n"+"Column 1: " + v1 + "\r\n" + "Column 2: " + v2 + "\r\n" + "Column 3: "+v3
self.messageBox(msg)
def messageBox(self,msg):
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Information)
msgBox.setWindowTitle("Qt Message Box")
msgBox.setText(msg)
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
returnValue = msgBox.exec()
def insertNewRow(self):
select_item=self.treeView.currentIndex()
if select_item.parent().row() == -1:
index=select_item.row()
self.treeModel.insertRow(index)
self.treeModel.setData(self.treeModel.index(index,0),'New Item')
else:
index=select_item.parent().row()
parent=self.treeModel.item(index)
parent.insertRow(select_item.row(), [QStandardItem('New Subitem')])
def appendNewRow(self):
select_item=self.treeView.currentIndex()
if select_item.parent().row() == -1:
index=select_item.row()+1
self.treeModel.insertRow(index)
self.treeModel.setData(self.treeModel.index(index,0),'New Item')
else:
index=select_item.parent().row()
parent=self.treeModel.item(index)
parent.insertRow(select_item.row()+1, [QStandardItem('New Subitem')])
def deleteRow(self):
select_item=self.treeView.currentIndex()
if select_item.parent().row() == -1:
index=select_item.row()
self.treeModel.removeRow(index)
else:
index=select_item.parent().row()
parent=self.treeModel.item(index)
parent.removeRow(select_item.row())
if __name__ == "__main__":
app = QApplication(sys.argv)
Form=QtWidgets.QMainWindow()
main_win = Ui_MainWindow()
main_win.setupUi(Form)
main_win.show(Form)
sys.exit(app.exec())
截图:文章来源:https://www.toymoban.com/news/detail-697623.html
文章来源地址https://www.toymoban.com/news/detail-697623.html
到了这里,关于Python Qt学习(八)Treeview的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!