Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt线程使Qt创建者暂时没有响应_Qt_Qthread - Fatal编程技术网

Qt线程使Qt创建者暂时没有响应

Qt线程使Qt创建者暂时没有响应,qt,qthread,Qt,Qthread,我有一个关于使用QtCreator4.5和Qt5.7演示QThread的简单项目。在项目中,将创建三个按钮,threadA、threadB和quit。我的目的是在分别单击threadA和threadB时,在控制台中打印A和B,并在按下“quit”时退出应用程序 下面是mythread.h: #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QString> class mythread

我有一个关于使用QtCreator4.5和Qt5.7演示QThread的简单项目。在项目中,将创建三个按钮,
threadA
threadB
quit
。我的目的是在分别单击
threadA
threadB
时,在控制台中打印
A
B
,并在按下“
quit
”时退出应用程序

下面是mythread.h:

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QString>

class mythread : public QThread
{
    Q_OBJECT

public:
    mythread();
    void setMessage(const QString &message);
    void stop();

protected:
    void run();

private:
    QString messageStr;
    volatile bool stopped;
};

#endif // MYTHREAD_H
mainwindow.cpp是:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_threadA_clicked()
{
    mythreadA.start();
    mythreadA.setMessage("A");
}

void MainWindow::on_threadB_clicked()
{
    mythreadB.start();
    mythreadB.setMessage("B");
}

void MainWindow::on_quit_clicked()
{
    mythreadA.stop();
    mythreadB.stop();
    MainWindow::close();
}

运行项目时,打印的结果将显示在QtCreator的应用程序输出中,而不是外部提示控制台中。退出应用程序使QtCreator暂时没有响应,但最终恢复正常。应用程序退出时,线程似乎仍在运行

我已经测试了你的代码,Qt-Creator没有任何错误,没有冻结

但是,在编写代码时,退出应用程序时,线程可能仍在运行。此外,私有成员
停止
应该受到互斥锁的保护,因为
volatile
不会执行此任务

要使用互斥锁保护您的私有变量
stopped
,您可以通过以下方式使用例如
QMutexLocker

void MyThread::stop() // called by the GUI Thread
{
    const QMutexLocker locker(&m_mutex);
    stopped = true;
}
要读取布尔值,请执行以下操作:

bool MyThread::isStopped // called by run()
{
    const QMutexLocker locker(&m_mutex);
    return stopped;
}
最后,要确保在按下退出按钮时正确完成线程,请执行以下操作:

void MainWindow::on_quit_clicked()
{
    mythreadA.stop();
    mythreadB.stop();
    myThreadA.wait(); 
    myThreadB.wait();
    this->close(); // close the main application
}     

主窗口::关闭()@eyllanesc:有什么问题吗?是的,更改
MainWindow::close()
关闭()@eyllanesc:没有太大区别。QtCreator在退出时仍将有一段时间“无响应”。可能,我需要创建一个控制台应用程序。eyllanesc:解决方案是将输出重定向到控制台,而不是从while中删除QtCreator或std::cout。我不知道为什么我可以被绞死。即使你用一个无限长的线程阻塞了应用程序的主线程,它也能正常工作。当我运行项目时,他在SO问题
中写道,打印的结果显示在QtCreator的应用程序输出中,
void MainWindow::on_quit_clicked()
{
    mythreadA.stop();
    mythreadB.stop();
    myThreadA.wait(); 
    myThreadB.wait();
    this->close(); // close the main application
}