📜 ⬆️ ⬇️

Writing Qt-based Python Applications

Good time of day.
Recently decided to learn another programming language. The choice fell on python. Wrote a few small scripts. But above all, I wanted to write applications with a graphical interface. On the Internet I came across this small tutorial, the reading of which resulted in the translation offered to your attention. I hope that it will be useful to someone.


This tutorial aims to get an idea of ​​how to write small python applications using the Qt library.


To successfully complete all tasks, you need to have basic knowledge of python, however, Qt is not necessary to know. I use linux in these examples and I assume that you already have working Python and PyQt . To test this, open the python shell by simply typing “python” in the console in and type
')
  import qt 


If no error message appears, you are ready to begin. The examples in this manual are simplified as much as possible, and show useful ways to write and structure your program. It is important that you read the source code of the examples, most of the things explained there. Use examples and try to change them, play with them. This is the best way to get used to.

Hello, world!


Let's start with the simple. The program opens a window and shows something. The code below shows a window that says “Hello, world!”. Obviously.

  #! / usr / bin / python
 import sys
 from qt import *
 # create an application and pass arguments
 a = QApplication (sys.argv)
 # create widget
 # The first argument is the text we want to see.
 The second argument is the parent widget, 
 # because  Hello - the only widget, then he has no parent
 hello = QLabel ("Hello world!", None)
 # make the widget mainly
 a.setMainWidget (hello)
 # show widget
 hello.show ()
 #launch the application
 a.exec_loop ()


About 7 lines of code, and it is as simple as possible.
And this is how it looks like:


Button


Add a bit of interactivity! We will replace the “Hello, World!” With the button and assign an action to it. This designation is called “signal attachment”, an event that is sent to the slot when the button is pressed, which is a function. This function is triggered when an event occurs.

  #! / usr / bin / python
 # coding = utf-8
 import sys
 from qt import *
 a = QApplication (sys.argv)
 # Our function to be called when a button is pressed
 def sayHello ():
     print "Hello, World!"
 # create button
 hellobutton = QPushButton ("Say 'Hello world!'", None)
 # assign a click handler to the button
 a.connect (hellobutton, SIGNAL ("clicked ()"), sayHello)
 # assign a button to the main widget
 a.setMainWidget (hellobutton)
 # show shows
 hellobutton.show ()
 #launch the application
 a.exec_loop ()




This is more like a normal application.


You can imagine that programming in this way is not expandable and do not stop to continue. Well, let's do it in Python, adding structure and using an object-oriented approach. We will create our own class inherited from QApplication and put the things we need into its methods: one method to create widgets and a slot that holds the code that runs when the signal arrives.

  #! / usr / bin / python
 # coding = UTF-8
 import sys
 from qt import *
 # class inherits from QApplication
 class HelloApplication (QApplication):
     def __init __ (self, args):
         "" "
             In the constructor, we do everything we need to run our application, which
             creates a QApplication in the __init__ method, then adds our widgets, and finally
             runs exec_loop
         "" "
         QApplication .__ init __ (self, args)
         self.addWidgets ()
         self.exec_loop ()        
     def addWidgets (self):
         "" "In this method, we add widgets and attach signal handlers.
             The signal handler for the widget is also called a "slot"
         "" "
         self.hellobutton = QPushButton ("Say 'Hello, World!'", None)
         self.connect (self.hellobutton, SIGNAL ("clicked ()"), self.slotSayHello)
         self.setMainWidget (self.hellobutton)
         self.hellobutton.show ()
     def slotSayHello (self):
         "" "
             This is an example of a slot - a method that is called when a signal comes.
         "" "
         print "Hello, World!"
 # This script should only be executed separately, so we have to check it,
 # but we should also be able to connect this program without running any code
 if __name__ == "__main__":
     app = HelloApplication (sys.argv)


Interface coding sucks



So we want to use Qt3 Designer to create a GUI. In the picture you can see a simple interface. The names of the widgets are written in green. All we have to do
  1. Compile a .ui file from Qt designer into a python class
  2. Write a subclass and use it as mainWidget

That way, we can change the interface in Qt designer without digging through the code

Team
  pyuic testapp_ui.ui -o testapp_ui.py 


make a python class with which we can work.

The work of our program can be described as follows:
  1. We fill lineedit
  2. By clicking on the add button, we will attach a method that reads text from lineedit and adds it to the listview.
  3. Clicking on the delete button will remove the selected item from the listview.

Here is the code with comments:
  #! / usr / bin / python
 # coding = utf-8
 from testapp_ui import TestAppUI
 from qt import *
 import sys
 class HelloApplication (QApplication):
     def __init __ (self, args):
         "" "
             In the constructor, we do everything that is necessary to run our application, which
             creates a QApplication in the __init__ method, then adds our widgets, and finally
             runs exec_loop
         "" "
         QApplication .__ init __ (self, args)
         # We pass None because  this top level widget
         self.maindialog = TestApp (None)
         self.setMainWidget (self.maindialog)
         self.maindialog.show ()
         self.exec_loop ()
 class TestApp (TestAppUI):
     def __init __ (self, parent):
         # Run the parent constructor and attach the slots to the methods
         TestAppUI .__ init __ (self, parent)
         self._connectSlots ()
         # Initially, the list is empty, so the delete button should not work
         # Make it inactive
         self.deletebutton.setEnabled (False)
     def _connectSlots (self):
         # Install handlers on the buttons
 self.connect (self.addbutton, SIGNAL ("clicked ()"), self._slotAddClicked)
 self.connect (self.lineedit, SIGNAL ("returnPressed ()"), self._slotAddClicked)
 self.connect (self.deletebutton, SIGNAL ("clicked ()"), self._slotDeleteClicked)
     def _slotAddClicked (self):
         # Read teskt from lineedit,
         text = self.lineedit.text ()
         # if lineedit is not empty,
         if len (text):
             # insert a new item in the list ...
             lvi = QListViewItem (self.listview)
             # with text from lineedit ...
             lvi.setText (0, text)
             # and clear the lineedit.
             self.lineedit.clear ()
             # Delete button m.  off, so turn it on.
             self.deletebutton.setEnabled (True)
     def _slotDeleteClicked (self):
         # Remove the selected item from the list
         self.listview.takeItem (self.listview.currentItem ())
         # Check if the list is empty - if yes, disable the deletebutton.
         # If the list after this was empty, then we will make the delete button inactive
         if self.listview.childCount () == 0:
             self.deletebutton.setEnabled (False)
 if __name__ == "__main__":
     app = HelloApplication (sys.argv)


And here is the result of works:

Good to know


Creating an interface in Qt designer not only makes it easier to create a GUI, but it is also a good tool for learning Qt. You can test how the widget looks, see what is available in Qt, and see the properties you may want to use.

API documentation for C ++ is also very useful (read required) when working with PyQt. The API is translated very simple, so after practicing, you will realize that the API documentation for developers is one of the tools you really need. While working in KDE, you can type qt: [widgetname] in concver. So qt: qbutton will open the documentation for you directly on the description of qbutton. The Trolltech site contains much more documentation that you may find useful.

The examples in this manual are created using Qt 3.3. I might be updating the manual when a usable version of pyQt for Qt 4 appears.

Source: https://habr.com/ru/post/31426/


All Articles