Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a Qlistwidget having some items(3 rows)
Total Errors:: __
Total Warnings:: __
Total Voilations:: __



I want to know how to update the QListWidget each item while executing the thread? I have counter variable for each item of the QListWidget(ErrCount, WarCount, VCount). All Counters are zero initially.

Thanks
Posted

1 solution

If the update should be initiated by another thread, you should create an event for that:

C++
// MyDialog.h
// This must be unique for the application.
#define UPDATE_COUNT_EV QEvent::User

class UpdateCountEvent : public QEvent
{
public:
    UpdateCountEvent(int err, int warn, int v) :
        QEvent((QEvent::Type)UPDATE_COUNT_EV),
        errCount(err),
        warnCount(warn),
        vCount(v)
    {
    }
    int errCount;
    int warnCount;
    int vCount;
};

class MyDialog : public QDialog
{
    // ...
    void customEvent(QEvent *event);
};

C++
// MyDialog.cpp
void MyDialog::customEvent(QEvent *event)
{
    if (static_cast<int>(event->type()) == UPDATE_COUNT_EV)
    {
        const UpdateCountEvent *ev = static_cast<const updatecountevent="">(event);
        ui->listWidget->item(0)->setText(QString("Total Errors: %1").arg(ev->errCount));
        ui->listWidget->item(1)->setText(QString("Total Warnings: %1").arg(ev->warnCount));
        ui->listWidget->item(2)->setText(QString("Total Violations: %1").arg(ev->vCount));
    }
}
</const></int>


The event can now be send from another thread (requires inclusion of the header that defines UpdateCountEvent and a pointer to the dialog widget):
QCoreApplication::postEvent(pDialogWidget, new UpdateCountEvent(ErrCount, WarnCount, VCount));


If you want to update only single values, create three events and update the list items in MyDialog::customEvent accordingly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900