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 implement threading in a QDialog
Forum Updated to NodeBB v4.3 + New Features

How to implement threading in a QDialog

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.0k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    KGWAY
    wrote on last edited by
    #1

    I'm trying to understand threading but I'm still not fully grasping it. I have a GUI application that is from a simple Dialog base class. I'm capturing data off Ethernet and will save it to a table. I want to use a dedicated thread to capturing certain data and writing it to the table in the ui.

    dialog.ccp

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        m_TableHeader<<"Name"<<"Number"<<"Address";    
        ui->setupUi(this);
    }
    
    Dialog::~Dialog()
    {
       delete ui;
    }
    
    ....
    
    void Dialog::table
    {
    ui->tableWidget->setRowCount(30);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->showGrid();
     ui->tableWidget->setHorizontalHeaderLabels(m_TableHeader); 
    }
    
    void Dialog::MyTimer()
    {
        timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(SecSlot()));
    
        timer->start(1000);
    }
    
    void Client::SecSlot()
    {
       //get data from ethernet
    }
    

    Dialog.h

    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
       Q_OBJECT
    
    public:
       explicit Dialog(QWidget *parent = 0);     
        void table();     
        void MyTimer
    
       QTimer *timer; 
    
       ~Dialog();
    
    signals:
       
    
    public slots:
       void SecSlot();
    
    private:
       Ui::Dialog *ui;
       QTcpSocket *socket;   
       QStringList m_TableHeader;
    };
    
    #endif // Dialog_H
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What is your question exactly ? How to move your logic to another thread ?

      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
      • K Offline
        K Offline
        KGWAY
        wrote on last edited by
        #3

        Yeah, how do I implement the table and adhere data to said table in another thread?

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

          Don't update GUI element from another thread, use signals and slots to transmit data back to the GUI thread. It's explained in QThread's documentation

          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
          • K Offline
            K Offline
            KGWAY
            wrote on last edited by
            #5

            So I'll need to make a new class dedicated to the thread itself and do the work through that thread then transmit to the Dialog.cpp file through Signals and Slots? or am I not understanding this still? Sorry threading is a bit of a new subject for me (I've only really approached it today).

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

              Yes, following the worker object paradigm (which is the correct one as it seems in your case)

              Before starting with threading, you should read about the subject. It's one of the trickiest you can find.

              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
              • K Offline
                K Offline
                KGWAY
                wrote on last edited by KGWAY
                #7

                I may have shot myself in the foot initially then because I capture most of my data in the Main dialog but maybe it will be best to just capture all data in separate threads entirely. I'm not close to being done with it, but it seems to be a little slow as is so I want to stream line the GUI as much as possible. Thanks for the suggesting I may just do that!

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

                  Unless you're doing something very intensive, standard network operations can be done in the GUI thread since Qt's network stack is asynchronous.

                  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
                  • K Offline
                    K Offline
                    KGWAY
                    wrote on last edited by
                    #9

                    Ok. Maybe I'll try a different approach then. Thanks for the help!

                    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