Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtConcurrent::run problem
QtWS25 Last Chance

QtConcurrent::run problem

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asad
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asad
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Don't create the QTableView in that function

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved