[英]PyQt5 QTreeWidget in layout showing an extra column outside the widget
I am trying to write a PyQt5 widget which will be a basic preset manager inside an application.我正在尝试编写一个 PyQt5 小部件,它将成为应用程序内的基本预设管理器。 I am trying to use the QTreeWidget to display the structure of each type.我正在尝试使用 QTreeWidget 来显示每种类型的结构。 I have this weird issue, where I see an extra QTreeWidget with a column named "1" outside my QTreeWidget.我有这个奇怪的问题,我看到一个额外的 QTreeWidget,在我的 QTreeWidget 之外有一列名为“1”。 I have attached a picture (sorry, I could not take a screenshots) and the code.我附上了一张图片(对不起,我无法截图)和代码。 I have also noticed that this does not happen if I don't use classes.我还注意到,如果我不使用类,这不会发生。
Here's my test code showing the problem:这是我显示问题的测试代码:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MyTree(QtWidgets.QTreeWidget):
def __init__(self, parent = None):
super(self.__class__, self).__init__(parent)
boxLayout = QtWidgets.QVBoxLayout()
treeWidget = QtWidgets.QTreeWidget()
treeWidget.setHeaderLabels(['Preset Name'])
treeWidget.setColumnCount(1)
items = []
for i in range(10):
items.append(QtWidgets.QTreeWidgetItem(["item {0}".format(i)]))
treeWidget.insertTopLevelItems(0, items)
boxLayout.addWidget(treeWidget)
self.setLayout(boxLayout)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MyTree()
w.show()
sys.exit(app.exec_())
That's what I see:这就是我所看到的:
My best guess that it's the layout that's causing it, because if I just create the QTreeWidget like so:我最好的猜测是导致它的布局,因为如果我像这样创建 QTreeWidget:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication(sys.argv)
treeWidget = QtWidgets.QTreeWidget()
treeWidget.setHeaderLabels(['Preset Name'])
treeWidget.setColumnCount(1)
items = []
for i in range(10):
items.append(QtWidgets.QTreeWidgetItem(["item {0}".format(i)]))
treeWidget.insertTopLevelItems(0, items)
treeWidget.show()
sys.exit(app.exec_())
That's what I see:这就是我所看到的:
Any ideas how to get it showing like the second image, from a class?任何想法如何让它像第二张图片一样显示,来自 class?
Any help is greatly appreciated任何帮助是极大的赞赏
The error is simple: Your window (MyTree) is an empty QTreeWidget that has another QTreeWidget inside, so "1" belongs to the empty QTreeWidget.错误很简单:您的 window (MyTree) 是一个空的 QTreeWidget,里面有另一个 QTreeWidget,所以“1”属于空的 QTreeWidget。
There are 2 possible solutions:有两种可能的解决方案:
No user as base class to QTreeWidget没有用户作为基础 class 到 QTreeWidget
class MyTree(QtWidgets.QWidget): def __init__(self, parent=None): super(self.__class__, self).__init__(parent) treeWidget = QtWidgets.QTreeWidget() treeWidget.setHeaderLabels(["Preset Name"]) treeWidget.setColumnCount(1) items = [] for i in range(10): items.append(QtWidgets.QTreeWidgetItem(["item {0}".format(i)])) treeWidget.insertTopLevelItems(0, items) boxLayout = QtWidgets.QVBoxLayout(self) boxLayout.addWidget(treeWidget)
Do not nest a new QTreeWidget不要嵌套新的 QTreeWidget
class MyTree(QtWidgets.QTreeWidget): def __init__(self, parent=None): super(self.__class__, self).__init__(parent) self.setHeaderLabels(["Preset Name"]) self.setColumnCount(1) items = [] for i in range(10): items.append(QtWidgets.QTreeWidgetItem(["item {0}".format(i)])) self.insertTopLevelItems(0, items)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.