2016-07-12 1 views
4


ステータスバーと他のウィジェットを含むメニューバーをウィンドウに持つPyQtアプリケーションを作成しようとしています。以下はクラスQtGui.QMainWindowメソッドで実行するためのコードです。しかし、私はさらなる機能を追加しようとすると、代わりにQtGui.QWidgetを使用する必要があります実現する。ここでステータスバーとメニューバーを持つPyQt


はコードです:

import sys 
from PyQt4 import QtGui, QtCore 

### How can I use QtGui.QWidget here??? ### 

class Example(QtGui.QMainWindow):         
    def __init__(self): 
      super(Example, self).__init__() 
      self.initUI() 

    def initUI(self):    

     QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))  
     self.setToolTip('This is a <b>QWidget</b> Window widget') 

     exitAction = QtGui.QAction(QtGui.QIcon('exit-icon-2.png'), '&Exit', self) 

     exitAction.setShortcut('Ctrl+Q')       
     exitAction.setStatusTip('Exit/Terminate application') 

     exitAction.triggered.connect(QtGui.qApp.quit)   

     self.statusBar()          

     menubar = self.menuBar()         
     menubar.setToolTip('This is a <b>QWidget</b> for MenuBar')         

     fileMenu = menubar.addMenu('&File')      
     fileMenu.addAction(exitAction)       
     toolbar = self.addToolBar('Exit')      
     toolbar.addAction(exitAction)       

     qbtn = QtGui.QPushButton('Quit', self)     

     qbtn.setToolTip('This is a <b>QPushButton</b> widget') 
     qbtn.clicked.connect(self.launchAAA)      
     qbtn.resize(qbtn.sizeHint())       
     qbtn.move(170, 190)          



     self.setGeometry(500, 180, 400, 400)      
     self.setWindowTitle('Quit button with Message')   
     self.show()            

    def launchAAA(self, event): 

     reply = QtGui.QMessageBox.question(self, 'Message', 
     "Are you sure to quit?", QtGui.QMessageBox.Yes | 
     QtGui.QMessageBox.No, QtGui.QMessageBox.No) 

     if reply == QtGui.QMessageBox.Yes: 
      QtGui.QApplication.quit() 
     else: 
      pass            


def main(): 

    app = QtGui.QApplication(sys.argv)       
    ex=Example() 

    sys.exit(app.exec_())          


if __name__ == '__main__': 
    main() 

私はメニューバーとタイトルバーがQWidgetのメソッドを使用して作成することができるという印象の下にいますが、ここでは動作しません。私はQtGui.QLCDNumberを使ってアプリケーションにLCD機能を追加しようと考えています。

上記の問題を解決する方法に関する提案。ありがとう

+1

@Mailerdaimon QWidgetには、後で必要となるLCDディスプレイ、vbox、hboxなどの機能がありますか?私はあなたの質問に混乱していると感じます、もっと教えてください。 – SamAct

+1

通常の方法は、QMainWindowを作成し、 'QMainWindow :: setCentralWidget(QWidget * widget)'を使用してこのウィンドウの中央QWidgetを設定することです。 Plus:QMainWindowはQWidgetから継承されています。つまり、QWidgetにはさらに多くの機能があります。 – Mailerdaimon

+0

@Mailerdaimon QMainWindowはQWidgetの継承されたすべての機能を持っていますので、それを使用しても問題ありません。中央のQWidgetを設定すると問題が解決するはずですが、上記のコードに変更があった場合は私を案内してくれますか? – SamAct

答えて

2

あなたのコードを使用した実用的なソリューションです。私は今、あなたのqbtnを保持するQMainWindowcentralWidgetcentralLayoutを追加しました:

import sys 
from PyQt4 import QtGui, QtCore 

class Example(QtGui.QMainWindow):         
    def __init__(self): 
      super(Example, self).__init__() 
      self.initUI() 

    def initUI(self):    

     QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))  
     self.setToolTip('This is a <b>QWidget</b> Window widget') 

     exitAction = QtGui.QAction(QtGui.QIcon('exit-icon-2.png'), '&Exit', self) 

     exitAction.setShortcut('Ctrl+Q')       
     exitAction.setStatusTip('Exit/Terminate application') 

     exitAction.triggered.connect(QtGui.qApp.quit)   

     self.statusBar()          

     menubar = self.menuBar()         
     menubar.setToolTip('This is a <b>QWidget</b> for MenuBar')         

     fileMenu = menubar.addMenu('&File')      
     fileMenu.addAction(exitAction)       
     toolbar = self.addToolBar('Exit')      
     toolbar.addAction(exitAction)       

     # Create a central Widgets 
     centralWidget = QtGui.QWidget() 

     # Create a Layout for the central Widget 
     centralLayout = QtGui.QHBoxLayout() 



     qbtn = QtGui.QPushButton('Quit', self)     

     qbtn.setToolTip('This is a <b>QPushButton</b> widget') 
     qbtn.clicked.connect(self.launchAAA)      
     qbtn.resize(qbtn.sizeHint())       
     qbtn.move(170, 190)  

     # Add the Button to the Layout 
     centralLayout.addWidget(qbtn) 

     # Set the Layout 
     centralWidget.setLayout(centralLayout) 

     # Set the Widget 
     self.setCentralWidget(centralWidget)  

     self.setGeometry(500, 180, 400, 400)      
     self.setWindowTitle('Quit button with Message')   
     self.show()            

    def launchAAA(self, event): 

     reply = QtGui.QMessageBox.question(self, 'Message', 
     "Are you sure to quit?", QtGui.QMessageBox.Yes | 
     QtGui.QMessageBox.No, QtGui.QMessageBox.No) 

     if reply == QtGui.QMessageBox.Yes: 
      QtGui.QApplication.quit() 
     else: 
      pass            


def main(): 

    app = QtGui.QApplication(sys.argv)       
    ex=Example() 

    sys.exit(app.exec_())          


if __name__ == '__main__': 
    main() 
+0

コードがうまく動作しているように見えます。しかし、ウインドウのサイズを増やしたり小さくしたりすると、押しボタンがそのサイズを保持することができませんでした。 'qbtn.resize(qbtn.sizeHint())'はこれに何の効果もないようです。私は何をすべきですか? – SamAct

+0

これは新しい質問であると思います。これにより、他の人が同じ問題を抱えていれば、そのソリューションを簡単に見つけることができます。 – Mailerdaimon

2

あなたは自分のボタン/ラベル/などを移動することができます。 QWidgetに移動し、このウィジェットをメインウィンドウに追加します。これはどのように見えるかを示しています(インポートを少し変更して読みやすくしました)。

あなたのコンテンツウィジェットクラス:

class Example(QMainWindow):         
    def __init__(self): 
     super(Example, self).__init__() 
     self.initUI() 

    def initUI(self):    
     QToolTip.setFont(QFont('SansSerif', 10)) 
     self.setToolTip('This is a <b>QWidget</b> Window widget') 

     exitAction = QAction(QIcon('exit-icon-2.png'), '&Exit', self) 
     exitAction.setShortcut('Ctrl+Q') 
     exitAction.setStatusTip('Exit/Terminate application') 
     exitAction.triggered.connect(qApp.quit) 

     self.statusBar() 

     menubar = self.menuBar()   
     menubar.setToolTip('This is a <b>QWidget</b> for MenuBar') 

     fileMenu = menubar.addMenu('&File') 
     fileMenu.addAction(exitAction) 
     toolbar = self.addToolBar('Exit') 
     toolbar.addAction(exitAction) 

     # create the widget here 
     content = ExampleContent(self) 
     self.setCentralWidget(content) 

     self.setGeometry(500, 180, 400, 400)  
     self.setWindowTitle('Quit button with Message') 
     self.show() 

を、すべてがちょうど新しいあなたがQWidgetを持っていることを除いて、以前のように動作します:

class ExampleContent(QWidget): 
    def __init__(self, parent): 
     QWidget.__init__(self, parent) 
     self.initUI() 

    def initUI(self):   
     qbtn = QPushButton('Quit', self) 
     qbtn.setToolTip('This is a <b>QPushButton</b> widget') 
     qbtn.clicked.connect(self.launchAAA)     
     qbtn.resize(qbtn.sizeHint())       
     qbtn.move(170, 190) 

    def launchAAA(self): 
     reply = QMessageBox.question(self, 'Message', 
     "Are you sure to quit?", QMessageBox.Yes | 
     QMessageBox.No, QMessageBox.No) 

     if reply == QMessageBox.Yes: 
      QApplication.quit() 
     else: 
      pass 

はメインウィンドウに追加します代わりにQMainWindowの代わりにそれが助けてくれることを願って!

0

また、QMainWindowとQWidgetを組み合わせて使用​​することもできます。

私はこれが有用な場合があることを知りました。 MainWindowセクションにステータスバーとメニューバーを追加し、QWidgetエリアにウィジェットを追加することができます。

import sys 
from PyQt4 import QtCore, QtGui 


class MainWindow(QtGui.QMainWindow): 

def __init__(self, parent=None): 

    super(MainWindow, self).__init__(parent) 

    self.win_widget = WinWidget(self) 
    widget = QtGui.QWidget() 
    layout = QtGui.QVBoxLayout(widget) 
    layout.addWidget(self.win_widget) 
    self.setCentralWidget(widget) 

    self.statusBar().showMessage('Ready') 

    self.setGeometry(300, 300, 450, 250) 
    self.setWindowTitle('Test') 
    self.setWindowIcon (QtGui.QIcon('logo.png')) 
    self.show() 

    self.win_widget = WinWidget (self) 



class WinWidget (QtGui.QWidget) : 

def __init__(self, parent): 
    super (WinWidget , self).__init__(parent) 
    self.__controls() 
    #self.__layout() 

def __controls(self): 

    self.qbtn = QtGui.QPushButton('Quit', self) 
    self.qbtn. clicked.connect(QtCore.QCoreApplication.instance().quit) 
    self.qbtn.setFixedSize (100,25) 
    self.qbtn.move(50, 50) 


def main(): 

app = QtGui.QApplication(sys.argv) 
win = MainWindow() 
win.show() 
sys.exit(app.exec_()) 

if __name__ == '__main__': 
main() 
関連する問題