P
P
PaffosONE2021-09-23 04:40:06
PyQt
PaffosONE, 2021-09-23 04:40:06

How to correctly implement adding data to a PyQt5 table?

Now I'm writing a data scraper from websites, I've encountered a GUI hang problem.
I read that you need to do everything in a separate thread, first I tried the standard python threading library, but I ran into an error:

QObject::setParent: Cannot set parent, new parent is in a different thread

After reading a few topics, I came to the use of QThread, but there are problems, the worker class gives an error when trying to add an Item to the table.

The worker class itself:

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

    def run( self ):
        try:
            block_username = QFrame( self.parent )
            text = "SomeText"
            self.table.setRowCount( self.table.rowCount() + 1 )
            self.table.setCellWidget( 0, 0, block_username ) 
            self.table.setItem( 0, 1 QTableWidgetItem( text ) )
        except Exception as error:
            print( error )

Mistake:

QObject::setParent: Cannot set parent, new parent is in a different thread
QObject::connect: Cannot queue arguments of type 'QVector<int>'    
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QVector<int>'    
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QVector<int>'    
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QVector<int>'    
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QVector<int>'    
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)

How can I implement adding data to the table so that the GUI does not freeze?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2021-09-23
@SaNNy32

Use the signal/slot mechanism to communicate between program threads.

B
bbkmzzzz, 2021-09-23
@bbkmzzzz

Qt is designed in such a way that you can only pull interface methods from the thread in which this interface is running. That is the way.
When inheriting from QThread, you need to override the run method, it will be called when the thread starts, or not implement it, and then it will work on signals.
Where is the flow start?
self.worker.start()
There is another option.
Use moveToThread, then ALL SLOTs of a class inherited from QObject (need to be marked with a slot decorator) will be executed in a separate thread

self.worker = QThread()
self.worker.start()

self.some_class.moveToThread(self.worker)

And another option.
QThreadPool + QRunnable
PS
ALWAYS capitalize classes
All class attributes initialize in __init__

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question