I guess I should start by saying that I’m using
PyQt4 mostly because… well, I don’t have an excuse, just pure laziness when I started writing the code. This is mostly an exercise where I toy with Python and Qt in general; I find it “relaxing” to work on other stuff than eating C++ every day, and Python being as delightful as it is I know I don’t have to worry about memory management at all.
So, in this little app I just wanted to play with slots/signals to see how it was done in PyQt. The documentation in PyQ is messy and that is an understatement, I had to poke a lot of pages in the documentation just to see how it was done. I’m not sure if PySide is any better …
Anyway here’s the code:
from PyQt4.QtGui import QSpacerItem, QApplication, QTextEdit, QPushButton, QWidget, QLabel, QVBoxLayout, QMainWindow from PyQt4.QtCore import Qt, SIGNAL, pyqtSignal, pyqtSlot, QObject import sys app = QApplication(sys.argv) window = QMainWindow() window.setWindowTitle("Qt Rocks!") window.setMinimumWidth(500) window.setMinimumHeight(500) widget = QWidget() textEdit = QTextEdit(widget) textEdit.setFixedHeight(50) pushy = QPushButton("Say my name", widget) spacer = QWidget() label = QLabel("Type your name",widget) @pyqtSlot() def receiver(): label.setText("Hello, %s" % textEdit.toPlainText()) return pushy.clicked.connect(receiver) # _clicked_ is a signal from QPushButton. This form of connection doesn't # get detected by YouCompleteMe in the autocomplete... so gotta read up documentation layout = QVBoxLayout() layout.setAlignment(Qt.AlignHCenter) layout.addWidget(label) layout.addWidget(textEdit) layout.addWidget(pushy) layout.addWidget(spacer) widget.setLayout(layout) window.setCentralWidget(widget) window.show() sys.exit(app.exec_())
I find that if I’m going to invest a lot of my time with Qt. I think Qt/Python makes a devastating (awesome) team in the sense that whilst you don’t have to worry about memory management you can fully focus on writing and gushing out that vital code the business needs . I’d say that depending on the nature of the application Python/Qt will do on most cases. These days memory is cheap, hardware is more powerful than ever… nevertheless always know the nature of the app before choosing a language.
I concluded that hopefully in my next project I’ll be using that combination.