向 QTreeWidget 添加一行
Posted
技术标签:
【中文标题】向 QTreeWidget 添加一行【英文标题】:Adding a row to QTreeWidget 【发布时间】:2013-05-27 09:26:18 【问题描述】:如何在 PyQt 中的 QTreeWidget 中添加一行? 我遇到了一些严重的问题 - 我有一个 QStrings 列表,但我不知道如何将它添加到 QTreeWidget,它在另一个类(Ui_Form 类)中,而 QStringList 在另一个类中。
【问题讨论】:
如果只想显示字符串列表,为什么要使用 QTreeWidget 而不是 QListWidget? 因为我想将字符串分组到列中 【参考方案1】:首先,您必须列出QTreeWidgetItem
,然后将它们添加到 QTreeWidget。
示例:
tree = your_qtreewidget # replace every 'tree' with your QTreeWidget
strings = list_of_strings
l = [] # list of QTreeWidgetItem to add
for i in strings:
l.append(QTreeWidgetItem(i)) # create QTreeWidgetItem's and append them
tree.addTopLevelItems(l) # add everything to the tree
参考:PyQt 文档中的QTreeWidget 和QTreeWidgetItem。
【讨论】:
TypeError: 参数不匹配任何重载调用:QTreeWidgetItem(type: int = QTreeWidgetItem.Type): argument 1 has unexpected type 'str' QTreeWidgetItem(Iterable[str], type: int = QTreeWidgetItem.类型):参数 1 具有意外类型“str”【参考方案2】:这是我的代码并执行以下操作:
-
每次单击“编辑”按钮时都会创建一个新行
使用户可以选中每一行的第一列并使其显示为复选框
另外,我写了
self.ui.treeWidget.topLevelItem(rowcount).setTextAlignment(0, QtCore.Qt.AlignHCenter)
将每个文本项对齐到中心(水平)
from PyQt5.QtWidgets import QMainWindow, QApplication, QTreeWidgetItem
from PyQt5 import QtCore
import sys
import treewidgetwindow as treewidget
class widgetwindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = treewidget.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton_edit.clicked.connect(self.add_items)
def add_items(self):
rowcount = self.ui.treeWidget.topLevelItemCount()
self.ui.treeWidget.addTopLevelItem(QTreeWidgetItem(rowcount))
self.ui.treeWidget.topLevelItem(rowcount).setText(0, 'Lincoln a good boy')
self.ui.treeWidget.topLevelItem(rowcount).setCheckState(0, QtCore.Qt.Unchecked)
self.ui.treeWidget.topLevelItem(rowcount).setFlags(
self.ui.treeWidget.topLevelItem(rowcount).flags() | QtCore.Qt.ItemIsUserCheckable)
self.ui.treeWidget.topLevelItem(rowcount).setText(1, 'He added a second column')
self.ui.treeWidget.topLevelItem(rowcount).setTextAlignment(0, QtCore.Qt.AlignHCenter)
self.ui.treeWidget.topLevelItem(rowcount).setTextAlignment(1, QtCore.Qt.AlignHCenter)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = widgetwindow()
w.show()
sys.exit(app.exec_())
窗口的图像将如下所示: TreeWidget Window Preview
另外,如果你必须运行完整的代码,那么下面是 UI 文件的代码:(你必须将两个文件放在同一个目录中)并将下面的代码命名为 @987654324 @
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'treewidgetwindow.ui'
#
# Created by: PyQt5 UI code generator 5.14.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
MainWindow.setStyleSheet("#centralwidget\n"
"border: 2px solid grey;\n"
"border-radius: 5px;\n"
"padding: 5px 15px;\n"
"background-color: rgb(232, 216, 86);\n"
"\n"
"QPushButton\n"
"border: 2px solid;\n"
"border-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0.985075, y2:0.017, stop:0 rgba(255, 255, 255, 255), stop:0.552239 rgba(237, 86, 0, 255));\n"
"padding: 5px 15px;\n"
"\n"
"QPushButton:hover:!pressed\n"
"color: rgb(238, 238, 236);\n"
"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
self.treeWidget.setStyleSheet("QHeaderView::section \n"
" color: black;\n"
" padding: 2px;\n"
" height: 20px;\n"
" border: 5px solid black;\n"
" border-left: 2px solid black;\n"
" border-right:2px solid black;\n"
"")
self.treeWidget.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.headerItem().setTextAlignment(0, QtCore.Qt.AlignCenter)
self.treeWidget.headerItem().setTextAlignment(1, QtCore.Qt.AlignCenter)
self.treeWidget.headerItem().setTextAlignment(2, QtCore.Qt.AlignCenter)
item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget)
self.verticalLayout.addWidget(self.treeWidget)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.pushButton_close = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_close.setObjectName("pushButton_close")
self.gridLayout.addWidget(self.pushButton_close, 1, 1, 1, 1)
self.pushButton_edit = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_edit.setObjectName("pushButton_edit")
self.gridLayout.addWidget(self.pushButton_edit, 0, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 0, 0, 1, 1)
self.verticalLayout.addLayout(self.gridLayout)
self.verticalLayout_2.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Treewidget Experiment"))
self.treeWidget.headerItem().setText(0, _translate("MainWindow", "Column 1"))
self.treeWidget.headerItem().setText(1, _translate("MainWindow", "Column 2"))
self.treeWidget.headerItem().setText(2, _translate("MainWindow", "Column 3"))
__sortingEnabled = self.treeWidget.isSortingEnabled()
self.treeWidget.setSortingEnabled(False)
self.treeWidget.topLevelItem(0).setText(0, _translate("MainWindow", "data_01"))
self.treeWidget.topLevelItem(0).setText(1, _translate("MainWindow", "data_02"))
self.treeWidget.topLevelItem(0).setText(2, _translate("MainWindow", "data_03"))
self.treeWidget.setSortingEnabled(__sortingEnabled)
self.pushButton_close.setText(_translate("MainWindow", "CLOSE"))
self.pushButton_edit.setText(_translate("MainWindow", "EDIT"))
【讨论】:
以上是关于向 QTreeWidget 添加一行的主要内容,如果未能解决你的问题,请参考以下文章
如何将 QTableWidget 添加到 QTreeWidget 但作为 QTreeWidgetItem