如何在python中从pyqt4升级到pyqt5 [英] how to upgrade from pyqt4 to pyqt5 in python

查看:115
本文介绍了如何在python中从pyqt4升级到pyqt5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此代码从 pyqt4 升级或转换为 pyqt5,因为此代码与最新的 pyqt5 不兼容.

那么谁能告诉我我可以对这段代码进行哪些重大更改才能在 pyqt5 中运行它.

导入系统从 PyQt4.QtCore 导入 Qt从 PyQt4.QtCore 导入 QRectF从 PyQt4.QtWidgets 导入 QApplication从 PyQt4.QtGui 导入 QColor从 PyQt4.QtGui 导入 QFont从 PyQt4.QtGui 导入 QPainter从 PyQt4.QtGui 导入 QPixmap从 PyQt4.QtGui 导入 QTextOption从 PyQt4.QtGui 导入 QToolTip从 PyQt4.QtGui 导入 QWidget

这是这段代码的所有导入库

class AreaSelector(QWidget):def __init__(self, parent=None):QWidget.__init__(self, None, Qt.FramelessWindowHint)self.setAttribute(Qt.WA_TranslucentBackground)self.setAttribute(Qt.WA_DeleteOnClose)self.setWindowState(Qt.WindowFullScreen)self.setAutoFillBackground(False)self.parent = 父母self.start_x = 0self.start_y = 0self.end_x = 0self.end_y = 0self.current_x = 0self.current_y = 0def showEvent(self, event):self.bg = QPixmap.grabWindow(QApplication.desktop().winId())self.screen_geometry = QApplication.desktop().screenGeometry(self)def mousePressEvent(self, event):self.start_x = event.globalX()self.start_y = event.globalY()def mouseReleaseEvent(self, event):self.end_x = event.globalX()self.end_y = event.globalY()

请在此处查看完整代码

观察QT += widgets中表示属于QtWidgets子模块的部分.

但是第二种情况稍微复杂一些,因为它涉及寻找可能在同一个类中也可能不在同一个类中的等价物,在这种情况下,它发生在 QPixmap.grabWindow() 方法中已弃用(参见此处了解更多信息).进行搜索后,您可以用 QApplication.primaryScreen().grabWindow(0) 替换该代码.

综合以上,翻译如下:

导入系统从 PyQt5.QtCore 导入 QRectF, Qt从 PyQt5.QtGui 导入 QColor、QFont、QPainter、QPixmap、QTextOption、QScreen从 PyQt5.QtWidgets 导入 QApplication、QToolTip、QWidget类区域选择器(QWidget):def __init__(self, parent=None):QWidget.__init__(self, None, Qt.FramelessWindowHint)self.setAttribute(Qt.WA_TranslucentBackground)self.setAttribute(Qt.WA_DeleteOnClose)self.setWindowState(Qt.WindowFullScreen)self.setAutoFillBackground(False)self.parent = 父母self.start_x = 0self.start_y = 0self.end_x = 0self.end_y = 0self.current_x = 0self.current_y = 0def showEvent(self, event):self.bg = QApplication.primaryScreen().grabWindow(0)self.screen_geometry = QApplication.primaryScreen().geometry()def mousePressEvent(self, event):self.start_x = event.globalX()self.start_y = event.globalY()def mouseReleaseEvent(self, event):self.end_x = event.globalX()self.end_y = event.globalY()def mouseMoveEvent(self, event):self.current_x = event.globalX()self.current_y = event.globalY()self.repaint()text = "开始:%sx%s \n结束:%sx%s" % (self.start_x,self.start_y,self.current_x,self.current_y,)QToolTip.showText(event.pos(), text)def keyPressEvent(self, event):如果 event.key() == Qt.Key_Return:self._acceptSelection()elif event.key() == Qt.Key_Escape:self.close()def _acceptSelection(self):如果 self.parent 不是 None:self.parent.areaSelectEvent(self.start_x、self.start_y、self.end_x、self.end_y)self.close()defpaintEvent(self, event):画家 = QPainter()画家.开始(自我)Painter.fillRect(self.screen_geometry, QColor(10, 10, 10, 125))self._paint_selection(画家)self._paint_usage_text(画家)画家.end()def _paint_usage_text(self,painter):font = QFont("Helvetica [Cronyx]", 26, QFont.Bold)Painter.setFont(字体)Painter.setPen(QColor(255, 255, 255, 255))screen_width = self.screen_geometry.width()文本宽度 = 800text_start_x = screen_width/2 - text_width/2screen_height = self.screen_geometry.height()文本高度 = 200text_start_y = screen_height/2 - text_height/2textoption = QTextOption(Qt.AlignCenter)textbox = QRectF(text_start_x, text_start_y, text_width, text_height)画家.drawText(文本框,"单击并拖动以选择一个区域\n" "ENTER 确认或 ESC 取消",文本选项,)Painter.drawRoundedRect(textbox, 20, 20)def_paint_selection(self,painter):"""绘制当前用户选择"""矩形 = QRectF()如果 self.start_x >self.current_x:矩形.setLeft(self.current_x)矩形.setRight(self.start_x)别的:矩形.setLeft(self.start_x)矩形.setRight(self.current_x)如果 self.start_y >self.current_y:矩形.setTop(self.current_y)矩形.setBottom(self.start_y)别的:矩形.setTop(self.start_y)矩形.setBottom(self.current_y)Painter.drawPixmap(矩形,self.bg,矩形)Painter.drawRect(矩形)如果 __name__ == "__main__":app = QApplication(sys.argv)main = AreaSelector()main.show()sys.exit(app.exec_())

I want to upgrade or convert this code from pyqt4 to pyqt5 as this code is not compatible with latest pyqt5.

So can someone tell me what major changes i can make in this code to run it in pyqt5.

import sys
from PyQt4.QtCore import Qt
from PyQt4.QtCore import QRectF
from PyQt4.QtWidgets import QApplication
from PyQt4.QtGui import QColor
from PyQt4.QtGui import QFont
from PyQt4.QtGui import QPainter
from PyQt4.QtGui import QPixmap
from PyQt4.QtGui import QTextOption
from PyQt4.QtGui import QToolTip
from PyQt4.QtGui import QWidget

this are all the imported libraries for this code

class AreaSelector(QWidget):

    def __init__(self, parent=None):

        QWidget.__init__(self, None, Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setWindowState(Qt.WindowFullScreen)
        self.setAutoFillBackground(False)

        self.parent = parent
        self.start_x = 0
        self.start_y = 0
        self.end_x = 0
        self.end_y = 0
        self.current_x = 0
        self.current_y = 0

    def showEvent(self, event):

        self.bg = QPixmap.grabWindow(QApplication.desktop().winId())
        self.screen_geometry = QApplication.desktop().screenGeometry(self)

    def mousePressEvent(self, event):

        self.start_x = event.globalX()
        self.start_y = event.globalY()

    def mouseReleaseEvent(self, event):

        self.end_x = event.globalX()
        self.end_y = event.globalY()

please view the full code here full code

解决方案

Translating a PyQt4 code to PyQt5 is not a trivial task:

  • PyQt4 and PyQt5 are wrappers of Qt4 and Qt5, respectively, so both are affected by the changes of that transition, and one of the transitions is that the QtGui sub-module of Qt4 was divided into the QtGui and QtWidgets sub-modules of Qt5.
  • Some classes and methods are deprecated so you will have to find an equivalent if it exists.

In this case both things happen, the solution for the first case is simple: You must look in the Qt docs and check to which sub-module it belongs, for example QToolTip, at the top there is a table:

And the part of QT += widgets that indicates that it belongs to the QtWidgets sub-module is observed.

But the second case is somewhat more complicated since it involves looking for an equivalent that may or may not be in the same class, in this case it happens with the QPixmap.grabWindow() method which is deprecates (see here for more information). After doing a search you can replace that code with QApplication.primaryScreen().grabWindow(0).

Considering all of the above, the translation is:

import sys
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtGui import QColor, QFont, QPainter, QPixmap, QTextOption, QScreen
from PyQt5.QtWidgets import QApplication, QToolTip, QWidget


class AreaSelector(QWidget):
    def __init__(self, parent=None):

        QWidget.__init__(self, None, Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setWindowState(Qt.WindowFullScreen)
        self.setAutoFillBackground(False)

        self.parent = parent
        self.start_x = 0
        self.start_y = 0
        self.end_x = 0
        self.end_y = 0
        self.current_x = 0
        self.current_y = 0

    def showEvent(self, event):
        self.bg = QApplication.primaryScreen().grabWindow(0)
        self.screen_geometry = QApplication.primaryScreen().geometry()

    def mousePressEvent(self, event):

        self.start_x = event.globalX()
        self.start_y = event.globalY()

    def mouseReleaseEvent(self, event):

        self.end_x = event.globalX()
        self.end_y = event.globalY()

    def mouseMoveEvent(self, event):

        self.current_x = event.globalX()
        self.current_y = event.globalY()
        self.repaint()

        text = "Start: %sx%s \nEnd: %sx%s" % (
            self.start_x,
            self.start_y,
            self.current_x,
            self.current_y,
        )
        QToolTip.showText(event.pos(), text)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Return:
            self._acceptSelection()
        elif event.key() == Qt.Key_Escape:
            self.close()

    def _acceptSelection(self):

        if self.parent is not None:
            self.parent.areaSelectEvent(
                self.start_x, self.start_y, self.end_x, self.end_y
            )
        self.close()

    def paintEvent(self, event):

        painter = QPainter()
        painter.begin(self)

        painter.fillRect(self.screen_geometry, QColor(10, 10, 10, 125))

        self._paint_selection(painter)
        self._paint_usage_text(painter)
        painter.end()

    def _paint_usage_text(self, painter):

        font = QFont("Helvetica [Cronyx]", 26, QFont.Bold)
        painter.setFont(font)
        painter.setPen(QColor(255, 255, 255, 255))

        screen_width = self.screen_geometry.width()
        text_width = 800
        text_start_x = screen_width / 2 - text_width / 2

        screen_height = self.screen_geometry.height()
        text_height = 200
        text_start_y = screen_height / 2 - text_height / 2

        textoption = QTextOption(Qt.AlignCenter)
        textbox = QRectF(text_start_x, text_start_y, text_width, text_height)
        painter.drawText(
            textbox,
            "Click & Drag to select an area\n" "ENTER to confirm or ESC to cancel",
            textoption,
        )
        painter.drawRoundedRect(textbox, 20, 20)

    def _paint_selection(self, painter):
        """Draws the current user selection"""
        rectangle = QRectF()

        if self.start_x > self.current_x:
            rectangle.setLeft(self.current_x)
            rectangle.setRight(self.start_x)

        else:
            rectangle.setLeft(self.start_x)
            rectangle.setRight(self.current_x)

        if self.start_y > self.current_y:
            rectangle.setTop(self.current_y)
            rectangle.setBottom(self.start_y)

        else:
            rectangle.setTop(self.start_y)
            rectangle.setBottom(self.current_y)

        painter.drawPixmap(rectangle, self.bg, rectangle)
        painter.drawRect(rectangle)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = AreaSelector()
    main.show()
    sys.exit(app.exec_())

这篇关于如何在python中从pyqt4升级到pyqt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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