Qt线程的弱问

小littleSun 2018-05-05 11:42:10
主线程中
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
listWidget = new QListWidget(this);
this->resize(300,300);
pBtn1= new QPushButton(this);
pBtn1->setText("stop");

thread = new QThread;
worker = new Work();
worker->moveToThread(thread);
qDebug()<<connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
qDebug()<<connect(thread, SIGNAL(started()), worker, SLOT(process()));
qDebug()<<connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
qDebug()<<connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
qDebug()<<connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
qDebug()<<connect(pBtn1,SIGNAL(clicked(bool)),worker,SLOT(finish()));
thread->start();
}


子线程中
void Work::process()
{
while(1)
{

qDebug()<<"hello thread";
QCoreApplication::processEvents();
QThread::sleep(1);
}

// Widget::i =5;
// Widget::listWidget->addItem(QString("hello"));
emit finished();
emit testsig();
}


void Work::finish()
{
qDebug()<<"finish in";
emit finished();
emit testsig();
}

现在我定义的按钮按下之后,主线程也相应了,但是子线程就是不停止运行是怎么回事啊
...全文
3002 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_26651063 2018-10-09
  • 打赏
  • 举报
回复
while中增加一个判断机制吧,满足某个条件break出去
彩阳 2018-10-04
  • 打赏
  • 举报
回复
引用 5 楼 shiyanbo_1006 的回复:
在while中增加一个标志,当受到emit信号时,标志置位,while结束,然后线程就可以结束了
稍微高级一点的,线程的run函数中启动一个QEventLoop,然后信号连接这个QEventLoop的quit()槽。这样也可以达到效果,还可以处理一些程序想要处理的事件。
待续_1006 2018-05-14
  • 打赏
  • 举报
回复
在while中增加一个标志,当受到emit信号时,标志置位,while结束,然后线程就可以结束了
dearbub 2018-05-09
  • 打赏
  • 举报
回复
在while中增加一个标志,当受到emit信号时,标志置位,while结束,然后线程就可以结束了
junki 2018-05-08
  • 打赏
  • 举报
回复
除了上面问题,要学会调试跟踪代码
Marcelxx 2018-05-07
  • 打赏
  • 举报
回复
void Work::process() { while(1) { qDebug()<<"hello thread"; QCoreApplication::processEvents(); QThread::sleep(1); } // Widget::i =5; // Widget::listWidget->addItem(QString("hello")); emit finished(); emit testsig(); } 你开启了一个死循环,线程一致在执行。
比菜鸟还菜 2018-05-07
  • 打赏
  • 举报
回复
while(1)死循环

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧