Qt线程学习 一个QThread例子

时间:2022-01-10 00:29:59

一个QThread例子 

//threadTest.h 
#ifndef THREADTEST_H 
#define THREADTEST_H

#include <qthread.h> 
#include "test.h"

class MyThread : public QThread 
{  
    protected : 
    virtual void run(); 
};

MyThread myThread;

void Test::newSlot() 

    myThread.start(); 
}

#endif

////////////////////////////////

//threadTest.cpp

#include <QDialog>

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 <qvariant.h> 
#include <qlineedit.h> 
#include <qpushbutton.h> 
#include <qtextedit.h> 
#include <qlayout.h> 
#include <qtooltip.h> 
#include <qwhatsthis.h> 
#include <qimage.h> 
#include <qpixmap.h> 
#include <QVBoxLayout>

/* 
*  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 <qvariant.h> 
#include <qwidget.h>

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 <qapplication.h> 
#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(); 
}

/////////////

调试运行

gdb *.exe

run step点击按钮就可以看到输出调试信息