Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Qt for Python Tutorial ClickableButton

From Qt Wiki
Revision as of 16:30, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

English 日本語

A simple clickable button tutorial

In this tutorial, we’ll show you how to start handling with PySide’s signals and slots. Basically, this Qt feature allows your graphical widgets to communicate with other graphical widgets or your own python code. Our application will create a clickable button which will show Hello World in the python console each time you press it.

Let’s starting by importing the necessary Qt classes and python sys class:

Let’s also create a python function which writes “Hello World” to the console:

Now, as mentioned in Your first PySide application, you must create the QApplication which will run your PySide code:

Let’s create the clickable button, a QPushButton. We pass a python string on the constructor which will label the button:

Before we show the button, we must connect it to the sayHello() function that we defined previously. For now, there are two ways of doing this – by using the old style or the new style. The new style is more pythonic and that’s what we’ll use here. You can find more information about both approaches in Signals_and_Slots_in_PySide. The QPushButton has a predefined signal called clicked which is triggered every time that the button is pressed. We’ll just connect this signal to the sayHello() function:

Finally, we show the button and start the Qt main loop:

Full Code

Now that you know the basics, can you get it to print your name?

Categories: