我将应用程序从qt3移植到qt4,并在线程更新qprogressdialog时不断遇到问题。最初的代码大致设计如下:
class ScanProcess : QObject{
Q_OBJECT
public:
QProgressDialog* progress;
private:
ScanProcessThread* thread;
};
class ScanProcessThread : QThread {
Q_OBJECT
public:
void run();
};
这是在运行qt3到4并从qt porting guide进行适当更改之后进行的。
在原始设计中,在scanprocessthread内:
void ScanProcessThread::run(){
//...
ProgressInfo *prog = new ProgressInfo(); //then fill it in
QCustomEvent* progEvent = new QCustomEvent(QEvent::User+1, (void*)prog);
QCoreApplication::postEvent(parent, progEvent);//Parent is pointer to the ScanProcess
//...
}
void ScanProcess::customEvent(QCustomEvent *e){
if(e->type() == QEvent::User+1){
//update QProgressDialog
progres->setValue(prog.index);//Value from the ProgressInfo passed above
//This line crashes deep in ntdll, but I have traced it to the QProgressDialog::repaint()
}
}
struct ProgressInfo {int count; int index; QString text;};
所以,这就是重要的代码。对qprogressdialog::repaint的调用似乎总是在ntdll的某个深度崩溃。我尝试过其他两种方法:
1)使用qcoreapplication::PostEvent()。
2)传递qprogressdialog*并让scanprocessthread直接更新它。在任何情况下,都是qprogressDialog::repaint()失败。有什么想法吗?
(第4.4季度,Windows XP SP3,Visual Studio 2008/)