QTreeWidget PyQt 中出现额外的行
Extra row appearing in QTreeWidget PyQt
我有一段代码,其中我向 QTreeWidget
父项添加了两个子项。子项设置为“可编辑”。
我在这里面临两个问题:
- 出现了一个额外的行,其中包含 空 个可编辑项目。 (我希望“最小”和“最大”在同一行)
- 如果我将可编辑项目更改为 空字符串 那么它看起来好像什么都没有,我想要某种荧光笔,有一个空字段在这里(某种盒子)
如何解决这些问题?
代码:
import sys
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.FilterList = QTreeWidget()
self.setCentralWidget(self.FilterList)
self.setWindowTitle("Form")
self.setGeometry(50,50,800,500)
self.generateData()
def generateData(self):
self.FilterList.setColumnCount(3)
self.FilterList.setHeaderLabels(["Filter Questions"])
DifficultyNode = QTreeWidgetItem(["Difficulty"])
self.FilterList.addTopLevelItem(DifficultyNode)
self.FilterList.itemChanged.connect(self.handleItemChanged)
EasyNode = QTreeWidgetItem(["Easy"])
EasyNode.setCheckState(0, Qt.Unchecked)
NormalNode = QTreeWidgetItem(["Normal"])
NormalNode.setCheckState(0, Qt.Unchecked)
HardNode = QTreeWidgetItem(["Hard"])
HardNode.setCheckState(0, Qt.Unchecked)
HardNode.setFlags(HardNode.flags() | QtCore.Qt.ItemIsEditable)
MinNode = QTreeWidgetItem()
MinNode.setText(1, "Min")
MinNode.setFlags(MinNode.flags() | QtCore.Qt.ItemIsEditable)
MaxNode = QTreeWidgetItem()
MaxNode.setText(2, "Max")
MaxNode.setFlags(MaxNode.flags() | QtCore.Qt.ItemIsEditable)
DifficultyNode.addChild(EasyNode)
EasyNode.addChild(MinNode)
EasyNode.addChild(MaxNode)
DifficultyNode.addChild(NormalNode)
DifficultyNode.addChild(HardNode)
def handleItemChanged(self, item, column):
if item.checkState(column) == QtCore.Qt.Checked:
print('Item Checked', item.text(column))
elif item.checkState(column) == QtCore.Qt.Unchecked:
print('Item Unchecked', item.text(column))
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
main()
要在同一行获得“Min”和“Max”,只使用一个 QTreeWidgetItem 并为两列设置文本。
如果你想要一个永久框,一个选项是将 QTreeWidgetItem 的 itemWidget
设置为 QLineEdit 并连接它的 editingFinished
以设置项目文本。
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.FilterList = QTreeWidget()
self.setCentralWidget(self.FilterList)
self.setWindowTitle("Form")
self.setGeometry(50,50,800,500)
self.generateData()
def generateData(self):
self.FilterList.setColumnCount(3)
self.FilterList.setHeaderLabels(["Filter Questions"])
DifficultyNode = QTreeWidgetItem(["Difficulty"])
self.FilterList.addTopLevelItem(DifficultyNode)
self.FilterList.itemChanged.connect(self.handleItemChanged)
EasyNode = QTreeWidgetItem(["Easy"])
EasyNode.setCheckState(0, Qt.Unchecked)
NormalNode = QTreeWidgetItem(["Normal"])
NormalNode.setCheckState(0, Qt.Unchecked)
HardNode = QTreeWidgetItem(["Hard"])
HardNode.setCheckState(0, Qt.Unchecked)
HardNode.setFlags(HardNode.flags() | QtCore.Qt.ItemIsEditable)
MinNode = QTreeWidgetItem()
DifficultyNode.addChild(EasyNode)
EasyNode.addChild(MinNode)
DifficultyNode.addChild(NormalNode)
DifficultyNode.addChild(HardNode)
self.setTextBox(MinNode, 1, "Min")
self.setTextBox(MinNode, 2, "Max")
def setTextBox(self, item, column, text):
box = QLineEdit(text)
box.editingFinished.connect(lambda: item.setText(column, box.text()))
self.FilterList.setItemWidget(item, column, box)
def handleItemChanged(self, item, column):
if item.checkState(column) == QtCore.Qt.Checked:
print('Item Checked', item.text(column))
elif item.checkState(column) == QtCore.Qt.Unchecked:
print('Item Unchecked', item.text(column))
我有一段代码,其中我向 QTreeWidget
父项添加了两个子项。子项设置为“可编辑”。
我在这里面临两个问题:
- 出现了一个额外的行,其中包含 空 个可编辑项目。 (我希望“最小”和“最大”在同一行)
- 如果我将可编辑项目更改为 空字符串 那么它看起来好像什么都没有,我想要某种荧光笔,有一个空字段在这里(某种盒子)
如何解决这些问题?
代码:
import sys
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.FilterList = QTreeWidget()
self.setCentralWidget(self.FilterList)
self.setWindowTitle("Form")
self.setGeometry(50,50,800,500)
self.generateData()
def generateData(self):
self.FilterList.setColumnCount(3)
self.FilterList.setHeaderLabels(["Filter Questions"])
DifficultyNode = QTreeWidgetItem(["Difficulty"])
self.FilterList.addTopLevelItem(DifficultyNode)
self.FilterList.itemChanged.connect(self.handleItemChanged)
EasyNode = QTreeWidgetItem(["Easy"])
EasyNode.setCheckState(0, Qt.Unchecked)
NormalNode = QTreeWidgetItem(["Normal"])
NormalNode.setCheckState(0, Qt.Unchecked)
HardNode = QTreeWidgetItem(["Hard"])
HardNode.setCheckState(0, Qt.Unchecked)
HardNode.setFlags(HardNode.flags() | QtCore.Qt.ItemIsEditable)
MinNode = QTreeWidgetItem()
MinNode.setText(1, "Min")
MinNode.setFlags(MinNode.flags() | QtCore.Qt.ItemIsEditable)
MaxNode = QTreeWidgetItem()
MaxNode.setText(2, "Max")
MaxNode.setFlags(MaxNode.flags() | QtCore.Qt.ItemIsEditable)
DifficultyNode.addChild(EasyNode)
EasyNode.addChild(MinNode)
EasyNode.addChild(MaxNode)
DifficultyNode.addChild(NormalNode)
DifficultyNode.addChild(HardNode)
def handleItemChanged(self, item, column):
if item.checkState(column) == QtCore.Qt.Checked:
print('Item Checked', item.text(column))
elif item.checkState(column) == QtCore.Qt.Unchecked:
print('Item Unchecked', item.text(column))
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
main()
要在同一行获得“Min”和“Max”,只使用一个 QTreeWidgetItem 并为两列设置文本。
如果你想要一个永久框,一个选项是将 QTreeWidgetItem 的 itemWidget
设置为 QLineEdit 并连接它的 editingFinished
以设置项目文本。
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.FilterList = QTreeWidget()
self.setCentralWidget(self.FilterList)
self.setWindowTitle("Form")
self.setGeometry(50,50,800,500)
self.generateData()
def generateData(self):
self.FilterList.setColumnCount(3)
self.FilterList.setHeaderLabels(["Filter Questions"])
DifficultyNode = QTreeWidgetItem(["Difficulty"])
self.FilterList.addTopLevelItem(DifficultyNode)
self.FilterList.itemChanged.connect(self.handleItemChanged)
EasyNode = QTreeWidgetItem(["Easy"])
EasyNode.setCheckState(0, Qt.Unchecked)
NormalNode = QTreeWidgetItem(["Normal"])
NormalNode.setCheckState(0, Qt.Unchecked)
HardNode = QTreeWidgetItem(["Hard"])
HardNode.setCheckState(0, Qt.Unchecked)
HardNode.setFlags(HardNode.flags() | QtCore.Qt.ItemIsEditable)
MinNode = QTreeWidgetItem()
DifficultyNode.addChild(EasyNode)
EasyNode.addChild(MinNode)
DifficultyNode.addChild(NormalNode)
DifficultyNode.addChild(HardNode)
self.setTextBox(MinNode, 1, "Min")
self.setTextBox(MinNode, 2, "Max")
def setTextBox(self, item, column, text):
box = QLineEdit(text)
box.editingFinished.connect(lambda: item.setText(column, box.text()))
self.FilterList.setItemWidget(item, column, box)
def handleItemChanged(self, item, column):
if item.checkState(column) == QtCore.Qt.Checked:
print('Item Checked', item.text(column))
elif item.checkState(column) == QtCore.Qt.Unchecked:
print('Item Unchecked', item.text(column))