Write Ethernet data to Mainwindow ui label using signal slot
-
I have a problem.I have an ethernet connection in ethernet class.I have to write data's (coming from ethernet) to ui label.Here is my ethernet class run method.
void ethernetthread::run(){ printf("Connecting to hello world sever"); void *context = zmq_ctx_new(); void *requester=zmq_socket(context ,ZMQ_REQ); zmq_connect(requester,"tcp://localhost:5555"); int request_nbr; for(request_nbr=0; request_nbr!=10; request_nbr++){ char buffer[10]; printf("Sending Hello %d\n" ,request_nbr); zmq_send(requester ,"abcde",10,0); zmq_recv(requester ,buffer,10,0); zmq_recv(requester,buffer,10,0); for(int i=0;i<10;i++){ printf("%c :",buffer[i]); qDebug()<<"buffer["<<i<<"] : "<<buffer[i]; } printf("Received World %d\n",request_nbr); } zmq_close(requester); zmq_ctx_destroy(context); }
Here is my MainWindow Class
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new
Ui::MainWindow)
{timerCounter = new QTimer(); connect(timerCounter,SIGNAL(timeout()),this,SLOT(timer_label_test_slot())); timerCounter->start(1000);
}
void MainWindow::timer_label_test_slot(){
this->ui->label->setText(QString::number(counter));
counter++;}
void MainWindow::timer_label_test_slot(){ this->ui->label->setText(QString::number(counter)); counter++; }
ethernet.h is here
#ifndef ETHERNETTHREAD_H #define ETHERNETTHREAD_H #include<QThread> #include<QDebug> #include"ethernetthread.h" #include<zmq.h> #include <QThread> #include<zmq.hpp> #include<QTimer> class ethernetthread : public QThread { public: ethernetthread(); void run(); ; #endif // ETHERNETTHREAD_H
So how can I connect this two classes? How can I show ethernet datas to ui label ? Maybe I should make signal slot mechanism between two seperate classes.
-
@ELIF said in Write Ethernet data to Mainwindow ui label using signal slot:
Maybe I should make signal slot mechanism between two seperate classes.
Yes indeed! Instead of needing any timer, have your thread raise a signal when it receives data and your UI have a slot on that to update whatever visually. It is very common to have signals/slots connected across classes/threads.
-
@ELIF said in Write Ethernet data to Mainwindow ui label using signal slot:
zmq_socket
question do you have to use zmq functions and classes or are you free to choose? because Qt has https://doc.qt.io/qt-5/qtcpsocket.html that offers an asynchronous api. No need for threading