I am trying to create a PyQt application that has both status bar and a menu bar with other Widgets in the window. Below is the code which I managed to get it run with class QtGui.QMainWindow
method. But as I intend to add further features, I realise I must use QtGui.QWidget
instead.
Here is the code:
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()
I am under the impression that menu bar and title bars can be created using QWidget method, but here it doesn't work. I intend to add an LCD function to the application using QtGui.QLCDNumber
.
Any suggestions on how to fix the above problem. Thanks
You could just move your buttons/labels/etc. to a QWidget
, and add this widget to the main window. Here is how it could look like (I changed the imports so that it is a bit more readable).
Your content widget class :
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
Add it to the main window :
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()
And everything just works as before, except that you new have a QWidget
in the middle, instead of a QMainWindow
. Hope that helped !