2016-04-25 75 views
3

可能是一個愚蠢的noob問題,但在這裏它是(濃縮的例子):PyQt的主窗口與對話

我已經有了一些基本的代碼來創建一個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一樣?

最好的問候,

回答

2

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") 
+0

完美 - 我的一部分,RTD健康提醒... :-) – BMichell