OpenMP、QTextEdit 和 QPlainTextEdit

OpenMP, QTextEdit and QPlainTextEdit

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

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 以提高性能。

好吧,我做到了,我认为它一定比连接插槽简单一些。

在我的 qt_mainwindow.h 中我只是添加了

public slots:
    void QT_PlainTextEdit_appendPlainText(QString line);
    void QT_PlainTextEdit_SetText(QString line);

在我的 qt_mainwindow.cpp 中我得到了这些行

void QT_MainWindow::QT_PlainTextEdit_appendPlainText(QString line)
{
    ui->QT_PlainTextEdit->appendPlainText(line);
}

void QT_MainWindow::QT_PlainTextEdit_SetText(QString line)
{
    ui->QT_PlainTextEdit->setPlainText(line);
}

void QT_MainWindow::Load()
{
    #pragma omp parallel num_threads(1)
    {
        std::ifstream file("file.txt");
        std::string line;

        while ( std::getline(file, line) )
            QMetaObject::invokeMethod(this,"QT_PlainTextEdit_appendPlainText",Qt::QueuedConnection,Q_ARG(QString,QString::fromStdString(line)));
        file.close();
    }
}

好吧,我尽可能多地尝试了它并没有失败,但我想知道我是否做对了