Qt for Python Tutorial SimpleDialog

From Qt Wiki
Jump to: navigation, search

Creating a simple PySide2 dialog application

In this tutorial we will show how to build a simple dialog with some basic widgets. The idea is that the user inserts his name in a QLineEdit, clicks a QPushButton and sees greetings.

Let us just start with a simple stub which creates and shows a dialog. This stub will be updated in the course of this tutorial but you can use this stub to your own PySide2 projects if you need to.

 1 import sys
 2 from PySide2.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton
 4 class Form(QDialog):
 6     def __init__(self, parent=None):
 7         super(Form, self).__init__(parent)
 8         self.setWindowTitle("My Form")
11 if __name__ == '__main__':
12     # Create the Qt Application
13     app = QApplication(sys.argv)
14     # Create and show the form
15     form = Form()
16     form.show()
17     # Run the main Qt loop
18     sys.exit(app.exec_())

The imports aren't new to you, the same for the creation of the QApplication and the execution of the Qt main loop. The only novelty here is the class definition.

You can create any class which subclasses PySide2 widgets. In this case, we are just subclassing the QDialog to do our custom dialog which we've called "Form". We have also implemented the init() method which calls the QDialog init method with the parent widget, if any. Also new, is the setWindowTitle() which just sets the title of the dialog window. Also, in main(), you can see that we are creating a Form object, and showing it to the world.

Create the widgets

We are going to create two widgets a QLineEdit where the user can insert his name, and a QPushButton which we will use to print the contents of the QLineEdit. So, we are going to add the following code to the init() method of our Form:

1 # Create widgets
2 self.edit = QLineEdit("Write my name here..")
3 self.button = QPushButton("Show Greetings")

As should be intuitive by now, both widgets will show the corresponding texts.

Create a layout to organize the widgets

Qt comes with some layouts which helps the user on the organization of his widgets on an application. In this case, we will make it simple and create a QVBoxLayout which will distribute the widgets vertically. The following code will be added also to the init() method, after the creation of the widgets:

1 # Create layout and add widgets
2 layout = QVBoxLayout()
3 layout.addWidget(self.edit)
4 layout.addWidget(self.button)
5 # Set dialog layout
6 self.setLayout(layout)

So, we create the layout, add the widgets with addWidget(), and finally we say that our Form will have our QVBoxLayout as its layout.

Create the function to greet and connect the button

Finally, we just have to add a function to our custom Form- the one which will greet the user - and connect our button to it. Our function will be a part of the Form, so you have to add it after the init() function:

1 # Greets the user
2 def greetings(self):
3     print ("Hello {}".format(self.edit.text()))

Our function just writes the contents of the QLineEdit to the python console. We have access to the text by means of the QLineEdit.text() method.

Now that we have everything, we just need to connect the QPushButton to the Form.greetings() method. In the init() method we just need to add:

1 # Add button signal to greetings slot
2 self.button.clicked.connect(self.greetings)

Once executed, you can insert your name in the QLineEdit and watch the console for greetings.

Full Code

Here is the full code for this tutorial:

 1 #!/usr/bin/python
 2 # -'''- coding: utf-8 -'''-
 4 import sys
 5 from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication,
 6     QVBoxLayout, QDialog)
 8 class Form(QDialog):
10     def __init__(self, parent=None):
11         super(Form, self).__init__(parent)
12         # Create widgets
13         self.edit = QLineEdit("Write my name here")
14         self.button = QPushButton("Show Greetings")
15         # Create layout and add widgets
16         layout = QVBoxLayout()
17         layout.addWidget(self.edit)
18         layout.addWidget(self.button)
19         # Set dialog layout
20         self.setLayout(layout)
21         # Add button signal to greetings slot
22         self.button.clicked.connect(self.greetings)
24     # Greets the user
25     def greetings(self):
26         print ("Hello %s" % self.edit.text())
28 if __name__ == '__main__':
29     # Create the Qt Application
30     app = QApplication(sys.argv)
31     # Create and show the form
32     form = Form()
33     form.show()
34     # Run the main Qt loop
35     sys.exit(app.exec_())