Last Updated on 2021-09-08 by Clay
Qt Designer is an auxiliary program dedicated to the Python PyQt5 graphical interface framework. Simply put, it has a graphical interface to help us make the program interface we want, which is very convenient and easy to use.
I have written about the installation instructions in Windows and Linux, and I feel that they are relatively simple. Only on Mac OS I have always heard that I can only install the relatively large Qt Creator, which makes me a little daunted.
Why do I say this? This is because Qt Creator is mainly a tool for Qt in C++. If you just want to write a Python version of PyQt, you can save a lot of space (dozens of GB?), just install a lightweight version of Qt Designer.
Install Qt Designer
In Mac OS, if you want to install Qt Designer, you can go the link: https://build-system.fman.io/qt-designer-download
After downloading, double-click the downloaded file to install it. However, when running Qt Designer, most of the following problems will cause it to fail to open:
“Qt Designer” cannot be opened because the developer cannot be verified.
This seems to be a problem cause by the lack of verification. Whether to use it or not, you have to weigh it yourself. But I personally will not hesitate! Firstly, I really need Qt Designer, and secondly, the GitHub project has nearly 3,000 starts!
Ok, let's explain how to force it to open on now.
First click on the Apple icon in the upper right corner and select System Preferences ... > Security & Privacy > General.
And you will see:
Open Anyway.
In this way, Qt Designer can be started normally! But the components are separated! This should be normal. (Linux and Windows are all integrated)
After the installation is complete, let's run a simple example to confirm it.
Sample program for testing
In short, first use Qt Designer to make an interface casually, and I will use the following casual interface test:
Then I saved it and called it test.ui (the files stored in Qt Designer are all in .ui format)
If you have no PyQt5 in your environment, you can use the following command to install it:
pip3 install pyqt5
Then we go to the folder that saved the test.ui, open terminal and key in:
python3 -m PyQt5.uic.pyuic test.ui -o test.py
PyUIC is a PyQt5 package tool, it can convert .ui file to .py file. Open the converted test.py file, you will see the following code:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test.ui' # # Created by: PyQt5 UI code generator 5.15.1 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(437, 334) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget) self.plainTextEdit.setGeometry(QtCore.QRect(20, 10, 401, 231)) self.plainTextEdit.setObjectName("plainTextEdit") self.textEdit = QtWidgets.QTextEdit(self.centralwidget) self.textEdit.setGeometry(QtCore.QRect(20, 250, 321, 31)) self.textEdit.setObjectName("textEdit") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(340, 240, 91, 51)) self.pushButton.setObjectName("pushButton") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 437, 22)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "Enter"))
And we write another program that import the test.py script.
# coding: utf-8 import sys from PyQt5.QtWidgets import * from test import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_())
Output:
It looks exactly the same as the picture I show above.
References
- https://stackoverflow.com/questions/5044353/qt-designer-for-pyqt-on-osx-10-6
- https://github.com/hashicorp/terraform/issues/23033