问题描述
- QT中QListWidget窗口中条目更新,如何让它动态显示出来
-
我做的是一个公交车报站系统,在点击完模拟按钮后,会模拟走完整条路线,但是QListWidget不会动态更新,其中的数据处理部分还是会进行
附上模拟按钮的代码
/* simulate */
void MyDialog::on_pushButton_3_clicked()
{
while (1)
{
// 先清空显示
ui->listWidget->clear();QFile file("bus_.xml"); if (!file.open(QIODevice::ReadOnly)) return ; QDomDocument doc; if (!doc.setContent(&file)) { file.close(); return ; } file.close(); QDomElement docElem = doc.documentElement(); QDomElement c; QString curstation; int flag=0, count=0; QDomNode n = docElem.firstChild(); while(!n.isNull()) { if (n.isElement()) { QDomElement e = n.toElement(); QDomNodeList list = e.childNodes(); if (flag == 1) { for(int i=0; i<list.count(); i++) { QDomNode node = list.at(i); if(node.isElement()) { if (i == number) { qDebug()<<number; //ui->listWidget->addItem(new QListWidgetItem(QIcon("yuan.jpg"), node.toElement().text())); ui->listWidget->setCurrentItem(new QListWidgetItem(QIcon("yuan.jpg"), node.toElement().text())); curstation = node.toElement().text(); qDebug()<<"next station"<<curstation; } else{ //ui->listWidget->addItem(" " + node.toElement().text()); ui->listWidget->setCurrentItem (new QListWidgetItem(QIcon("yuan.jpg"), node.toElement().text())); } } } } else { QDomNode node_count = list.at(0); count = node_count.toElement().text().toInt(); //qDebug()<<count; } flag++; } n = n.nextSibling(); } //char command[256] = {0}; QString command; command = _config_2() + curstation ; qDebug()<<command; /* yuying */ tts_open_test("/dev/ttyUSB0", (char *)command.toStdString().c_str()); /* net_demo_string */ /* string shuld be utf-8 */ net_demo_string((char *)command.toStdString().c_str()); qDebug()<<"net_demo_string"; /* maybe change */ QByteArray datagram = curstation.toStdString().c_str(); sender->writeDatagram (datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454); if (count == number) { return ; } ui->listWidget->update (); number++; sleep(1); }
}
时间: 2024-12-09 14:09:06