Qt使用QFuture错误创建子线程

时间:2012-02-08 00:01:05

标签: qt qthread

我正在尝试创建一个Collaborative Editor(我必须使用Linux网络库来处理所有网络内容),我有所有组件的主要小部件(继承QWidget的自定义类)。在构造函数中,我在这个主Widget上创建了所有Widgets,最后我尝试使用QFuture创建一个新线程(我使用QFuture而不是QThread,因为它允许我轻松地使用任何类型的参数调用函数,如QTextEdit,QTextCursor ...)但它在编译时给了我这个错误:
“QObject:无法为不同线程中的父级创建子级。 (Parent是QTextDocument(0x1b064b0),父线程是QThread(0x1985750),当前线程是QThread(0x1ae7610)“。
如何解决错误?
这是我的代码:
mainwindow.h:

...//includes
using namespace QtConcurrent;
...
namespace Ui {
    class Widget;
    class TextEdit;
}

class TextEdit;
class Widget;

class Widget : public QWidget {
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
    ...
    QFuture<void> thread;
}

class TextEdit : public QTextEdit {
    Q_OBJECT
    ...
}

static void receiveKeyPress(TextEdit *textedit, QTextCursor *secondUserCursor) {
    unsigned long long int Number = NULL;
    QMessageBox::information(textedit->parentWidget(), "UI Component", "This makes the thread to throw the error");
    while(1) if(connected == 1) {
        read(recvFileDescriptor, &Number, sizeof(unsigned long long int));
          if( Number != NULL)
            if( Number == Qt::Key_Home )
              secondUserCursor->movePosition(QTextCursor::StartOfLine);
              ...
              else {
                QTextCharFormat backgroundFormat = textedit->textCursor().charFormat();
                backgroundFormat.setBackground(QColor("lightGreen"));
                //If I don't use QMessageBox up there, it breaks here on the next command
                secondUserCursor->setCharFormat(backgroundFormat);
                secondUserCursor->setPosition(textedit->textCursor().position());
                secondUserCursor->insertText(QString::number(Number));
               } //else
            }//while
        }//the function

mainwindow.cpp:

#include "mainwindow.h"

Widget::Widget(QWidget *parent) {
...
thread = run(receiveKeyPress, this->edit1, this->edit1->secondUserCursor); //run is from QtConcurrent namespace 
}


main.cpp中:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget window;
...
    window.show();
    return a.exec();
}



我在这里读过stackoverflow,其他人如何使用QObject(我从未使用过,我也不了解它)和QThread(唯一的组合),但我已经尝试使用QThread而我无法通过QTextEdit和QTextCursor。它
提前致谢
修改


mainwindow.h

class TextEdit : public QTextEdit {
    Q_OBJECT
...
public slots:
    void receiveKeyPress(qulonglong);
...
};


mainwindow.cpp

void TextEdit::receiveKeyPress(qulonglong Number) {
if( Number == Qt::Key_Home )
...
}


recv-thread.h - 根据此链接http://developer.qt.nokia.com/doc/qt-4.8/thread-basics.html#example-3-clock

创建
#include <QThread>
#include "mainwindow.h" //To get TextEdit in here

class RecvThread : public QThread {
    Q_OBJECT

signals:
    void transferDataToSlot(qulonglong Data);

protected:
    void run();
};


的recv-thread.cpp

#include "recv-thread.h"

void RecvThread::run() {
    unsigned long long int Number = NULL;
    while(1) if(connected == 1) {
        read(recvFileDescriptor, &Number, sizeof(unsigned long long int));
        if( Number != NULL) {
            emit transferDataToSlot(Number);
        }
    }
}


的main.cpp

...
#include "recv-thread.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget window;
    RecvThread recvThread;
...
    QObject::connect(&recvThread, SIGNAL(transferDataToSlot(qulonglong)), window.edit1, SLOT(receiveKeyPress(qulonglong)), Qt::QueuedConnection); //line 38
    recvThread.start();
    //Displaying the window
    window.show();
    a.exec();
    recvThread.quit();
    recvThread.wait();
    return 0;
}

我做得对吗?

0 个答案:

没有答案