我正在使用与 Qtable 小部件相关的 class 但它没有出现在 QMainWindow 中

I'm using a class related to the Qtable widget but it doesn't show up in QMainWindow

我无法在 App class 中显示 DataEntryForm class,即在 QMainWindow 中。非常感谢。

#QWidget,包含我要显示的QTableWidget的widget

class DataEntryForm(QWidget):
    def __int__(self):
        super().__init__()

        self.item = 0
        self.data = {"Phone L": 50.5}

        # leftside
        self.table = QTableWidget()
        self.table.setColumnCount(2)
        self.table.setHorizontalHeaderLabels('Descriptions', 'Price')
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.table, 50)
        self.setLayout(self.layout)

#QMainWindow,我想在此 class.

中显示数据输入表单 class
class App(QMainWindow):
    def __init__(self, widget):
        super().__init__()
        self.title = 'Hello, world!'
        self.setWindowTitle(self.title)
        self.resize(1200, 600)
        self.menuBar = self.menuBar()
        self.fileMenu = self.menuBar.addMenu('File')

        # export to csv file action
        exportAction = QAction('Export to csv', self)
        exportAction.setShortcut('Ctrl+E')

        self.setWindowIcon(QIcon('data-analysis_icon-icons.com_52842.ico'))
        # exportAction.triggered.connect

        # exit Action
        exitAction = QAction('Exit', self)
        exitAction.setShortcut("Ctrl+Q")
        exitAction.triggered.connect(lambda: app.quit())

        self.fileMenu.addAction(exportAction)
        self.fileMenu.addAction(exitAction)





if __name__ == '__main__':
    app = QApplication(sys.argv)
    x = DataEntryForm()
    ex = App(x)
    ex.show()
    sys.exit(app.exec_())

QMainWindow 是一种特殊类型的 QWidget,它使用 central widget 显示其主要内容。当您正确创建 DataEntryForm 的实例时,您只是将其作为参数添加到主 window 构造函数中,但您没有对其进行任何操作。

要使用该小部件,请使用 setCentralWidget():

class App(QMainWindow):
    def __init__(self, widget):
        super().__init__()
        self.setCentralWidget(widget)
        # ...

提示:避免为 classes 和不同类型的实例使用相似的名称; app 是 QApplication 的 实例 App 是 QMainWindow class。使用一些不同的和更相关的东西 class,比如“MainWindow”。