Sunday, September 1, 2013

PyQt: Qt Widget with button with a fixed margin from bottom right corner

PyQt4 provides an easy to use interface for the creation of GUI Applications. In order to get some practice in PyQt4 I decided to create a subclass WidgetWithQuit of QWidget that has a QuitButton (derived form QPushButton) as member variable that sends a quit signal to the qapp on a left click.
Thanks to the high level interface of PyQt4 this requires only a small modification of the resizeEvent function of QWidget (see code below).


from PyQt4 import QtGui, QtCore
import sys


class QuitButton(QtGui.QPushButton):
    def __init__(self, parent, text="Quit"):
        QtGui.QPushButton.__init__(self, text, parent)
        parent.connect(self, QtCore.SIGNAL('clicked()'),
                       QtGui.qApp, QtCore.SLOT('quit()'))
        self.resize(self.sizeHint().width(), 
                    self.sizeHint().height())


class WidgetWithQuit(QtGui.QWidget):
    """QWidget that has a QuitButton at the bottom right;
    margin specified in the constructor"""
    def __init__(self, parent=None, buttontext="Quit", margin=10):
        self.margin = margin
        QtGui.QWidget.__init__(self, parent)
        self.quit_button = QuitButton(self, text=buttontext)
        self.align_button()

    def align_button(self):
        widget_geometry = self.geometry()
        button_geometry = self.quit_button.geometry()
        width_offset = widget_geometry.width() - (button_geometry.width() + self.margin)
        height_offset = widget_geometry.height() - (button_geometry.height() + self.margin)
        self.quit_button.move(width_offset, height_offset)

    def resizeEvent(self, event):
        QtGui.QWidget.resizeEvent(self, event)
        self.align_button()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    widget = WidgetWithQuit()
    widget.setGeometry(300, 300, 100, 100)
    widget.quit_button.setToolTip('Quit on left mouse click')
    widget.show()
    sys.exit(app.exec_())

Of course this is just a very simple, "academic" example; still it should be clear that PyQt4 offers an easy to use interface to build user-friendly GUI applications.
Thanks to C/C++ binding generators such as boost python you can still rely on fast C/C++ for the underlying algorithms which gives you the best of both worlds: easy implementation of the GUI using a high level language and fast algorithms working on (big) data in the background.
For the documentation of the classes used above, see:

No comments:

Post a Comment