天下

记录修行的印记

qt对话框+线程小试

//thread.h
#pragma once
#define QT3_SUPPORT
#include 
<iostream>
#include 
<cstdio>
#include 
<unistd.h>
#include 
<Qt/qapplication.h>
#include 
<Qt/qpushbutton.h>
#include 
<qmessagebox.h> 
#include 
<Qt/qdialog.h>
#include 
<Qt/qthread.h>
#include 
<QString>


template
<typename T>
class Thread : public QThread
{
public:
    Thread();
    
~Thread();
    
void run();
    
void setTObj(T* pobj);
public:
    
int flag;
    unsigned 
int pos;
    T
* pTObj;
};


template
<typename T>
Thread
<T>::Thread()
{
    flag 
= 1;
    pos 
=0;
}

template
<typename T>
Thread
<T>::~Thread()
{

}

template
<typename T>
void Thread<T>::run()
{
    
//QMessageBox::information( NULL, "Application name","Started.\n");
    QString strText;
    
while(flag)
    {
        strText.sprintf(
"flag:%d",pos);
        pTObj
->setWindowTitle(strText);
        pos
++;
        QThread::msleep(
500);
    }
}

template
<typename T>
void Thread<T>::setTObj(T* pobj)
{
    pTObj 
= pobj;
}

//ProcessDialog.h
#pragma once
#define QT3_SUPPORT

#include 
<iostream>
#include 
<cstdio>
#include 
<unistd.h>
#include 
<Qt/qapplication.h>
#include 
<Qt/qpushbutton.h>
#include 
<qmessagebox.h> 
#include 
<Qt/qdialog.h>
#include 
<Qt/qthread.h>
#include 
<QString>
#include 
"Thread.h"

class ProcessDialog :public QDialog 
{
    Q_OBJECT
public:
    ProcessDialog(QWidget
* parent=0)
        :btnStop(
"Stop",this)
        ,btnWork(
"Work",this)
    {
        btnStop.setGeometry( 
62407530 );
        btnWork.setGeometry( 
162,1407530 );

        threadA.setTObj(
this);

        connect(
&btnWork,SIGNAL( clicked()),this,SLOT(start()) );
        connect(
&btnStop,SIGNAL( clicked()),this,SLOT(stop()) );
    }

public:
    QPushButton btnStop;
    QPushButton btnWork;
    
private slots:
        
void start()
        {
            btnWork.setText(
"Working");
            threadA.start();
            threadA.flag 
= 1;
        }
        
void stop()
        {
            btnWork.setText(
"Work");
            threadA.flag 
= 0;
            
//threadA.pos  = 0;
        }

private:
    Thread
<ProcessDialog> threadA;    
};


//main.cpp
#define QT3_SUPPORT
#include 
<iostream>
#include 
<cstdio>
#include 
<unistd.h>
#include 
<Qt/qapplication.h>
#include 
<Qt/qpushbutton.h>
#include 
<qmessagebox.h> 
#include 
<Qt/qdialog.h>
#include 
<Qt/qthread.h>
#include 
<QString>
#include 
"Thread.h"
#include 
"ProcessDialog.h"

int main( int argc, char **argv )
{
    QApplication app( argc, argv );
    ProcessDialog dlg;
    app.setMainWidget( 
&dlg );
    dlg.resize(
640,480);
    dlg.setModal(
true);
    dlg.exec();
    
return app.exec();
}

posted on 2011-09-22 10:24 天下 阅读(1064) 评论(0)  编辑 收藏 引用 所属分类: Linux编程QT


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论