#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
PyQt4 conversion of Qt Tutorial 4
Create a custom widget.
The original code uses an init() signature of
MyWidget::MyWidget( QWidget *parent, const char *name )
The 'name' parameter is not part of the PyQt4 QWidget __init__() signature
If used, as shown here, do not pass it on to the base class.
The parameter is useful for identifying the widget in CSS style sheets
i.e. QWidget#myWidget{ color: blue; }
Note: QWidget appears to have a default QLayout manager that uses
absolute positioning based on the window's top-left corner
For example if you add the following to init()
btn = QPushButton('Button', self)
btn1 = QPushButton('Button 1', self)
both buttons will be added to the top-left (0,0) corner
with 'btn1' being positioned over 'btn'
Set the geometry on either button to reposition it.
last modified: 2012-01-19 jg
ref:
http://doc.trolltech.com/3.3/tutorial1-04.html
http://riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html
'''
import sys
from PyQt4.QtGui import (QApplication, QWidget, QPushButton, QFont)
class MyWidget(QWidget): # MyWidget subclasses QWidget
def __init__(self, parent=None, name=''):
super(MyWidget, self).__init__(parent) # initialise base (QWidget) class
if name:
self.setObjectName(name)
self.setMinimumSize(200, 120)
self.setMaximumSize(200, 120)
quitBtn = QPushButton('Quit', self)
quitBtn.setGeometry(62, 40, 75, 30) # position the button in the window
quitBtn.setFont(QFont("Times", 18, QFont.Bold))
# Note: the 'QApplication.instance()' returns the
# QApplication 'instance object' whose 'quit' method is invoked
quitBtn.clicked.connect(QApplication.instance().quit)
# test default layout behaviour
btn = QPushButton('Button', self)
btn1 = QPushButton('Button 1', self)
btn1.setGeometry(50, 10, 50, 30) # x, y, width, height
def main():
app = QApplication(sys.argv) # required
w = MyWidget(name='myWidget')
w.setGeometry(100, 100, 200, 120)
w.show()
sys.exit(app.exec_()) # start main event loop, exit when app closed
if __name__ == '__main__':
main()
Sunday, January 22, 2012
Qt Tutorial #1-4 Let there be widgets
This is from Qt Tutorial #1-4 Let there be widgets
Labels:
Qt Tutorial 1