11.1.5 线程与主线程UI界面交互(友元类)

举报
DS小龙哥 发表于 2021/12/09 23:53:24 2021/12/09
【摘要】 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-...

11.1.5 线程与主线程UI界面交互(友元类)

1. widget.cpp 文件


  
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent) :
  4. QWidget(parent),
  5. ui(new Ui::Widget)
  6. {
  7. ui->setupUi(this);
  8. thread_flag=0; //默认线程停止
  9. thread.show_ui=this; //给值
  10. }
  11. Widget::~Widget()
  12. {
  13. delete ui;
  14. }
  15. //开始执行线程
  16. void Widget::on_pushButton_startthread_clicked()
  17. {
  18. thread_flag=!thread_flag;
  19. if(thread_flag)
  20. {
  21. thread.count=0; //清空计数器
  22. thread.start(); //开始执行线程
  23. ui->pushButton_startthread->setText("停止执行线程");
  24. }
  25. else
  26. {
  27. thread.terminate();//终止线程的执行。线程可能会或可能不会立即被终止,这取决于操作系统的调度策略。终止后需要调用QThread::wait()()。
  28. thread.wait();
  29. ui->pushButton_startthread->setText("开始执行线程");
  30. }
  31. }
  32. //线程执行起始点
  33. void Thread::run()
  34. {
  35. /* 线程的相关代码 */
  36. while

文章来源: xiaolong.blog.csdn.net,作者:DS小龙哥,版权归原作者所有,如需转载,请联系作者。

原文链接:xiaolong.blog.csdn.net/article/details/120953263

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。