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:
<br /> from PyQt4.QtGui import QSpacerItem, QApplication, QTextEdit, QPushButton, QWidget, QLabel, QVBoxLayout, QMainWindow<br /> from PyQt4.QtCore import Qt, SIGNAL, pyqtSignal, pyqtSlot, QObject<br /> import sys</p> <p>app = QApplication(sys.argv)</p> <p>window = QMainWindow()<br /> window.setWindowTitle("Qt Rocks!")<br /> window.setMinimumWidth(500)<br /> window.setMinimumHeight(500)</p> <p>widget = QWidget()<br /> textEdit = QTextEdit(widget)<br /> textEdit.setFixedHeight(50)<br /> pushy = QPushButton("Say my name", widget)<br /> spacer = QWidget()<br /> label = QLabel("Type your name",widget)</p> <p>@pyqtSlot()<br /> def receiver():<br /> label.setText("Hello, %s" % textEdit.toPlainText())<br /> return</p> <p>pushy.clicked.connect(receiver)<br /> # _clicked_ is a signal from QPushButton. This form of connection doesn't<br /> # get detected by YouCompleteMe in the autocomplete... so gotta read up documentation</p> <p>layout = QVBoxLayout()<br /> layout.setAlignment(Qt.AlignHCenter)<br /> layout.addWidget(label)<br /> layout.addWidget(textEdit)<br /> layout.addWidget(pushy)<br /> layout.addWidget(spacer)<br /> widget.setLayout(layout)<br /> window.setCentralWidget(widget)<br /> window.show()</p> <p>sys.exit(app.exec_())<br />
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.