PyQt4 到 PyQt5 怎么样? [英] PyQt4 to PyQt5 how?

查看:56
本文介绍了PyQt4 到 PyQt5 怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是用 PyQt4 创建的,我想将其转换为 PyQt5.我尝试了一些脚本来转换代码;但是,除了名字,什么都没有改变.我需要手动更改什么才能使代码与 PyQt5 一起使用?

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)

还有这个代码:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    #style = QApplication.setStyle('plastique')
    window = MyApp()
    window.setFixedSize(750, 320)
    window.show()
    sys.exit(app.exec_())

推荐答案

从 Qt4 到 Qt5 以及从 PyQt4 到 PyQt5 的主要变化是某些类的重新排列,以便 Qt 项目具有可扩展性并生成更小的可执行文件.

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.

QtGui 库分为 2 个子模块:QtGui 和 QtWidgets,在第二个只有小部件,即 QMainWindow、QPushButton 等.这是您必须做出的更改:

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)

注意: PyQt5 中不存在 Phonon,您必须使用 QtMultimedia,您可以在以下答案中找到准确的解决方案:PyQt5 中不存在声子类

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

这篇关于PyQt4 到 PyQt5 怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆