【问题标题】:I'm using a class related to the Qtable widget but it doesn't show up in QMainWindow我正在使用与 Qtable 小部件相关的类,但它没有显示在 QMainWindow
【发布时间】:2021-02-09 20:53:42
【问题描述】:

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

#QWidget,包含我要显示的 QTableWidget 的小部件

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,我想在这个类中显示Data Entry Form类。

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

【问题讨论】:

    标签: python pyqt pyqt5 pyside pyside2


    【解决方案1】:

    QMainWindow 是一种特殊类型的 QWidget,它使用 central widget 来显示其主要内容。虽然您正确地创建了 DataEntryForm 的实例,但您只是将其作为参数添加到主窗口构造函数中,但您什么也没做。

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

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

    提示:避免不同类型的类和实例使用相似的名称; app 是 QApplication 的 instanceApp 是 QMainWindow 类。为该类使用不同且更相关的内容,例如“MainWindow”。

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多