代码之家  ›  专栏  ›  技术社区  ›  David Souther

将螺纹从qt3移植到qt4

  •  0
  • David Souther  · 技术社区  · 16 年前

    我将应用程序从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/)

    1 回复  |  直到 16 年前
        1
  •  0
  •   Javier    16 年前

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