2010-04-14 17 views
0

我在Qt的新有點...如何重繪另一個Qt的類

我有一個Qt的GUI應用程序(由我寫的),我們稱之爲QtAPP.exe 當QtAPP.exe運行,我將使用QThread和QProcess來執行一些外部文件,如player.exe(用原生C語言編寫)等 。

我的問題是: 在QtAPP.exe,有​​2個班, 1的QMainWindow - 核心QtAPP.exe的 2的QThread - 線程類執行外部事物

現在,如果我在QThread中獲得完成()信號, 我該如何強制QMainWindow重新繪製自己?

希望有人能告訴我一些提示,也許示例代碼:) 任何建議,歡迎〜

+0

這裏是主 INT主(INT的argc,字符* argv的[]){ 的QApplication一個(的argc,argv的); MainWindow w; w.show(); return a.exec(); } 的ManiWindow 空隙的MainWindow :: exeExt() { pT.setupExt(一些參數(一個或多個)); //初始QThread pT.start(); //開始的QThread } 的QThread的 無效Exthread ::設置(有些參數(S)){ // 東西做初始化。 } void Exthread :: run() { QProcess * extP; extP-> execute(「./ player.exe param(s)」); QObject :: connect(extP,SIGNAL(finished(int)),XXXX,SLOT(yyyy)); //仍然不知道如何 } – RRTW 2010-04-15 03:51:23

回答

1

一個解決辦法是簡單地完成的()信號連接到插槽中主窗口,其實現調用update() 。請注意,由於發送者和接收者對象位於不同的線程中,因此此信號的傳遞將是異步的。

這裏是一個工作示例:

的main.cpp

#include <QtGui/QApplication> 
#include "stuff.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    MainWindow w; 
    w.show(); 
    return app.exec(); 
} 

stuff.h

#ifndef STUFF_H 
#define STUFF_H 

#include <QtGui/QMainWindow> 
#include <QtCore/QThread> 

class QLabel; 

class Thread : public QThread 
{ 
    Q_OBJECT 
public: 
    Thread(QObject *parent); 
    void run(); 

private: 
    void startWork(); 

signals: 
    void workFinished(); 
}; 

class MainWindow : public QWidget 
{ 
    Q_OBJECT 
public: 
    MainWindow(); 

public slots: 
    void startWork(); 
    void workFinished(); 

private: 
    QLabel* m_label; 
    Thread* m_thread; 
}; 

#endif 

stuff.cpp

#include <QtCore/QTimer> 
#include <QtCore/QMutex> 
#include <QtCore/QWaitCondition> 
#include <QtGui/QVBoxLayout> 
#include <QtGui/QPushButton> 
#include <QtGui/QLabel> 
#include "stuff.h" 

#include <QDebug> 

// Global variables used for ITC 
QWaitCondition buttonPressed; 
QMutex mutex; 

Thread::Thread(QObject *parent) 
    : QThread(parent) 
{ 

} 

void Thread::run() 
{ 
    qDebug() << "Thread::run" << QThread::currentThreadId(); 
    while (1) { 
     mutex.lock(); 
     buttonPressed.wait(&mutex); 
     mutex.unlock(); 
     startWork(); 
    } 
} 

void Thread::startWork() 
{ 
    qDebug() << "Thread::startWork" << QThread::currentThreadId(); 
    // Simulate some long-running task 
    sleep(3); 
    // Emit a signal, which will be received in the main thread 
    emit workFinished(); 
} 


MainWindow::MainWindow() 
    : m_label(new QLabel(this)) 
    , m_thread(new Thread(this)) 
{ 
    QPushButton *button = new QPushButton("Start", this); 
    connect(button, SIGNAL(pressed()), this, SLOT(startWork())); 

    QVBoxLayout *layout = new QVBoxLayout(this); 
    layout->addWidget(button); 
    layout->addWidget(m_label); 
    setLayout(layout); 

    // Create connection across thread boundary 
    connect(m_thread, SIGNAL(workFinished()), this, SLOT(workFinished())); 

    m_thread->start(); 
} 

void MainWindow::startWork() 
{ 
    // Signal the thread to tell it that the button has been pressed 
    mutex.lock(); 
    m_label->setText("Started"); 
    buttonPressed.wakeAll(); 
    mutex.unlock(); 
} 

void MainWindow::workFinished() 
{ 
    qDebug() << "MainWindow::workFinished" << QThread::currentThreadId(); 
    m_label->setText("Finished"); 
} 
+0

請檢查我上面的示例代碼,由於不允許有太多的話在1條評論... 什麼是XXXX是,我如何實現MainWindow類? 或者我在想的方式完全不正確? 感謝您的建議,但我仍然不明白如何將信號連接到其他類中的插槽.. @@「 您可以提供一些關於此的更多提示嗎? – RRTW 2010-04-15 03:53:09

+0

謝謝,我確實捕捉完成()信號並傳遞給其他類,但未能重新繪製:( (這是另一個故事...) 還是謝謝你幫我解決問題:) – RRTW 2010-04-27 03:52:12