11.1.5 线程与主线程UI界面交互(友元类)
1. widget.cpp 文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
thread_flag=0; //默认线程停止
thread.show_ui=this; //给值
}
Widget::~Widget()
{
delete ui;
}
//开始执行线程
void Widget::on_pushButton_startthread_clicked()
{
thread_flag=!thread_flag;
if(thread_flag)
{
thread.count=0; //清空计数器
thread.start(); //开始执行线程
ui->pushButton_startthread->setText("停止执行线程");
}
else
{
thread.terminate();//终止线程的执行。线程可能会或可能不会立即被终止,这取决于操作系统的调度策略。终止后需要调用QThread::wait()()。
thread.wait();
ui->pushButton_startthread->setText("开始执行线程");
}
}
//线程执行起始点
void Thread::run()
{
/* 线程的相关代码 */
while