Home > database >  signal with QThread in PyQt
signal with QThread in PyQt

Time:11-04

I have a program that needs to measure the size of the user profile in windows.

I made a QObject class to measure the size:

class TailleRep(QObject):
    size_ready = pyqtSignal(int)

    def __init__(self, repertoire):
        super(TailleRep, self).__init__()
        self.repertoire = repertoire

    def work(self):
        size = self.getFolderSize(self.repertoire)
        print(size)
        self.size_ready.emit(size)

    def getFolderSize(self, folder_path):
        start_path = Path(os.path.join(folder_path, '.'))
        return sum(f.stat().st_size for f in start_path.glob('**/*') if f.is_file())

The size result printed in console is correct, but when I use this in the ui It's not the same:

import sys
import os

from PyQt5.QtCore import Qt, QObject, pyqtSignal, QThread
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, \
    QPushButton, QVBoxLayout, QWidget
from pathlib import Path

class Window(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi()

    def setupUi(self):
        self.resize(300, 80)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.sizeLabel = QLabel("Size: ---", self)
        self.sizeLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.getSizeBtn = QPushButton("Get size", self)
        self.getSizeBtn.clicked.connect(self.GetSize)
        layout = QVBoxLayout()
        layout.addWidget(self.sizeLabel)
        layout.addWidget(self.getSizeBtn)
        layout.addStretch()
        self.centralWidget.setLayout(layout)

    def GetSize(self):
        self.getSizeBtn.setEnabled(False)
        self.thread = QThread()
        self.worker = TailleRep(os.getenv('USERPROFILE'))
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.work)  
        self.worker.size_ready.connect(self.size_ready)
        self.thread.start()

    def size_ready(self, size):
        self.sizeLabel.setText("Size: {}".format(size))
        self.getSizeBtn.setEnabled(True)

app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())

Can you tell me where my mistake is?

CodePudding user response:

The problem seems to be a PyQt5 bug, which converts a python integer to a C integer, and the latter has a maximum of 2147483647 causing an overflow since the size is greater than that value. A possible solution is to create a python class that has an integer as a field so that the python object is passed and there is no conversion.

from dataclasses import dataclass


@dataclass
class Data:
    size: int
class TailleRep(QObject):
    size_ready = pyqtSignal(Data)

    # ...

    def work(self):
        ize = self.getFolderSize(self.repertoire)
        print(size)
        data = Data(size=size)
        self.size_ready.emit(data)
    def size_ready(self, data):
        size = data.size
        self.sizeLabel.setText("Size: {}".format(size))
        self.getSizeBtn.setEnabled(True)
  • Related