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. How to control MainWindow widget from Thread ?
Forum Updated to NodeBB v4.3 + New Features

How to control MainWindow widget from Thread ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 877 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.
  • H Offline
    H Offline
    Hiloshi
    wrote on last edited by
    #1

    Hi All,

    I create a MainWindow , a button and a listWidget on my mainwindow.cpp by:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    
    void MainWindow::on_pushButton_2_clicked()
    {
        ui->listWidget_2->addItem("abc");
    }
    

    Then I create a thread by Qthread as below:

    class T3Thread : public QThread
    {
        Q_OBJECT
    
    private:
        void run()
         {
             //ui->listWidget_2->addItem("567");
             for (int i = 0; i <= 100; i++)
             {          
                 delay(20);
             }
         }
    };
    

    I want to show some information to my listWiget_2, however listWidget_2 not belong to "T3Thread",
    so compiler will fail if I add "ui->listWidget_2->addItem("567");".

    I understand this issue is cause by ui not declared in T3Thread, but I do not now how to fix it.

    Any suggestion will be very appreciate.

    Thanks,

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      • Create a signal in the thread like Q_SIGNAL void addItemToList(const QString&);
      • Create a slot in MainWindow like void addItemToList(const QString& val){ui->listWidget_2->addItem(val);}
      • connect them connect(t3thread,&T3Thread::addItemToList,this,&MainWindow::addItemToList,Qt::QueuedConnection);
      • emit the signal from the thread when you want to add an item emit addItemToList("567");

      P.S.
      Almost mandatory reading: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      H 1 Reply Last reply
      4
      • H Offline
        H Offline
        Hiloshi
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • VRoninV VRonin
          • Create a signal in the thread like Q_SIGNAL void addItemToList(const QString&);
          • Create a slot in MainWindow like void addItemToList(const QString& val){ui->listWidget_2->addItem(val);}
          • connect them connect(t3thread,&T3Thread::addItemToList,this,&MainWindow::addItemToList,Qt::QueuedConnection);
          • emit the signal from the thread when you want to add an item emit addItemToList("567");

          P.S.
          Almost mandatory reading: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

          H Offline
          H Offline
          Hiloshi
          wrote on last edited by
          #4

          @VRonin
          Thanks for your help. I should spend some time to try your suggestion and study the link in more detail.

          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