C++ Qt C++;:无法为位于不同线程中的父线程创建子线程

C++ Qt C++;:无法为位于不同线程中的父线程创建子线程,c++,qt,C++,Qt,在Qt(c++)中创建一个简单的应用程序来加密文本。我得到一个错误:“不能为处于不同线程中的父线程创建子线程。” 我使用线程在输入文本时实时更新文本编辑框 我看到了其他类似的话题,但我没有找到任何适合自己的解决方案 你能告诉我怎么解决吗 .h文件 #ifndef GENERATORSHACODE_H #define GENERATORSHACODE_H #include <QMainWindow> #include <thread> QT_BEGIN_NAMESPAC

在Qt(c++)中创建一个简单的应用程序来加密文本。我得到一个错误:“不能为处于不同线程中的父线程创建子线程。”

我使用线程在输入文本时实时更新文本编辑框

我看到了其他类似的话题,但我没有找到任何适合自己的解决方案

你能告诉我怎么解决吗

.h文件

#ifndef GENERATORSHACODE_H
#define GENERATORSHACODE_H

#include <QMainWindow>
#include <thread>

QT_BEGIN_NAMESPACE
namespace Ui { class GeneratorShaCode; }
QT_END_NAMESPACE

class GeneratorShaCode : public QMainWindow
{
    Q_OBJECT

public:
    GeneratorShaCode(QWidget *parent = nullptr);
    ~GeneratorShaCode();
    QString Sha512Generator(QString);
    void updateOutputEditText();
    std::thread GenerateCode;

private:
    Ui::GeneratorShaCode *ui;
};
#endif // GENERATORSHACODE_H

#如果NDEF生成器代码为H
#定义生成器hacode_H
#包括
#包括
QT_开始名称空间
命名空间Ui{class GeneratorShaCode;}
QT_END_名称空间
类生成器代码:公共QMainWindow
{
Q_对象
公众:
GeneratorShaCode(QWidget*parent=nullptr);
~GeneratorShaCode();
QString SHA512发生器(QString);
void updateOutputeItText();
std::线程生成代码;
私人:
Ui::GeneratorShaCode*Ui;
};
#endif//GENERATORSHACODE_H
.cpp文件

#include "generatorshacode.h"
#include "ui_generatorshacode.h"
#include <windows.h>
#include "sha512.h"
#include <string>

#include <QtDebug>

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

    GenerateCode = std::thread(&GeneratorShaCode::updateOutputEditText, this);
    GenerateCode.detach();
}

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

QString GeneratorShaCode::Sha512Generator(QString Qstr)
{
    return QString::fromStdString(sha512(Qstr.toStdString()));
}

void GeneratorShaCode::updateOutputEditText()
{
    while(true)
    {
       ui->textEdit_Output->setText(Sha512Generator(ui->textEdit_Input->toPlainText()));
    }
}

#包括“generatorshacode.h”
#包括“ui_generatorshacode.h”
#包括
#包括“sha512.h”
#包括
#包括
GeneratorShaCode::GeneratorShaCode(QWidget*父项)
:QMainWindow(父级)
,ui(新ui::GeneratorShaCode)
{
用户界面->设置用户界面(此);
GenerateCode=std::thread(&GeneratorShaCode::updateOutputeText,this);
GenerateCode.detach();
}
GeneratorShaCode::~GeneratorShaCode()
{
删除用户界面;
}
QString GeneratorShaCode::Sha512Generator(QString Qstr)
{
返回QString::fromStdString(sha512(Qstr.toststring());
}
void GeneratorShaCode::UpdateOutputeItemText()
{
while(true)
{
ui->textEdit_Output->setText(Sha512Generator(ui->textEdit_Input->toPlainText());
}
}

GUI线程是主线程。不能在子线程中直接操作任何ui控件。通常,您应该在GUI线程和子线程之间使用信号/插槽。

不确定在这种情况下,
消息“无法创建子线程…”的确切位置。
但是。。。您直接从多个线程同时访问
ui
数据/变量,而不进行任何同步——这是未定义的行为(并且
Qt
不支持从运行
main
的线程以外的任何线程访问GUI组件)。您想使用信号和插槽在线程和GUI线程之间进行通信。我没有使用线程就解决了这个问题。我使用插槽和信号。对于textEdit对象,我使用了textChanged()方法。谢谢你的建议!