Multithreaded Update of the QStandardItemModel Data

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Multithreaded Update of the QStandardItemModel Data

Christian Schulze

Hello,

 

 

I’m trying to update the QStandardItemModel data from another thread. This causes several issues:

1.       The error message:
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().)

2.       Not all changes are applied. Namely the number of items in the TreeView changes, e.g. if I resize the window.

3.       Often the python.exe crashes on windows

It would be great if someone could tell me how to fix this!

 

 

 

Attached you find a simple (complete) example that illustrates the issues. Tested with Anaconda Python 2/3 and PyQt5.

 

The first issue seems to occur if a QStandardItem from the item model is modified from the second thread (:45). Adding new items seems to work fine.

 

The 2. issue suggests that I didn’t delete some item. But how could I check if removing the row was successful?

 

Maybe the 3. issue could be related to some event buffer overflow?

 

 

Best regards,

Christian


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

MultithreadedItemModelUpdate.py (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Multithreaded Update of the QStandardItemModel Data

Florian Bruhin
Hi,

On Mon, Mar 12, 2018 at 08:59:11PM +0000, Christian Schulze wrote:
> I'm trying to update the QStandardItemModel data from another thread. This causes several issues:
>
> [...]

QStandardItemModel is part of QtGui, and stuff in QtGui is not
threadsafe and must be used from the main (GUI) thread only.

> It would be great if someone could tell me how to fix this!

Your only option is pretty much "don't do this". Why do you need to
modify a QStandardItemModel from a non-main thread?

Florian

--
https://www.qutebrowser.org | [hidden email] (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Multithreaded Update of the QStandardItemModel Data

Christian Schulze

Hi Florian,

 

I need to update many items continuously. Several rows are updated at least once per second. For some use cases I have 20000 rows in total. I prepared all the data (new/modified/removed items), so that hardly any calculation is done in the main thread.

 

However, the main thead seems to be blocked for too long. The GUI seems to freeze for a short moment when I update the data. This is really annoying when you’re working with the application and I’d like to fix that.

 

I had expected that I’m allowed to modify the item model, since the GUI thread only reads data. I don’t care if an intermediate state is displayed.

 

So I need to implement my own threadsafe item model? Is any python based item model implementation threadsafe due to the GIL? Is the interaction of python and C++ fast enough to do this?

 

Thanks,

Christian

 

 

Von: Florian Bruhin [mailto:[hidden email]]
Gesendet: Montag, 12. März 2018 22:13
An: Christian Schulze <[hidden email]>
Cc: [hidden email]
Betreff: Re: [PyQt] Multithreaded Update of the QStandardItemModel Data

 

Hi,

On Mon, Mar 12, 2018 at 08:59:11PM +0000, Christian Schulze wrote:
> I'm trying to update the QStandardItemModel data from another thread. This causes several issues:
>
> [...]

QStandardItemModel is part of QtGui, and stuff in QtGui is not
threadsafe and must be used from the main (GUI) thread only.

> It would be great if someone could tell me how to fix this!

Your only option is pretty much "don't do this". Why do you need to
modify a QStandardItemModel from a non-main thread?

Florian

--
https://www.qutebrowser.org | [hidden email] (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Multithreaded Update of the QStandardItemModel Data

Barry Scott
The trick you need is that you can send a signal from a non-main thread to the gui main-thread.

In response to the signal you update the model. Put all the information that you need as the signal arg or args.

I use this idea in scm-workbench to sync the ui to background changes.

Barry


On 12 Mar 2018, at 21:52, Christian Schulze <[hidden email]> wrote:

Hi Florian,

 

I need to update many items continuously. Several rows are updated at least once per second. For some use cases I have 20000 rows in total. I prepared all the data (new/modified/removed items), so that hardly any calculation is done in the main thread.

 

However, the main thead seems to be blocked for too long. The GUI seems to freeze for a short moment when I update the data. This is really annoying when you’re working with the application and I’d like to fix that.

 

I had expected that I’m allowed to modify the item model, since the GUI thread only reads data. I don’t care if an intermediate state is displayed.

 

So I need to implement my own threadsafe item model? Is any python based item model implementation threadsafe due to the GIL? Is the interaction of python and C++ fast enough to do this?

 

Thanks,

Christian

 

 

Von: Florian Bruhin [[hidden email]]
Gesendet: Montag, 12. März 2018 22:13
An: Christian Schulze <[hidden email]>
Cc: [hidden email]
Betreff: Re: [PyQt] Multithreaded Update of the QStandardItemModel Data

 

Hi,

On Mon, Mar 12, 2018 at 08:59:11PM +0000, Christian Schulze wrote:
> I'm trying to update the QStandardItemModel data from another thread. This causes several issues:
>
> [...]

QStandardItemModel is part of QtGui, and stuff in QtGui is not
threadsafe and must be used from the main (GUI) thread only.

> It would be great if someone could tell me how to fix this!

Your only option is pretty much "don't do this". Why do you need to
modify a QStandardItemModel from a non-main thread?

Florian

--
https://www.qutebrowser.org | [hidden email] (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Multithreaded Update of the QStandardItemModel Data

Christian Schulze

Great! The issues are fixed! Thanks!

 

The updated example is attached. I’ll try implement it in my code.

 

Christian

 

 

Von: Barry [mailto:[hidden email]]
Gesendet: Montag, 12. März 2018 23:03
An: Christian Schulze <c.schulze@tlk-therm
o.com>
Cc: Florian Bruhin <[hidden email]>; [hidden email]
Betreff: Re: [PyQt] Multithreaded Update of the QStandardItemModel Data

 

The trick you need is that you can send a signal from a non-main thread to the gui main-thread.

 

In response to the signal you update the model. Put all the information that you need as the signal arg or args.

 

I use this idea in scm-workbench to sync the ui to background changes.

 

Barry

 


On 12 Mar 2018, at 21:52, Christian Schulze <[hidden email]> wrote:

Hi Florian,

 

I need to update many items continuously. Several rows are updated at least once per second. For some use cases I have 20000 rows in total. I prepared all the data (new/modified/removed items), so that hardly any calculation is done in the main thread.

 

However, the main thead seems to be blocked for too long. The GUI seems to freeze for a short moment when I update the data. This is really annoying when you’re working with the application and I’d like to fix that.

 

I had expected that I’m allowed to modify the item model, since the GUI thread only reads data. I don’t care if an intermediate state is displayed.

 

So I need to implement my own threadsafe item model? Is any python based item model implementation threadsafe due to the GIL? Is the interaction of python and C++ fast enough to do this?

 

Thanks,

Christian

 

 

Von: Florian Bruhin [[hidden email]]
Gesendet: Montag, 12. März 2018 22:13
An: Christian Schulze <[hidden email]>
Cc: [hidden email]
Betreff: Re: [PyQt] Multithreaded Update of the QStandardItemModel Data

 

Hi,

On Mon, Mar 12, 2018 at 08:59:11PM +0000, Christian Schulze wrote:
> I'm trying to update the QStandardItemModel data from another thread. This causes several issues:
>
> [...]

QStandardItemModel is part of QtGui, and stuff in QtGui is not
threadsafe and must be used from the main (GUI) thread only.

> It would be great if someone could tell me how to fix this!

Your only option is pretty much "don't do this". Why do you need to
modify a QStandardItemModel from a non-main thread?

Florian

--
https://www.qutebrowser.org | [hidden email] (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
         I love long mails! | https://email.is-not-s.ms/

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt


_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

MultithreadedItemModelUpdate2.py (2K) Download Attachment