📝

PyQt5와 PyQt6가 달라진점(21년 5월 업데이트)

PyQt6는 21년 2월 6일에 6.0.0.1 Version이 배포 되었습니다.
기본적인 hello world를 출력해보도록 하겠습니다.
<PyQt5>
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot def helloworld(): app = QApplication(sys.argv) widget = QWidget() textLabel = QLabel(widget) textLabel.setText("Hello World!") textLabel.move(150, 200) widget.setGeometry(50, 50, 400, 400) widget.setWindowTitle("PyQt5 Example") widget.show() sys.exit(app.exec_()) 프로그램무한반복 = QApplication(sys.argv) 실행인스턴스 = helloworld() 프로그램무한반복.exec_()
<PyQt6>
import sys from PyQt6.QtWidgets import QApplication, QWidget, QLabel from PyQt6.QtGui import QIcon from PyQt6.QtCore import pyqtSlot def helloworld(): app = QApplication(sys.argv) widget = QWidget() textLabel = QLabel(widget) textLabel.setText("Hello World!") textLabel.move(150, 200) widget.setGeometry(50, 50, 400, 400) widget.setWindowTitle("PyQt5 Example") widget.show() sys.exit(app.exec()) 프로그램무한반복 = QApplication(sys.argv) 실행인스턴스 = helloworld() 프로그램무한반복.exec()
위 두 코드의 차이점은 아래와 같습니다.
  1. exec_()가 exec()가 되었습니다.
  1. PyQt5가 PyQt6가 되었습니다.
 
공식문서에는 어떤 내용이 담겨있는지 한 번 살펴보도록 하겠습니다.
 
아래 영문 스크립트를 가져왔습니다.
In this section we give an overview of the differences between PyQt6 and PyQt5. This is not an exhaustive list and does not go into the detail of the differences between the Qt v6 and Qt v5 APIs.
  • All named enums are now implemented as a sub-class of the standard Python Enum class. (PyQt5 used IntEnum for scoped enums and a custom type for traditional named enums).
  • Qt provides the QFlags template class as a type-safe way of using enum values that can be combined as a set of flags. The name of the class is often the plural form of the name of the enum. PyQt5 implements both of these as separate types. PyQt6 instead combines them as a single type, using the name of the enum, as a sub-class of Flag.
  • Q_ENUM()Q_ENUMS()Q_FLAG() and Q_FLAGS() have been replaced by the pyqtEnum() class decorator.
  • All exec_() and print_() methods have been removed.
  • qApp has been removed.
  • The PYQT_CONFIGURATION dict has been removed.
  • The Qt module has been removed.
  • The bindings for the (GPL licensed) Qt classes that implement support for network authorisation have moved out to a separate add-on project PyQt6-NetworkAuth. This means that all of the libraries wrapped by PyQt6 itself are licensed under the LGPL.
  • pylupdate6 is a completely new pure-Python implementation. It can no longer read a .pro file in order to determine the names of .py files to translate.
  • Support for Qt’s resource system has been removed (i.e. there is no pyrcc6).
  • Python v3.6.1 or later is required.
Qt v6 implements a number of functions from Qt v5 that are now marked as being deprecated. These are not supported in PyQt6.
 
주목할만한 점은 Qt 모듈이 삭제된 것과 Python 3.6.1 Version이 필요하다는 것 정도가 될 것 같습니다.
아래 코드를 PyQt6로 변환할 때에는 모듈명이 조금씩 다른 것이 있기 때문에 공식문서에서 검색을 해보시면서 변경해가시는 것이 좋습니다.
예를 들어 Qt에 RightButton은 PyQt5에서는 아래와 같이 사용할 수 있었습니다.
if event.button() == Qt.RightButton: print('마우스 오른쪽 버튼을 클릭하셨어요.')
하지만 PyQt6에서는 하위 클래스로 구현되어 아래와 같이 사용하셔야 합니다.
if event.button() == Qt.MouseButtons.RightButton: print('마우스 오른쪽 버튼을 클릭하셨어요.')
 
PySide6도 사용법이 비슷하여 간단한 튜토리얼 Code를 명시합니다.
<PySide6>
import sys from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox from PySide6.QtGui import QCloseEvent class ExampleWindow(QWidget): def __init__(self): super().__init__() self.setup() def setup(self): btn_quit = QPushButton('Force Quit', self) btn_quit.clicked.connect(QApplication.instance().quit) btn_quit.resize(btn_quit.sizeHint()) btn_quit.move(90, 100) self.setGeometry(100, 100, 200, 150) self.setWindowTitle('Window Example') self.show() def closeEvent(self, event: QCloseEvent): reply = QMessageBox.question(self, 'Message', 'Are you sure you want to quit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() def run(): app = QApplication(sys.argv) ex = ExampleWindow() sys.exit(app.exec()) if __name__ == '__main__': run()
출처 : https://medium.com/weekly-python/getting-started-writing-qt-6-applications-in-python-with-pyside6-389ee4c384ee