Qt线程学习 一个QThread例子

这篇博客通过一个QThread的例子展示了如何在Qt中创建和使用线程。在`threadTest.cpp`中,定义了一个`MyThread`类继承自`QThread`,并在`run()`方法中进行线程执行的业务逻辑。当在`Test`类的`newSlot()`槽函数中启动线程时,会打印一系列调试信息。同时,`Test`类是一个包含文本编辑器、输入框和两个按钮的简单界面,点击按钮可以触发线程的启动。
摘要由CSDN通过智能技术生成

一个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点击按钮就可以看到输出调试信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值