将线程从QT3移植到QT4

时间:2009-12-29 18:30:59

标签: qt qt4

我正在将应用程序从QT3移植到QT4,并在线程更新QProgressDialog时继续遇到问题。原始代码大致设计如下:

class ScanProcess : QObject{
Q_OBJECT
public:
  QProgressDialog* progress;
private:
  ScanProcessThread* thread;
};

class ScanProcessThread : QThread {
Q_OBJECT
public:
  void run();
};

这是在运行qt3to4并从QT移植指南进行适当更改之后。

在原始设计中,在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()失败。有什么想法吗?

(qt4.4,windows xp sp3,Visual Studio 2008 /)

1 个答案:

答案 0 :(得分:0)

由于只有主线程可以访问GUI,因此您必须确保在主线程中处理ScanProcess::customEvent()例程。通常最简单的方法是确保发出或处理信号的任何线程都有own event loop