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. Write Ethernet data to Mainwindow ui label using signal slot
QtWS25 Last Chance

Write Ethernet data to Mainwindow ui label using signal slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 206 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.
  • E Offline
    E Offline
    ELIF
    wrote on last edited by ELIF
    #1

    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.

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • E ELIF

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • E ELIF

        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.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1

        • Login

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