二級考試C++基礎(chǔ)QThread基礎(chǔ)實(shí)例

字號:

//threadTest.h
    #ifndef THREADTEST_H
    #define THREADTEST_H
    #include
    #include "test.h"
    class MyThread : public QThread
    {
    protected :
    virtual void run();
    };
    MyThread myThread;
    void Test::newSlot()
    {
    myThread.start();
    }
    #endif
    //threadTest.cpp
    #include
    void MyThread::run()
    {
    for(int i=0;i <100;i++)
    {
    printf("new value i is:%d",i);
    //QMessageBox::information(this,tr("info"),tr("load dll OK!"));
    //QMessageBox::information(this, tr("Empty Search "),"click Find.");
    //Test m;
    //m.show();
    qDebug("mhf");
    }
    /*
    QDialog *dialog = new QDialog(0,”popup”,FALSE);
    dialog->setCaption(“A QDialog Window”);
    dialog->setMinimumSize(200,80);
    dialog->setMaximumSize(200,80);
    dialog->show();
    */
    }
    //test.cpp
    #include "test.h"
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    /*
    * Constructs a Test as a child of ’parent’, with the
    * name ’name’ and widget flags set to ’f’.
    */
    Test::Test( QWidget* parent ): QWidget( parent=0 )
    {
    this->setWindowTitle("w");
    this->resize(250, 50);
    textEdit1 = new QTextEdit( "textEdit1" );
    textEdit1->setGeometry( QRect( 10, 60, 570, 291 ) );
    lineEdit1 = new QLineEdit( "lineEdit1" );
    lineEdit1->setGeometry( QRect( 10, 360, 571, 31 ) );
    pushButton2 = new QPushButton( "pushButton2" );
    pushButton2->setGeometry( QRect( 380, 400, 201, 41 ) );
    pushButton1 = new QPushButton( "pushButton1" );
    pushButton1->setGeometry( QRect( 10, 10, 261, 41 ) );
    //languageChange();
    resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
    // signals and slots connections
    connect( pushButton1, SIGNAL( clicked() ), this, SLOT( newSlot() ) );
    connect( pushButton2, SIGNAL( clicked() ), this, SLOT( languageChange() ) );
    //add
    QVBoxLayout *layout=new QVBoxLayout;
    layout->addWidget(textEdit1);
    layout->addWidget(lineEdit1);
    layout->addWidget(pushButton1);
    layout->addWidget(pushButton2);
    this->setLayout(layout);
    this->resize(350, 200);
    }
    /*
    * Destroys the object and frees any allocated resources
    */
    Test::~Test()
    {
    // no need to delete child widgets, Qt does it all for us
    }
    /*
    * Sets the strings of the subwidgets using the current
    * language.
    */
    void Test::languageChange()
    {
    this->setWindowTitle("m");
    //printf("new value i is");
    pushButton2->setText( tr( "pushButton2" ) );
    pushButton1->setText( tr( "pushButton1" ) );
    }
    //test.h
    #ifndef TEST_H
    #define TEST_H
    #include
    #include
    class QVBoxLayout;
    class QHBoxLayout;
    class QGridLayout;
    class QLineEdit;
    class QPushButton;
    class QTextEdit;
    class Test : public QWidget
    {
    Q_OBJECT
    public:
    Test( QWidget* parent = 0 );
    ~Test();
    QTextEdit* textEdit1;
    QLineEdit* lineEdit1;
    QPushButton* pushButton2;
    QPushButton* pushButton1;
    public slots:
    virtual void newSlot();
    protected:
    protected slots:
    virtual void languageChange();
    };
    #endif // TEST_H
    main.cpp
    #include
    #include "test.h"
    int main( int argc, char ** argv )
    {
    QApplication a( argc, argv );
    Test w;
    w.show();
    a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    return a.exec();
    }
    /////////////
    調(diào)試運(yùn)行
    gdb *.exe
    run step點(diǎn)擊按鈕就可以看到輸出調(diào)試信息。