我有一个简单的应用程序,在主视图中我有QListview。我的流程是这样的,我需要知道它是否正确
1.App启动并启动单线程,还可以看到线程对象与主应用程序之间的信号/插槽连接。
2.线程以xml格式从远程服务器获取数据,并将数据设置为对象容器(表示数据的类)。
3.当数据在对象中准备就绪时,它触发信号返回主应用程序(信号/插槽来自第1节)。
4.信号调用更新函数,该函数通过其模型(QAbstractListModel)将格式化数据设置为QAbstractListModel。
问题是,当第4阶段发生时,我在应用程序中看到了2-3秒,这让我想知道这里到底出了什么问题。
更新:
在使用困倦对应用程序进行分析之后,它看起来像是应用程序中的延迟,我不确定,但是在专用列中显示的是非常高的322.35s。在调用run方法中的http请求的线程中,我有这样的代码,它引导线程暂停。
void RequestThread::run()
{
m_RequestThreadTimer = new QTimer();
connect(m_RequestThreadTimer, SIGNAL(timeout()),
this,SLOT(fire(),Qt::DirectConnection));
QVariant val(GetValFromConfig());
int interval = val.toInt();
m_RequestThreadTimer->setInterval(interval);
m_RequestThreadTimer->start();
QThread::exec();
}但现在的问题是如何改进它呢?
发布于 2011-12-29 10:23:30
我怀疑,由于您在QThread::run()方法中创建了计时器,所以定时器连接的时隙是在主线程的上下文中调用的。
您不需要子类QThread来在它自己的线程中运行代码。
只需子类一个QObject,添加所需的功能,创建一个QThread实例,启动它并使用QObject::moveToThread()方法将QObject的线程关联设置为新线程。
worker = new WorkerClass;
connect(worker,SIGNAL(response(QString)),this,SLOT(response(QString)));
QThread *t = new QThread;
t->start();
worker->moveToThread(t);
//Start it either like this or by emitting a signal connected to the startWorking slot
QMetaObject::invokeMethod(worker,"startWorking",Qt::QueuedConnection);发布于 2016-03-24 14:50:33
我建议您在线程的情况下使用QEventloop。
在主目录中启动事件循环
//start the function to get data from remote server
GetData::getInstance()->StratReading();
QEventLoop loop; //loop to continue the reading.
loop.connect(GetData::getInstance(),SIGNAL(ReadingFinished()),SLOT(quit()));
loop.exec();
GetData::StratReading()
{
//sets the data into object container
//the data is ready in the object it trigger SIGNAL to main function to update to Ui
emit ReadingFinished(); //this will quit the loop
}https://stackoverflow.com/questions/8664626
复制相似问题