1

I am trying to write a PyQt5 widget which will be a basic preset manager inside an application. I am trying to use the QTreeWidget to display the structure of each type. I have this weird issue, where I see an extra QTreeWidget with a column named "1" outside my QTreeWidget. 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_())

2020欧洲杯时间表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:

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_())

2020欧洲杯时间表That's what I see:

2020欧洲杯时间表Any ideas how to get it showing like the second image, from a class?

2020欧洲杯时间表Any help is greatly appreciated

1

2020欧洲杯时间表The error is simple: Your window (MyTree) is an empty QTreeWidget that has another QTreeWidget inside, so "1" belongs to the empty QTreeWidget.

2020欧洲杯时间表There are 2 possible solutions:

  • No user as base class to 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

    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)
    
| improve this answer | |
  • Both methods work! Thank you very much! I did not notice that using the QTreeWidget as a base class was creating the nesting! – vaco May 24 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy2020欧洲杯时间表

Not the answer you're looking for? Browse other questions tagged or ask your own question.