更新/更改QListWidget项值 [英] update/change QListWidget item value

查看:665
本文介绍了更新/更改QListWidget项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含一些项目(3行)的Qlistwidget

总错误:: __

总警告:: __

总计Voilations :: __







我想知道如何在执行时更新每个项目的QListWidget线?我有QListWidget(ErrCount,WarCount,VCount)的每个项目的计数器变量。所有计数器最初都为零。



谢谢

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

推荐答案

如果更新应由另一个线程启动,你应该为此创建一个事件:



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

// 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);
};




// 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>





活动可以现在从另一个线程发送(需要包含定义 UpdateCountEvent 的标头和指向对话框小部件的指针):



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));





如果您只想更新单个值,创建三个事件并相应地更新 MyDialog :: customEvent 中的列表项。


这篇关于更新/更改QListWidget项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆