QTreeWidget 没有显示在我的 QMainWindow 中

QTreeWidgetdoesn't show into my QMainWindow

我的 QTreeWidget 有问题。他没有出现在我的 QMainWindow 上,但 window 显示成功。

这是我的代码:

 datas = {
    'd1' : {'a': '1', 'b': '2'},
    'd2' : {'a': '2', 'b': 'b'},
    'd1' : {'a': '2', 'b': '2'},
    'd3' : {'a': '3', 'b': '1'},
}

class Main(QMainWindow):
    """ Main App """

    def InitUI(self):

        # Layout
        layout = QVBoxLayout()
        self.setLayout(layout)

        # TreeWidget
        treeView = QTreeView()
        treeView.setModel(QStandardItemModel())
        treeView.setHeaderHidden(False)
        treeView.model().setHorizontalHeaderLabels(['mois', 'support', 'marque'])
        # TreeWidget Items
        for data in datas:
            if not datas[data]:
                continue
            parent = QStandardItem(data)
            parent.setFlags(Qt.NoItemFlags)
            for d in datas[data]:
                value = datas[data][d]
                child0 = QStandardItem(d)
                child0.setFlags(Qt.NoItemFlags | Qt.ItemIsEnabled)
                child1 = QStandardItem(str(value))
                child1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | ~ Qt.ItemIsSelectable)
                parent.appendRow([child0, child1])
            treeView.model().appendRow(parent)

        treeView.expandAll()
        layout.addWidget(treeView)    

希望你能帮我解决:)

谢谢预付

试一试:

import sys
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *


datas = {
    'd1' : {'a': '1', 'b': '2'},
    'd2' : {'a': '2', 'b': 'b'},
    'd1' : {'a': '2', 'b': '2'},
    'd3' : {'a': '3', 'b': '1'},
}

class Main(QMainWindow):
    """ Main App """
    def __init__(self, parent=None):                 # +++
        super().__init__()                           # +++

        self.InitUI()                                # +++

    def InitUI(self):


        # TreeWidget
        treeView = QTreeView()
        treeView.setModel(QStandardItemModel())
        treeView.setHeaderHidden(False)
        treeView.model().setHorizontalHeaderLabels(['mois', 'support', 'marque'])
        # TreeWidget Items
        for data in datas:
            if not datas[data]:
                continue
            parent = QStandardItem(data)
            parent.setFlags(Qt.NoItemFlags)
            for d in datas[data]:
                value = datas[data][d]
                child0 = QStandardItem(d)
                child0.setFlags(Qt.NoItemFlags | Qt.ItemIsEnabled)
                child1 = QStandardItem(str(value))
                child1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | ~ Qt.ItemIsSelectable)
                parent.appendRow([child0, child1])
            treeView.model().appendRow(parent)

        treeView.expandAll()

        centralWidget = QWidget()                    # +++
        self.setCentralWidget(centralWidget)         # +++

        # Layout
        layout = QVBoxLayout(centralWidget)          # + centralWidget
#        self.setLayout(layout)                      # --- 

        layout.addWidget(treeView) 


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())