簡體   English   中英

PyQt主窗口與對話框

[英]PyQt Main Window vs. Dialog

可能是一個愚蠢的菜鳥問題,但在這里它是(濃縮的例子):

我有一些基本代碼來創建QDialog。 在實踐中,這是運作良好,我有一些東西,創建一個Pyqtgraph窗口,加載和繪制數據等:

import sys
from PyQt4 import QtGui

#class Window(QtGui.QMainWindow):
class Window(QtGui.QDialog):

    def __init__(self):
        super(Window, self).__init__()

        # Button to load data
        self.LoadButton = QtGui.QPushButton('Load Data')
        # Button connected to `plot` method
        self.PlotButton = QtGui.QPushButton('Plot')

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.LoadButton)
        layout.addWidget(self.PlotButton)

        self.setLayout(layout)

        self.setGeometry(100,100,500,300)
        self.setWindowTitle("UI Testing")


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

但是我想創建它作為QMainWindow(現在只是為了獲得最大化,關閉等按鈕)但是如果我將類定義更改為:

class Window(QtGui.QMainWindow):

當我運行代碼時,我得到一個空白的主窗口。 所以簡單的問題是,我需要做什么來使布局顯示在QMainWindow中的QDialog中?

最好的祝福,

來自doc

注意:不支持創建沒有中央窗口小部件的主窗口。 即使它只是一個占位符,您也必須擁有一個中央窗口小部件。

所以應該創建和設置中央窗口小部件:

    def __init__(self):
        super(Window, self).__init__()

        # Button to load data
        self.LoadButton = QtGui.QPushButton('Load Data')
        # Button connected to `plot` method
        self.PlotButton = QtGui.QPushButton('Plot')

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.LoadButton)
        layout.addWidget(self.PlotButton)

        # setup the central widget
        centralWidget = QtGui.QWidget(self)
        self.setCentralWidget(centralWidget)
        centralWidget.setLayout(layout)

        self.setGeometry(100,100,500,300)
        self.setWindowTitle("UI Testing")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM