以下是一个最基本的QT线程应用程序,就是让线程打印20次PING字符串。
我的问题是:怎么让QT线程可以访问hello类中的成员呢,比如说在线程里,给button设置一下按钮文字。如果用类成员函数就很好实现了,在构造的时候用button->setText(“退出”)就可以了。
////////////////////////////////////////////////////////////////////// hello.cpp ////////////////////////////////////////////////////////////////////////////////
#include "hello.h"
#include <qpushbutton.h>
hello::hello( QWidget* parent, const char* name, WFlags fl ) : helloBase( parent, name, fl )
{
button = new QPushbutton;
a.start();
connect(button, SIGNAL(clicked()), this, SLOT(goodBye()));
}
hello::~hello()
{
}
void hello::goodBye()
{
close();
}
void MyThreadA::run()
{
for(int count=0;count<20;count++)
{
sleep(1);
qDebug("Ping!");
}
}
////////////////////////////////////////////////////////////////////// hello.h ////////////////////////////////////////////////////////////////////////////////
#ifndef HELLO_H
#define HELLO_H
#include "hellobase.h"
#include <qthread.h>
#include <qpushbutton.h>
class MyThreadA : public QThread
{
public:
virtual void run();
};
class hello : public helloBase
{
Q_OBJECT
public:
MyThreadA a;
hello( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
QPushbutton *button;
~hello();
private slots:
void goodBye();
};
#endif // HELLO_H
////////////////////////////////////////////////////////////////////// mani.cpp ////////////////////////////////////////////////////////////////////////////////
#include "hello.h"
//#include <qpe/qpeapplication.h>
#include <qapplication.h>
hello *mw;
int main( int argc, char ** argv )
{
// QPEApplication a( argc, argv );
QApplication a( argc, argv );
mw = new hello;
a.setMainWidget(&mw);
mw.show();
return a.exec();
}