?print-pdf
' Created for
Qt (pronounced "cute") framework is a widget toolkit for creating GUIs as well as cross-platform applications that run on various software and hardware platforms such as Linux, Windows, macOS, Android or embedded systems with little or no change in the underlying codebase while still being a native application with native capabilities and speed.[Reference: Qt @wikipediq]
# on activated virtual environment:
pip install PyQt6
# check install info:
pip show PyQt6
pyqt_test.py
file and run it.
import sys
from PyQt6.QtWidgets import QApplication,QWidget
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('PyQt6 App Works')
window.setGeometry(100, 100, 500, 500)
window.show()
sys.exit(app.exec())
You should see a window like:
Qt Designer normally ships as a part of Qt Creator. This is Qt's official editor and lets you do a lot more than just graphically design user interfaces. It is a full-fledged and very powerful C++ IDE. But For PyQT development you don't need the whole Qt Creator (which is huge).
You can install only the Qt Designer in two ways:
# make sure you're on activated virtual environment:
pip install pyqt6-tools
# check install info:
pip show pyqt6-tools
pyuic6
is included in the PyQt6-tools
, so you do not need to install it. Just use it.
pyuic6 helloWorld.ui -o helloWorld.py
You need to perform next steps only if you don't have PyQt6
, PyQt6-sip
and PyQt6-tools
packages
File => Settings => Tools => External Tools
click the +
buttonFile => Settings => Tools => External Tools
click the +
buttonui
file with QtDesigner
import sys
from PyQt6.QtWidgets import (
QApplication, QMainWindow
)
from helloWorld import Ui_MainWindow
class Window(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
Template | Form Type | Widgets | Base Class |
---|---|---|---|
Dialog with Buttons Bottom | Dialog | OK and Cancel buttons laid out horizontally on the bottom-right corner | QDialog |
Dialog with Buttons Right | Dialog | OK and Cancel buttons laid out vertically on the top-right corner | QDialog |
Dialog without Buttons | Dialog | No | QDialog |
Main Window | Main Window | A menu bar at the top and a status bar at the bottom | QMainWindow |
Widget | Widget | No | QWidget |
The Qt team maintains a thorough documentation, as well as user guides, tutorials, etc. for working wit QtDesigner
# 1. import needed QtWidgets classes
from PyQt6.QtWidgets import QApplication, QWidget
# 2. the main app instance for our application.
app = QApplication([])
# 3. Create Qt widget, which will be our main window.
window = QWidget()
# 4. show the window
window.show()
# 5. Start the event loop
app.exec()
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
# Import the compiled UI module
from MainWindow import Ui_MainWindow
class MyApplication(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
# Set up the user interface from Designer
self.setupUi(self)
# Connect signals and slots here, or setup other initializations
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = MyApplication()
mainWindow.show()
sys.exit(app.exec())
Make sure you really understand the concept of class inheritance in Python
# the base class
class Parent:
def __init__(self,*args, **kwargs):
print(f'{self} constructor execute')
print(args)
print(kwargs)
# the derived class, which inherits from base class:
class Child(Parent):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
p1 = Parent(1,2,a=3,b=4)
c = Child(5,6,c=7,d=8)
QWidget
classQWidget
class will be also available in any other widget.
window = MainWindow(cursor=qtc.Qt.CursorShape.WaitCursor)
QWidget
object has its Access function, which allows you to set that property after the object is created
# equivalent to example above
window = MainWindow()
window.setCursor(qtc.Qt.CursorShape.WaitCursor)
# --------------------------- your code starts here -------------------------- #
# create user input widgets:
user_name_input = qtw.QLineEdit()
password_input = qtw.QLineEdit()
#
password_input.setEchoMode(qtw.QLineEdit.EchoMode.Password)
# ---------------------------- your code ends here --------------------------- #
# create user input widgets:
user_name_input = qtw.QLineEdit(self)
password_input = qtw.QLineEdit(self)
geometry
property or its accessor method: setGeometry
class MainWindow(qtw.QWidget):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
#set window geometry
self.setGeometry(300, 200, 500, 300)
...
move(int x, int y)
QWidget widget method
class MainWindow(qtw.QWidget):
def __init__(self , *args, **kwargs):
super().__init__(*args, **kwargs)
#set window geometry
self.setGeometry(300, 200, 500, 300)
# create user input widgets:
user_name_input = qtw.QLineEdit(self)
password_input = qtw.QLineEdit(self)
user_name_input.move(20, 10)
password_input.move(20,50)
# --------------------------- your code starts here -------------------------- #
# create user input widgets:
user_name_input = qtw.QLineEdit()
password_input = qtw.QLineEdit()
password_input.setEchoMode(qtw.QLineEdit.EchoMode.Password)
# create the submit button:
btn_submit = qtw.QPushButton('Login')
# create Form Layout and layout widgets in it
form_layout = qtw.QFormLayout()
form_layout.addRow('User name: ', user_name_input)
form_layout.addRow('Password: ', password_input)
form_layout.addRow(btn_submit)
# apply the form_layout to our widget
# this will attach our form widget's into main window
self.setLayout(form_layout)
# ---------------------------- your code ends here --------------------------- #
self.btn_cancel = qtw.QPushButton('Cancel')
self.btn_cancel.clicked.connect(self.close)
textChanged
signal that sends the text entered into the widget along with the signalsetText()
slot that accepts a string argument. We could connect them like this:
self.line_edit1 = qtw.QLineEdit()
self.line_edit2 = qtw.QLineEdit()
self.line_edit1.textChanged.connect(self.line_edit2.setText)
self.mainLayout = qtw.QVBoxLayout()
self.mainLayout.addWidget(self.line_edit1)
self.mainLayout.addWidget(self.line_edit2)
self.setLayout(self.mainLayout)
line_edit1
) in itline_edit1
that content to be printed in the console.Click on "view raw" link at gist's bottom right corner to view the code, ready for copy-paste :)
@qtc.pyqtSlot(str)
def some_slot(*args):
for arg in args:
print(arg)
pyqtSignal()
function.
# create custom signal which will carry a string data type data:
sig_submit = qtc.pyqtSignal(str)
@qtc.pyqtSlot(bool)
def onSubmit(self):
self.sig_submit.emit(self.edit.text())
self.close()
Note that MainWindow must know form's implementation
Note that MainWindow don't care about form's implementation, it just pass and receive data