九、另一种创建线程的方式
面向对象程序设计实践的早期,工程中习惯于通过继承的方式扩展系统的功能。
现代软件架构技术,参考标准,一是尽量使用组合的方式实现系统功能,二是代码中仅体现需求中的继承关系。
通过继承的方式实现新的线程类有什么实际意义?
线程的各个子类仅保护的void run()函数不同,接口部分完全相同。
结论:
通过继承的方式实现多线程没有任何实际意义,QThread对应于操作系统中的线程,QThread类用于充当一个线程操作的集合。所以应该提供灵活的方式指定线程的入口参数(槽函数),尽量避免重写void run()。
灵活指定一个线程对象的线程入口函数——信号与槽
1、在类中定义一个槽函数void tmain()作为线程入口函数;
2、在类中定义一个QThread成员对象m_thread;
3、改变当前对象的线程依附性到m_thread;
4、连接m_thread的start()信号到tmain();
AnotherThread::AnotherThread(QObject *parent) : QObject(parent)
{
moveToThread(&m_thread); //改变当前对象的线程依附性
connect(&m_thread, SIGNAL(started()), this, SLOT(tmain()));
}
void AnotherThread::tmain()
{
qDebug() << "void AnotherThread::tmain() tid = " << QThread::currentThreadId();
for(int i=0; i<10; i++)
{
qDebug() << "void AnotherThread::tmain() i = " << i;
}
qDebug() << "void AnotherThread::tmain() end";
}
void AnotherThread::start()
{
qDebug() << " void AnotherThread::start() ";
m_thread.start();
}
void AnotherThread::terminate()
{
m_thread.terminate();
}
void AnotherThread::exit()
{
m_thread.exit();
}
AnotherThread::~AnotherThread()
{
m_thread.wait();
}
void test()
{
AnotherThread at;
at.start();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << " main() tit" << QThread::currentThreadId();
test();
return a.exec();
}
运行效果如下:
意义:将QThread作为一个线程的操作集,通过信号与槽的方式指定线程入口函数,避免重新run()函数。
十、多线程与界面组件的通信
是否可以在子线程中创建界面组件?
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
TestThread::TestThread(QObject *parent) : QThread(parent)
{
}
void TestThread::run()
{
/* It Is ERROR to create GUI elements in SUB THREAD */
QWidget w;
w.show();
exec();
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
TestThread* ptt = new TestThread();
ptt->start(); }
上面代码视图在子线程中创建一个窗口,运行上面代码,程序崩溃,提示信息为:
ASSERT failure in QWidget: "Widgets must be created in the GUI thread.", file kernel/qwidget.cpp, line 1301
为什么呢?
GUI系统设计原则:所有界面组件的操作只能在主线程中完成,因此,主线程也叫作UI线程。
那么子线程如何对界面组件进行更新呢?——信号与槽
1、在子线程类中定义及诶满足建的更新信号(updateUI);
2、在主窗口类中定义更新界面组件的槽函数(setInfo);
3、使用异步方式连接更新信号到槽函数(updateUI --> setInfo)
子线程通过发射信号的方式更新界面组件,而所有的界面组件对象只能依附于主线程。
示例代码:
QApplication a(argc, argv);
Widget w;
w.show();
/******************************************/
UpdateThread m_thread;
QPlainTextEdit textEdit;Widget::Widget(QWidget *parent) : QWidget(parent){ //TestThread* ptt = new TestThread(); //ptt->start(); textEdit.setParent(this); textEdit.move(20, 20); textEdit.resize(200, 150); textEdit.setReadOnly(true); connect(&m_thread, SIGNAL(updateUI(QString)), this, SLOT(appendText(QString))); m_thread.start();}void Widget::appendText(QString text){ textEdit.appendPlainText(text);}/******************************************/signals: void updateUI(QString text);void UpdateThread::run(){ emit updateUI("Begin"); for(int i=0; i<10; i++) { emit updateUI(QString::number(i)); sleep(1); } emit updateUI("End");}
重点:界面组件的对象必须依附于主线程,进行信号与槽的连接必须异步连接(同步调用在槽函数返回时间短,信号等待时间长的情况下也可以,但不推荐)的方式。
Widget::Widget(QWidget *parent) : QWidget(parent){ //TestThread* ptt = new TestThread(); //ptt->start(); textEdit.setParent(this); textEdit.move(20, 20); textEdit.resize(200, 150); textEdit.setReadOnly(true); connect(&m_thread, SIGNAL(updateUI(QString)), this, SLOT(appendText(QString))); m_thread.start();}void Widget::appendText(QString text){ textEdit.appendPlainText(text);}/******************************************/signals: void updateUI(QString text);void UpdateThread::run(){ emit updateUI("Begin"); for(int i=0; i<10; i++) { emit updateUI(QString::number(i)); sleep(1); } emit updateUI("End");}
子线程能够改变界面组件状态的本质是什么?
子线程发射信号通知主线程界面请求更新,主线程根据具体信号以及信号参数对界面组件进行修改。
是否有其他间接的方式,可以让子线程更新界面组件的状态?
解决方案——发送自定义事件
1、用自定义事件类用于描述界面更新细节;
2、在主窗口类中重写事件处理函数event();
3、使用postEvent()函数(异步方式)发送自定义事件类对象:
子线程指定消息的对象为主窗口对象,在event事件处理函数中更新界面状态。
代码示例如下:
QApplication a(argc, argv);
Widget w;
w.show();
/**************************************************/
UpdateThread m_thread;
QPlainTextEdit textEdit;
bool event(QEvent *);//重写event()函数
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
textEdit.setParent(this);
textEdit.move(20, 20);
textEdit.resize(200, 150);
textEdit.setReadOnly(true);
m_thread.setParent(this); //设置父组件,即此类对象包含父组件信息
m_thread.start();
}
bool Widget::event(QEvent *evt)
{
bool ret = true;
if( evt->type() == StringEvent::TYPE )
{
StringEvent* se = dynamic_cast<StringEvent*>(evt);
if( se != NULL )
{
textEdit.appendPlainText(se->data());
}
}
else
{
ret = QWidget::event(evt);
}
return ret;
}
/**************************************************/
QString m_data; //描述界面组件的状态变化
public:
const static Type TYPE = static_cast<Type>(QEvent::User + 0xFF);
explicit StringEvent(QString data);
QString data();
StringEvent::StringEvent(QString data) : QEvent(TYPE)
{
m_data = data;
}
QString StringEvent::data()
{
return m_data;
}
/**************************************************/
void UpdateThread::run()
{
//emit updateUI("Begin");
QApplication::postEvent(parent(), new StringEvent("Begin")); //知道了父组件,向父组件发送一个事件
for(int i=0; i<10; i++)
{
QApplication::postEvent(parent(), new StringEvent(QString::number(i)));
sleep(1);
}
QApplication::postEvent(parent(), new StringEvent("end"));
}
重点:通过重写event()函数,将
QApplication::postEvent(parent(), new StringEvent("Begin"));
函数中发送的信息进行类型对比后,输出至界面显示。
注意:子线程创建时,必须附带目标对象的地址信息,如postEvent()中的parent()函数,得到当前线程类对象的父组件——主窗口。发送的事件对象必须在堆上创建。