QtConcurrent::run problem



  • My basic requirement is I want to load a Qtableview in different thread.But I am gettting (QObject: Cannot create children for a parent that is in a different thread.)(Parent is QApplication(0x3efe38), parent's thread is QThread(0x40ddb0), current thread is QThread(0x433400)
    0xfc4 ontable QObject::setParent: Cannot set parent, new parent is in a different thread.

    this is my mainwindow class
    @ui->setupUi(this);

    QWidget *widget =  new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout;
    QFuture<void> future = QtConcurrent::run(this,&MainWindow::ontableView);
    qDebug()<< "mainwindow thread"<< thread()->currentThreadId();
    future.waitForFinished();
    

    layout->addWidget(m_tableview);

    widget->setLayout(layout);
    setCentralWidget(widget);@
    

    this is my fuction that i want to run in different thread.
    @void MainWindow::ontableView()
    {
    m_tableview = new QTableView;
    m_model= new QStandardItemModel();
    for (int i=0;i<100000;i++)
    {
    for (int j=0;j<5;j++)
    {

     m_item = new QStandardItem(QString(" %0,%1").arg(i).arg(j));
    m_model->setItem(i,j,m_item);
        }
    }
    
    m_tableview->setModel(m_model);
    

    qDebug()<< QThread::currentThreadId() << "ontable";
    }@

    Please help guys what i am missing.. Thanks in Advance.


  • Lifetime Qt Champion

    Hi,

    You are doing something forbidden: you can't create/modify GUI elements in a secondary thread. You can communicate from a secondary thread to the main a.k.a. GUI thread through signals and slots to change something in the GUI.

    However you can update the content of the model from another thread.

    Anyway, following your implementation, why do you use QtConcurrent since you have to wait for it to finish ?



  • Thanks for your reply,
    @ future.waitForFinished();@

    i removed this line so now its giving the error.
    QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread.

    Please can you give an idea or example how.to put the function OntableView In differnet thread


  • Lifetime Qt Champion

    Don't create the QTableView in that function


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.