从 PyQt5 转换为 PyQt4

Convert from PyQt5 to PyQt4

各位兄弟姐妹,我就想知道有没有人能帮我把这个程序转成PyQt4。我尽力了,但我做不到 ;,)。它是用于在 UI.

上显示 实时文本 的基本程序

程序如下:

import sys
import time
import random
from PyQt5 import QtWidgets, QtCore
 
class WorkerThread(QtCore.QObject):
    signal = QtCore.pyqtSignal(int)
 
    def __init__(self):
        super().__init__()
 
    @QtCore.pyqtSlot()
    def run(self):
        while True:
            number = random.randint(1, 1000)
            self.signal.emit(number)
            time.sleep(1)
 
class WorkerLabel(QtWidgets.QLabel):
    def __init__(self, parent):
        super().__init__()
 
    @QtCore.pyqtSlot(int)
    def slot(self, i):
        self.setText(str(i))
 
class UserInterface(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__()
        self.label = WorkerLabel(self)
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)
 
class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = UserInterface(self)
        self.setCentralWidget(self.ui)
 
        self.worker = WorkerThread()
        self.workerThread = QtCore.QThread()  # Move the Worker object to the Thread object
        self.workerThread.started.connect(self.worker.run)  # Init worker run() at startup
        self.worker.moveToThread(self.workerThread)
        self.worker.signal.connect(self.ui.label.slot)
        self.workerThread.start()
 
        self.show()
 
if __name__== '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())

我不是 PyQt 专家,但我用它来开发汽车、机器人、小型飞机的遥测技术。感谢 eyllanesc 它现在已经解决并且运行良好。问题是 PyQt4 在 PyQt5

中使用 QtGui 而不是 QtWidgets

示例代码如下:

import sys
import time
import random
from PyQt4 import QtGui, QtCore
 
class WorkerThread(QtCore.QObject):
    signal = QtCore.pyqtSignal(int)
 
    def __init__(self):
        super().__init__()
 
    @QtCore.pyqtSlot()
    def run(self):
        while True:
            number = random.randint(1, 1000)
            self.signal.emit(number)
            time.sleep(1)
 
class WorkerLabel(QtGui.QLabel):
    def __init__(self, parent):
        super().__init__()
 
    @QtCore.pyqtSlot(int)
    def slot(self, i):
        self.setText(str(i))
 
class UserInterface(QtGui.QWidget):
    def __init__(self, parent):
        super().__init__()
        self.label = WorkerLabel(self)
        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)
 
class Main(QtGui.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = UserInterface(self)
        self.setCentralWidget(self.ui)
 
        self.worker = WorkerThread()
        self.workerThread = QtCore.QThread()  # Move the Worker object to the Thread object
        self.workerThread.started.connect(self.worker.run)  # Init worker run() at startup
        self.worker.moveToThread(self.workerThread)
        self.worker.signal.connect(self.ui.label.slot)
        self.workerThread.start()
 
        self.show()
 
if __name__== '__main__':
    app = QtGui.QApplication([])
    gui = Main()
    sys.exit(app.exec_())