[Qt-interest] QAbstractItemView::scrollToBottom and multiple threads

Sean Harmer sean.harmer at maps-technology.com
Fri May 29 10:37:28 CEST 2009


On Friday 29 May 2009 08:48:20 Stefan Bisplinghoff wrote:
> Hi all!
>
> I use a QListWidget to display some log messages in my application.
> The items are added from another single thread.
>
> To keep track of the latest messages, I want to scroll down the list
> after each item insertion. So what I do in the worker thread is
>
> m_listWidget->addItem('Some Text');
> m_listWidget->scrollToBottom();
>
>
> This results in a very strange behaviour. If I add several items
> successively in a short time, all items are displayed correctly, but the
> GUI thread gets very busy (up to 100% CPU).  However, the GUI is still
> useable, so events are processed.
>
> Moreover, if I add the new list items too fast, the QListWidget throws
> an "index out of range" error, while redrawing the widget.
>
>
> Both errors don't happen, if I omit the scrollToBottom command.
> Are there known problems with the insertion of new items while the GUI
> thread handles the scrolling?
Yes! You can only perform GUI operations in the main thread in which 
QApplication lives. To get around your problem simply emit a signal with your 
message in the worker thread and connect this up to a slot on an object in 
your main thread that takes care of appending your log message to the widget 
and performs the scrolling.

HTH,

Sean




More information about the Qt-interest-old mailing list