【发布时间】:2015-07-04 13:51:40
【问题描述】:
在尝试了我所知道的一切之后,甚至添加了
QT_MainWindow::QT_MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::QT_MainWindow)
{
ui->setupUi(this);
qRegisterMetaType<QTextCursor>("QTextCursor");
qRegisterMetaType<QTextBlock>("QTextBlock");
}
对于我的来源,我仍然无法在 QTextEdit 和 QPlainTextEdit 中修改来自其他线程的文本,而且我正在使用带有 Qt 的 OpenMP。
谁能告诉我在 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 )
【问题讨论】:
-
附点源码就好了,很难说……
-
好的,等一下我会的
-
@RobertWadowski 好的,我添加了源代码并注意我在运行任何东西之前都会执行 qRegisterMetaType
-
如果你想从不同的线程加载一些字符串,我认为你对自己来说很复杂。最好将线程源放在 qthread 中并通过信号/插槽进行通信。这不是绝对的 QT 方式....创建工作类,将其放入线程,连接到 ui 插槽,您就有了清晰(和分离)的实现。
-
我想我找到了原因见stackoverflow.com/questions/16501284/…。好像只有GUI线程可以访问ui和控件
标签: c++ multithreading qt openmp