问题描述
我创建了一个包含一些项目(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
推荐答案
// 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项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!