Search code examples
pythonpyqtpyqt5pyqt4

Convert from PyQt5 to PyQt4


Hey brothers and sisters, I just wondered if anyone could help me convert this program to PyQt4. I did my best however I couldn't ;,). It is a basic program for displaying Real Time text on the UI.

Here is the program:

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_())

Solution

  • I am not an expert in PyQt however I use it to develop a telemetry for Cars, Robots, Small Aircraft. Thanks to eyllanesc its solved now and working well. The problem was that PyQt4 uses QtGui instead of QtWidgets in PyQt5

    Here is the sample code:

    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_())