开发pyqt4树小部件

编程入门 行业动态 更新时间:2024-10-23 23:30:04
本文介绍了开发pyqt4树小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在pyqt中写一棵树吗?它看起来像这样:

i need to write a tree?, in pyqt. It looks like this:

Clients(this is text) Type A (this is a Clients child and has a checkbox) Type B (this is a Clients child and has a checkbox) Vendors(this is text) Mary (this is a Vendors child and has a checkbox) Arnold (this is a Vendors child and has a checkbox) Time Period Init(this is a Time Period child, and would be a calendarWidget for date selection) End (this is a Time Period child, and would be a calendarWidget for date selection)

您对此有什么建议?QTreeWidget?QTreeView?这将是我将用于构建 sql 查询的可点击项目.感谢阅读.

What would you recommend for this? QTreeWidget? QTreeView? This will be clickable items that i'll use to build sql queries. Thanks for reading.

推荐答案

我推荐你使用 QTreeWidget 而不是 QTreeView,因为您的任务非常简单.QTreeView(使用自定义模型,例如 QStandardItemModel) 用于困难事件.你的很简单.

I recommend you to use QTreeWidget instead of QTreeView, because your tasks are pretty simple. QTreeView (with custom model, for example QStandardItemModel) is for difficult events. Yours is simple.

import sys from PyQt4 import QtCore, QtGui class Window(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.treeWidget = QtGui.QTreeWidget() self.treeWidget.setHeaderHidden(True) self.addItems(self.treeWidget.invisibleRootItem()) self.treeWidget.itemChanged.connect (self.handleChanged) layout = QtGui.QVBoxLayout() layout.addWidget(self.treeWidget) self.setLayout(layout) def addItems(self, parent): column = 0 clients_item = self.addParent(parent, column, 'Clients', 'data Clients') vendors_item = self.addParent(parent, column, 'Vendors', 'data Vendors') time_period_item = self.addParent(parent, column, 'Time Period', 'data Time Period') self.addChild(clients_item, column, 'Type A', 'data Type A') self.addChild(clients_item, column, 'Type B', 'data Type B') self.addChild(vendors_item, column, 'Mary', 'data Mary') self.addChild(vendors_item, column, 'Arnold', 'data Arnold') self.addChild(time_period_item, column, 'Init', 'data Init') self.addChild(time_period_item, column, 'End', 'data End') def addParent(self, parent, column, title, data): item = QtGui.QTreeWidgetItem(parent, [title]) item.setData(column, QtCore.Qt.UserRole, data) item.setChildIndicatorPolicy(QtGui.QTreeWidgetItem.ShowIndicator) item.setExpanded (True) return item def addChild(self, parent, column, title, data): item = QtGui.QTreeWidgetItem(parent, [title]) item.setData(column, QtCore.Qt.UserRole, data) item.setCheckState (column, QtCore.Qt.Unchecked) return item def handleChanged(self, item, column): if item.checkState(column) == QtCore.Qt.Checked: print "checked", item, item.text(column) if item.checkState(column) == QtCore.Qt.Unchecked: print "unchecked", item, item.text(column) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())

更多推荐

开发pyqt4树小部件

本文发布于:2023-11-07 00:54:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!