Python Qt学习(八)Treeview

这篇具有很好参考价值的文章主要介绍了Python Qt学习(八)Treeview。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

源代码:

# -*- 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())        

截图:

Python Qt学习(八)Treeview,python,qt文章来源地址https://www.toymoban.com/news/detail-697623.html

到了这里,关于Python Qt学习(八)Treeview的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python Qt学习(四)Radio Button

    代码 运行截图:

    2024年02月10日
    浏览(31)
  • Python Qt学习(十)一个简易的POP3邮件客户端

    公司把126这类的邮箱网站都封了,正好现在无事,加之,算是一个对这俩周学习Qt的一个总结吧。遂写了这么一个简易的通过POP3协议接收126邮件的客户端。 源代码: 截图:

    2024年02月09日
    浏览(30)
  • 【深度学习】基于Qt的人脸识别系统,门禁人脸识别系统,Python人脸识别流程,树莓派

    在深度学习领域做人脸识别的识别准确率已经高到超出人类识别,但综合考虑模型复杂度(推理速度)和模型的识别效果,这个地方还是有做一些工作的需求的。 人脸识别的过程基本由下面的流程组成。 yolov5-face、yolov7-face等github项目都可以做到这一点,在公开数据集上训练

    2024年02月09日
    浏览(32)
  • Python+Qt掌纹识别

    程序示例精选 Python+Qt掌纹识别 如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助! 这篇博客针对Python+Qt掌纹识别编写代码,代码整洁,规则,易读。 学习与应用推荐首选。 一、所需工具软件 二、使用步骤         1. 引入库         

    2024年02月01日
    浏览(28)
  • QT界面建立(Python)

    工具:QTdesigner+pycharm+py3.6+pyui(转换工具) QTpython语言文件的生成 QTdesigner作为可视化编程软件,上手很容易。首先通过QTdesigner编写界面,这里的编写可以不需要加入槽函数,等待可视化编写完成后,再通过对源文件添加语句来实现函数的调用。 下图就是编程界面,有许多控

    2024年02月16日
    浏览(31)
  • Qt for Python

    目前python的qt 绑定有两种方式 pyqt 和 pyside。目前PyQt是由一家小公司Riverbank Computing维护的,PyQt历史更长一些,比较稳定,开发社区也比较大,有相关的deploy工具;而PySide(又名Qt for Python)现由Qt公司维护,比PyQt更年轻一些。截至2019年,最新版本是PyQt5和PySide2。PySide6是2020年

    2024年02月16日
    浏览(24)
  • qt&python混合编程

    参考:Qt调用Python详细过程_贝勒里恩的博客-CSDN博客_qt python 1.环境搭建 1.1.python3.6 32bits 1.2.qt5.12.12 32bits 说明: (1). 务必保持版本位数一致,没有一致就得重新下载一致的版本 (2).以上软件环境的下载和安装,请自行百度,资料很多,在此重点说说混合编程遇到的错误如何解决. (3).请装

    2024年02月06日
    浏览(45)
  • Qt调用Python详细过程

      本文福利, 莬 费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击 莬 费领取↓↓ 编写Python代码,

    2023年04月11日
    浏览(35)
  • Python Qt(七)Listview

    源代码: 截图:

    2024年02月11日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包