Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt4 to PyQt5 how?

My code was created with PyQt4 and I want to convert it to PyQt5. I have tried some scripts to convert the code; but, nothing changed except the name. What do I need to change manually in order to make the code work with PyQt5?

Here is the first part of my code:

import sys
from pymaxwell import *
from numpy import *
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import QMainWindow, QApplication
from PyQt4.QtCore import *
from PyQt4.phonon import Phonon
from ffmpy import FFmpeg
import os
import app_window_dark
import about

uifile = 'Ui/app_window_dark.ui'
aboutfile = 'Ui/about.ui'

Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)

class About(QtGui.QMainWindow, about.Ui_Dialog):
    def __init__(self, parent=None):
        super(About, self).__init__()
        QtGui.QMainWindow.__init__(self, parent)
        Ui_Dialog.__init__(self)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        point = parent.rect().bottomRight()
        global_point = parent.mapToGlobal(point)
        self.move(global_point - QPoint(395, 265))
        self.setupUi(self)

class MyApp(QtGui.QMainWindow, app_window_dark.Ui_MainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.about_btn.clicked.connect(self.popup)

        #prev next
        self.btn_next.clicked.connect(self.renderSet)
        self.btn_prev.clicked.connect(self.renderSet)

and also this code:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    #style = QApplication.setStyle('plastique')
    window = MyApp()
    window.setFixedSize(750, 320)
    window.show()
    sys.exit(app.exec_())
like image 443
seghier Avatar asked Oct 03 '17 23:10

seghier


1 Answers

The main change from Qt4 to Qt5 and hence from PyQt4 to PyQt5 is the rearrangement of certain classes so that the Qt project is scalable and generates a smaller executable.

The QtGui library was divided into 2 submodules: QtGui and QtWidgets, in the second only the widgets, namely QMainWindow, QPushButton, etc. And that is the change you must make:

[...]
from PyQt5 import QtGui, QtCore, uic, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import *
[...]

Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
Ui_Dialog= uic.loadUiType(uifile)

class About(QtWidgets.QMainWindow, about.Ui_Dialog):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        point = parent.rect().bottomRight()
        global_point = parent.mapToGlobal(point)
        self.move(global_point - QPoint(395, 265))

class MyApp(QtWidgets.QMainWindow, app_window_dark.Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.about_btn.clicked.connect(self.popup)

        #prev next
        self.btn_next.clicked.connect(self.renderSet)
        self.btn_prev.clicked.connect(self.renderSet)

Note: Phonon does not exist in PyQt5, you must use QtMultimedia, an accurate solution you can find it in the following answer: Phonon class not present in PyQt5

like image 89
eyllanesc Avatar answered Sep 20 '22 14:09

eyllanesc