PDA

View Full Version : QListWidget refresh issue



summer_of_69
17th June 2009, 08:56
Hi All,

I am using QListWidget to show output ( e.g:- similar to output window you see in VS2005 IDE) .The problem I am facing is it's not updating accordingly,can anyone help me with this.
I want it to add one item each second and show the updated result , but it just shows whole list after 5 second.



void POCSleep::on_pushButton_clicked()
{
for(int i=0; i<5;i++)
{
ui.listWidget->update();
mySleep(1); /* delay of one second*/
displayMessage(QString("Number is: %1\n").arg( i+1));
}
}
void POCSleep::displayMessage(QString qmsg)
{
ui.listWidget->addItem(qmsg);
}


Thanks in advance.

Lykurg
17th June 2009, 09:08
Your sleep functions blocks the gui! Also that is not a good design. If you want it that way have a look at QCoreApplication::processEvents() and there is also an article on qt Quarterly about keepeing a gui app responsibly.

summer_of_69
17th June 2009, 09:28
Ok. May be I shall explain the whole scenerio.
I have one script (set of certain functions and commands) running at background ,It prints lot of information (till now I was using printf()) ,but now I want the same messages to be displayed in my DockWidget which contains QListWidget (for displaying messages).

What would be the appropriate way of doing this?

shentian
17th June 2009, 09:50
Is the script running in another process? If so, use QProcess to start it and read the messages from there. You can connect to QIODevice::readyRead to get notified when the process has written data to stdout.

Lykurg
17th June 2009, 09:58
What would be the appropriate way of doing this?
Use a worker thread for your "set of certain functions and commands" and use the signal slot mechanism to show the messages.

summer_of_69
17th June 2009, 10:38
void POCSleep::on_pushButton_clicked()
{
for(int i=0; i<5;i++)
{
ui.listWidget->update();
mySleep(1); /* delay of one second*/
displayMessage(QString("Number is: %1\n").arg( i+1));
}
}
void POCSleep::displayMessage(QString qmsg)
{
ui.listWidget->addItem(qmsg);
}



Thanks for your valuable reply,this will definitely work.

One thing I am unable to understand why worker thread is required for this?
Can it be done in simpler way?? I have one class which take care of all script related functionalities I just need to call functions using my Dialog and display related error messages or any messages(which can be done in console using printf()) in my Qlistwidget.

Lykurg
17th June 2009, 10:46
If you don't want that your GUI thread is blocked (this is the case when u use sleep) then you have to use your own treads or call processEvents().

In Your case you also can use a QTimerLine or use QtConcurrent, depends on what you really want achieve. Your posted code seems to me like a "first testing". If you really only want to print 5 messages with a 1 second delay, then use QTimerLine.