411 REFERENCE · Wikibooks
PyKDE Programming/QT
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
The structure of a QT Application
A basic QT Application (with a widget)
You will create your first Window in this application.
#!/usr/bin/env python
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
app = QApplication(sys.argv)
win = QWidget()
win.show()
sys.exit(app.exec_())
Did you notice the differences? You did less and got more. But don’t let this fool you. KDE requires this information, but from now on, i doesn’t get any more complicated in KDE than in pure Qt.
Maybe you did spot what we did now and didn’t do earlier: We constructed a widget and showed it. Clicking on the standard “x”-button it closes the whole application by default, but you can change that, of course. You can now transfer the widget-code into the KDE application but you wouldn’t want to write KDE applications if you didn’t want to use KDE’s features, do you?
Let’s move on, but without wasting space: I will leave out everything redundant now, you should keep it in your script, though. Everything we care about now is what we can do between these two lines:
win = QWidget()
win.show()
What about using our window as a frame for other widgets?
In order to do that, we will need a so-called layout and add our widgets to it:
win = QWidget()
layout = QVBoxLayout()
layout.addWidget(QLabel("This is a Label showing text,<br> but it can contain a Picture instead"))
layout.addWidget(QPushButton("Push me"))
win.setLayout(layout)
win.show()
What have we done? Quite something, I guess. We have:
Created and shown our widget, as we did before
Created a layout which stacks widgets vertically (VBox) and told the widget to use it
Added two more widgets to it, both containing text. (As you can see, the QLabel even supports HTML-stuff like the <br>-linebreak)