2017-12-20 3 views
1

QMainWindow (기본적으로 메뉴 영역과 기본 영역에 두 개의 버튼이있는 응용 프로그램)에 추가해야하는 두 개의 버튼이 가로 레이아웃으로 있다고 가정 해보십시오. . 레이아웃을 QMainWindow의 기본 위젯으로 만드는 방법

나는, 그것을 내 두 개의 버튼을 정의 이런 식으로

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

     # Menus 
     exitAct = QAction(QIcon('exit.png'), '&Exit', self) 
     exitAct.setShortcut('Ctrl+Q') 
     exitAct.setStatusTip('Exit application') 
     exitAct.triggered.connect(qApp.quit) 

     self.statusBar() 

     menubar = self.menuBar() 
     fileMenu = menubar.addMenu('&File') 
     fileMenu.addAction(exitAct) 

     # central widget 
     firstButton = QPushButton("first") 
     secondButton = QPushButton("second") 

     hbox = QHBoxLayout() 
     hbox.addWidget(firstButton) 
     hbox.addWidget(secondButton) 

     # Not working because TypeError: setCentralWidget(self, QWidget): argument 1 has unexpected type 'QHBoxLayout' 
     # self.setCentralWidget(hbox) 

     # Not working because centralWidget is not set, therefore is null 
     # self.centralWidget().setLayout(hbox) 

     # Not working because this is a QMainWindow, and the top-level widget already has a layout containing the menu bar for instance 
     self.setLayout(hbox) 

     self.setGeometry(300, 300, 300, 190) 
     self.setWindowTitle('Points') 
     self.show() 

을 구현하려고 수평 레이아웃을 생성하고 레이아웃에 버튼을 추가했습니다. 이제이 창을 사용하도록 내 창에 알려야합니다.

QMainWindow에 이미 레이아웃 (예 : 메뉴 모음)이 있으므로이 레이아웃을 QMainWindow에 추가 할 수 없습니다.

결과적으로 내 버튼이 표시되지 않습니다. 이것을 어떻게 할 수 있습니까?

답변

2

당신은 QWidget를 만들 여기에 레이아웃을 적용하고, 중앙 위젯으로 설정할 수 있습니다

centralWidget = QWidget() 
centralWidget.setLayout(hbox) 
self.setCentralWidget(centralWidget) 
관련 문제