DEV Community

loading...

Qt Designer and Python: Build Your GUI Applications Faster

jones268 profile image jones268 ・3 min read

Designer aims to provide a lightweight, yet powerful solution for creating PyQt5 applications.

It makes it possible to create GUI applications with drag and drop. It is based on the Qt framework using Qt Designer, which is a visual layout and forms editor.

For those familiar with GUI programming in Python, you may have used the tkinter GUI library.

PyQt applications can be written in multiple ways: pure Python, QML (and its tools) or from Qt Designer's forms.

pyqt designer

Some of the benefits of PyQt Designer:

  • The generated code is fully Pythonic.
  • It makes use of the latest Python features such as Decorators, Generators and Lambdas.
  • It makes use of Python 3.
  • It uses the Qt styles and themes for maximum consistency with your application design.
  • It can generate code compatible with both Qt4 and Qt5.

It features a simple and powerful forms editor, allowing non-programmers to create complex layouts efficiently.

You can use PyQt with any IDE, including PyCharm or simply from vim.

If you are new to PyQt, I recommend this course:
Create Desktop Apps with Python PyQt5

What is Designer

Qt Designer is a graphical user interface (GUI) layout and forms editor, which is part of the Qt Framework.

Qt Designer is a powerful tool for designing user interfaces. Its primary purpose is to create widget and GUI layout specifications that can be compiled and used in applications written in C++. However, Qt Designer is also perfectly capable of generating code for Python applications.

What is PyQt?

PyQt is a set of Python bindings for the Qt cross-platform GUI framework. It provides Python applications with a way to quickly create user interfaces that closely resemble those constructed using Qt Designer.

PyQt also includes tools to deploy applications written in Python. However, it makes no attempt to hide the fact that it generates Python code or that it is not as fast as a natively written Python application.

While PyQt applications are generally faster than their alternatives, they can’t compare well to native applications where the entire application has been written in C++.

Designer or Python?

There are many differences between code generated by a GUI builder and manual PyQt code, and this is one of the strengths of Qt: you have the power to choose what works best for each situation.

There are certainly some situations where an existing Qt Designer UI can be easily converted into Python code. But there are also situations where it makes sense to write directly in Python.

When Should You Use What?

Most of your GUI code should be created using a GUI builder. This is because a GUI builder provides a convenient, visual way to keep all elements aligned and to see the overall structure of your GUI as you build it.

pyqt designer

The GUI builder also manages the layout for you, so that you don't need to worry about trying to work out just how big each element should be compared to all the others when it comes time to actually draw them onto a screen.

Load ui files

The .ui files are XML files that contain user interface definitions along with the layout of widgets within the interface.

In order to use Qt Designer .ui files directly in your code, you need to use Python to load the data from the .ui file. You can do this with a simple Python script or a more complex program.

PyQt-Python makes it easy to create GUI applications with Python. However, if you want to use Qt Designer .ui files directly in your code, you need to use Python to load the data from the .ui file. You can do this with a simple Python script or a more complex program.

from PyQt5 import QtWidgets, uic
import sys

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('basic.ui', self)
        self.show()

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
Enter fullscreen mode Exit fullscreen mode

The following simple Python script demonstrates loading a .ui file and displaying the contents of one of the widgets in the designer. Saves the script as userinterface.py.

Discussion (0)

pic
Editor guide