How to implement threading in a QDialog
-
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
-
Hi,
What is your question exactly ? How to move your logic to another thread ?
-
Yeah, how do I implement the table and adhere data to said table in another thread?
-
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
-
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).
-
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.
-
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!
-
Unless you're doing something very intensive, standard network operations can be done in the GUI thread since Qt's network stack is asynchronous.
-
Ok. Maybe I'll try a different approach then. Thanks for the help!