gpt4 book ai didi

c++ - OpenMP、QTextEdit 和 QPlainTextEdit

转载 作者:行者123 更新时间:2023-11-30 01:13:47 28 4
gpt4 key购买 nike

好吧,在尝试了我所知道的一切之后,甚至通过添加

QT_MainWindow::QT_MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::QT_MainWindow)
{
ui->setupUi(this);
qRegisterMetaType<QTextCursor>("QTextCursor");
qRegisterMetaType<QTextBlock>("QTextBlock");
}

根据我的消息来源,我仍然无法在 QTextEdit 和 QPlainTextEdit 中修改来自其他线程的文本,而且我正在将 OpenMP 与 Qt 一起使用。

谁能告诉我在 QTextEdit 和 QPlainTextEdit 中修改来自其他线程的文本的正确方法是什么,因为我没有设法找到任何相关的帮助我

这是我的来源:

void QT_MainWindow::Load()
{
ui->QT_PlainTextEdit->setPlainText("");

std::ifstream file("File.txt");
std::string line;
#pragma omp parallel
{
while ( std::getline(file, line) )

ui->QT_PlainTextEdit->appendPlainText( QString::fromStdString(line));

file.close();
}
}

我设法让它像这样工作

void QT_MainWindow::Load()
{
omp_set_dynamic(0);
omp_set_nested(3);
#pragma omp parallel num_threads(3)
{
ui->QT_PlainTextEdit->setPlainText("");
}
}

但是如果我尝试设置文本

void QT_MainWindow::Load()
{
omp_set_dynamic(0);
omp_set_nested(3);
#pragma omp parallel num_threads(3)
{
ui->QT_PlainTextEdit->setPlainText("TEST");
}
}

我收到这个错误

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x3bbc758), parent's thread is QThread(0x3bd140), current thread is QThread(0x3bbcb68)
The program has unexpectedly finished.

还有

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x465bfc0), parent's thread is QThread(0x3f3ad60), current thread is QThread(0x466d450)QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x465bfc0), parent's thread is QThread(0x3f3ad60), current thread is QThread(0x46eebe0)HEAP[app.exe]:
Invalid address specified to RtlFreeHeap( 00000000023F0000, 0000000003F3DC40 )

最佳答案

问题是您正在主线程以外的线程中访问 Qt GUI 对象。

来自 http://doc.qt.io/qt-4.8/threads-qobject.html

Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.

解决此问题的一种方法是使用 Qt 信号/槽使用 QueuedConnection 将来自工作线程的信号连接到主线程中的槽,但是在您的情况下,我认为这没有多大意义。即使您收到使用 openmp 的信号,您也无法以并行方式将字符串附加到 QPlainTextEdit 以提高性能。

关于c++ - OpenMP、QTextEdit 和 QPlainTextEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221388/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com