简体   繁体   English

PyQt4插槽系统过渡到PyQt5

[英]PyQt4 slot system transition to PyQt5

I followed some tutorial online to learn about threading with PyQt but the example uses PyQt4 and I'm using PyQt5. 我在网上遵循了一些教程,以学习有关使用PyQt进行线程化的操作,但是该示例使用PyQt4,而我使用的是PyQt5。 I read this link to get some information: http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html . 我阅读了此链接以获取一些信息: http : //pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html

But I still don't understand how I should change my example below for it to work with PyQt5. 但是我仍然不明白如何更改下面的示例才能使其与PyQt5一起使用。

What this example is supposed to do is show the cpu percentage on a progress bar, and the reason why I'm using a thread is so the progress bar value will change when the cpu load changes. 此示例应该做的是在进度条上显示cpu百分比,而我使用线程的原因是,这样,当cpu负载更改时,进度条值也将更改。

So FYI the code below is code that works with PyQt4 but not with version 5, it would be helpful if somebody could show me the right way to do it with PyQt5. 因此,仅供参考,下面的代码是适用于PyQt4的代码,但不适用于版本5,如果有人向我展示使用PyQt5的正确方法,这将很有帮助。

My example code: 我的示例代码:

import sys
import os
import sysInfo
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QWidget, QApplication

from test import Ui_Form

class MainUiClass(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(MainUiClass, self).__init__(parent)
        self.setupUi(self)
        self.threadclass = ThreadClass()
        self.threadclass.start()
        self.connect(self.threadclass, SIGNAL('CPU_VALUE'), self.updateProgressBar)

    def updateProgressBar(self, val):
        self.progressBar.setValue(val)

class ThreadClass(QThread):
    def __init__(self, parent=None):
        super(ThreadClass, self).__init__(parent)

    def run(self):
        while 1:
            val = sysInfo.getCPU()  # get cpu load
            self.emit(SIGNAL('CPU_VALUE'), val)

if __name__ == '__main__':
    app = QApplication([])
    window = MainUiClass()
    window.show()
    sys.exit(app.exec_())

The way to declare a signal is changed from pyqt4 to pyqt5: 声明信号的方式从pyqt4更改为pyqt5:

class MainUiClass(QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(MainUiClass, self).__init__(parent)
        self.setupUi(self)
        self.threadclass = ThreadClass()
        self.threadclass.cpuValueChanged.connect(self.updateProgressBar)
        self.threadclass.start()


    def updateProgressBar(self, val):
        self.progressBar.setValue(val)

class ThreadClass(QThread):
    cpuValueChanged = pyqtSignal(int)
    def __init__(self, parent=None):
        super(ThreadClass, self).__init__(parent)

    def run(self):
        while 1:
            val = sysInfo.getCPU()  # get cpu load
            self.cpuValueChanged.emit(val)

if __name__ == '__main__':
    app = QApplication([])
    window = MainUiClass()
    window.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM