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. accessing and reading public variable of object1 from another object2 or object3
Forum Updated to NodeBB v4.3 + New Features

accessing and reading public variable of object1 from another object2 or object3

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 461 Views 1 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Apart from the real problem - why do you need threading here at all? Do you process large data somewhere?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    VikramSamyV 1 Reply Last reply
    2
    • Christian EhrlicherC Christian Ehrlicher

      Apart from the real problem - why do you need threading here at all? Do you process large data somewhere?

      VikramSamyV Offline
      VikramSamyV Offline
      VikramSamy
      wrote on last edited by
      #3

      @Christian-Ehrlicher said in accessing and reading public variable of object1 from another object2 or object3:

      Apart from the real problem - why do you need threading here at all? Do you process large data somewhere?

      actually the original code already use threading, i just adding stuff to the original code and modifying it according to my needs now.....actually the client is a camera that will send some data to the server which will process using the Data_process class. i dont want to change the architecture as it is already working, now i just need add a extra thing to count the number of clients.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        There is no need to emit a signal from within the thread since you can count it inside TCP_Server::incomingConnection() - you even already have the count there - in clientcameramap.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        VikramSamyV 1 Reply Last reply
        2
        • LematL Offline
          LematL Offline
          Lemat
          wrote on last edited by Lemat
          #5

          try out:

          in Connection_Thread.h add:

          signals:
          void thread_is_out (qintptr descriptor);
          

          in Connection_Thread.cpp (slot Disconnected):

          void Connection_Thread::Disconnected ()
          {
           //run yours codes
            emit thread_is_out (this->socket_descriptor);
          }
          

          in TCP_Server.cpp :

          void TCP_Server::incomingConnection(qintptr socketDescriptor)
          {
            //add
           connect(thread, SIGNAL(thread_is_out (qintptr)), this, SLOT(create_on_function(qintptr)));
          }
          
          void TCP_Server::create_on_function(qintptr key)
          { 
          clientcameramap.erase(clientcameramap.find(key));
          //or
          clientcameramap.remove(key);
          
          //and what you want to do
          }
          

          just try out!!!

          LematL VikramSamyV 2 Replies Last reply
          1
          • LematL Offline
            LematL Offline
            Lemat
            wrote on last edited by
            #6
            This post is deleted!
            1 Reply Last reply
            0
            • LematL Lemat

              try out:

              in Connection_Thread.h add:

              signals:
              void thread_is_out (qintptr descriptor);
              

              in Connection_Thread.cpp (slot Disconnected):

              void Connection_Thread::Disconnected ()
              {
               //run yours codes
                emit thread_is_out (this->socket_descriptor);
              }
              

              in TCP_Server.cpp :

              void TCP_Server::incomingConnection(qintptr socketDescriptor)
              {
                //add
               connect(thread, SIGNAL(thread_is_out (qintptr)), this, SLOT(create_on_function(qintptr)));
              }
              
              void TCP_Server::create_on_function(qintptr key)
              { 
              clientcameramap.erase(clientcameramap.find(key));
              //or
              clientcameramap.remove(key);
              
              //and what you want to do
              }
              

              just try out!!!

              LematL Offline
              LematL Offline
              Lemat
              wrote on last edited by
              #7

              @Lemat

              //additional
              here is my code to destroy all threads after server stopped

              QList <QThread*> thread_list=TCP_Server::findChildren <QThread*>();
              foreach (QThread *elem, thread_list) elem->exit(0);
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                There is no need to emit a signal from within the thread since you can count it inside TCP_Server::incomingConnection() - you even already have the count there - in clientcameramap.

                VikramSamyV Offline
                VikramSamyV Offline
                VikramSamy
                wrote on last edited by
                #8

                @Christian-Ehrlicher said in accessing and reading public variable of object1 from another object2 or object3:

                There is no need to emit a signal from within the thread since you can count it inside TCP_Server::incomingConnection() - you even already have the count there - in clientcameramap.

                yup i already can count , but now let say some clients got disconnected, then i will need to remove the client from the clientcameramap right, and then i need to update to the frontpage object so that it can refresh the label.

                For this i created a "Clientdisconnected" slot in the TCP_server to remove the client from the map, as per that if u refer my Connection_Thread::run() functions, once a client signals disconnect, i want call the slot to remove the clients, only this part i confuse because the TCP_server object is created on the mainwindows.cpp, so i cant connect the client disconnect signal to the TCP_server objects slot.

                KillerSmathK 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @VikramSamy said in accessing and reading public variable of object1 from another object2 or object3:

                  but now let say some clients got disconnected,

                  Then you should update your clientcameramap (already now) - otherwise you've old stuff in this map and it's useless. Since you know when a client is disconnected (I assume that you close the thread in Disconnected()) you can watch on the QThread::finished() signal.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • VikramSamyV VikramSamy

                    @Christian-Ehrlicher said in accessing and reading public variable of object1 from another object2 or object3:

                    There is no need to emit a signal from within the thread since you can count it inside TCP_Server::incomingConnection() - you even already have the count there - in clientcameramap.

                    yup i already can count , but now let say some clients got disconnected, then i will need to remove the client from the clientcameramap right, and then i need to update to the frontpage object so that it can refresh the label.

                    For this i created a "Clientdisconnected" slot in the TCP_server to remove the client from the map, as per that if u refer my Connection_Thread::run() functions, once a client signals disconnect, i want call the slot to remove the clients, only this part i confuse because the TCP_server object is created on the mainwindows.cpp, so i cant connect the client disconnect signal to the TCP_server objects slot.

                    KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on last edited by
                    #10

                    @VikramSamy

                    You could watch the Qthread::finished() signal.
                    Disconnect = Call Exit Thread
                    Remove the item from Map as mentioned by Lemant and finally emit a signal from TCP_Server to update the count on frontpage label.

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    VikramSamyV 1 Reply Last reply
                    0
                    • LematL Lemat

                      try out:

                      in Connection_Thread.h add:

                      signals:
                      void thread_is_out (qintptr descriptor);
                      

                      in Connection_Thread.cpp (slot Disconnected):

                      void Connection_Thread::Disconnected ()
                      {
                       //run yours codes
                        emit thread_is_out (this->socket_descriptor);
                      }
                      

                      in TCP_Server.cpp :

                      void TCP_Server::incomingConnection(qintptr socketDescriptor)
                      {
                        //add
                       connect(thread, SIGNAL(thread_is_out (qintptr)), this, SLOT(create_on_function(qintptr)));
                      }
                      
                      void TCP_Server::create_on_function(qintptr key)
                      { 
                      clientcameramap.erase(clientcameramap.find(key));
                      //or
                      clientcameramap.remove(key);
                      
                      //and what you want to do
                      }
                      

                      just try out!!!

                      VikramSamyV Offline
                      VikramSamyV Offline
                      VikramSamy
                      wrote on last edited by VikramSamy
                      #11

                      @Lemat @Christian-Ehrlicher @KillerSmath

                      yup it works! thanks, so each time a client connects & diconnects i can know the count, so now im trying pass the count value to the FrontPage Object which is created in the mainwindow.cpp

                       FP = new FrontPage(this);  // at mainwindows.cpp
                      

                      the Frontpage class.h is as below:-

                      class FrontPage : public QWidget
                      {
                          Q_OBJECT
                      public:
                          explicit FrontPage(QWidget *parent = 0);
                          void ServerStatus (bool status);
                          QLabel      *serverstatuslabel;
                          QLabel      *clientconnectedlabel;
                          QLabel      *ServerIPaddr;
                          QLabel      *ServerPORTno;
                          QLabel      *infolabel;
                          QLabel      *clientcount;
                      
                      signals:
                      
                      public slots:
                          void ClientCount(unsigned int count);
                      
                      private:
                          QVBoxLayout  *vboxlayout;
                      };
                      
                      

                      so i need to emit signal at TCP_Server::incomingConnection , and connect it to the FrontPage slots: FrontPage::ClientCount(unsigned int count) then i should also emit a signal from the TCP_Server::ClientDisconnected after removing the client from the clientcameramap to the same slot FrontPage::ClientCount(unsigned int count), so this way i can pass around the count value to be refreshed at the Frontpage label each time a connects/disconnects happens...
                      But like just now the FrontPage is created in mainwindows.cpp, as per that the frontpage object not visible/accessible to the TCP_server, how to go around with this?? this where i still not much understand... can u tell something about this?? my FrontPage is slot code is as below:-

                       void FrontPage::ClientCount(unsigned int count)
                        {
                            QString tmp = QString::number(count);
                            QString Stringtext;
                      
                            Stringtext = "number of client connected is = %1";
                      
                            clientconnectedlabel->setText(Stringtext.arg(tmp));
                      
                            //QString tmp = QString::number(myInt)
                            //QString tmp = tr("%1").arg(myInt)
                        }
                      

                      but i need a proper way to connect both signals emitted from TCP_Server::incomingConnections and TCP_Server::ClientDisconnected to the FrontPage slot.

                      below is some part of my related mainwindows.cpp code:-

                      FP = new FrontPage(this);
                      m_server = new TCP_Server(m_process_data,this);                                                                       
                      
                           if(m_server->Start_Server())
                           {      connect(m_server,SIGNAL(status(APC_CAMERA_DATA)),m_sql_process,SLOT(Store_APC_CAMERA_DATA(APC_CAMERA_DATA)));
                               FP->ServerStatus(true);
                           }
                           else
                           {
                               FP->ServerStatus(false);
                           }
                      
                          setCentralWidget(FP);
                      /*so this FP is just a gui widget display the server is running and number of clients connected to it*/
                      
                      1 Reply Last reply
                      0
                      • KillerSmathK KillerSmath

                        @VikramSamy

                        You could watch the Qthread::finished() signal.
                        Disconnect = Call Exit Thread
                        Remove the item from Map as mentioned by Lemant and finally emit a signal from TCP_Server to update the count on frontpage label.

                        VikramSamyV Offline
                        VikramSamyV Offline
                        VikramSamy
                        wrote on last edited by
                        #12

                        @KillerSmath said in accessing and reading public variable of object1 from another object2 or object3:

                        finally emit a signal from TCP_Server to update the count on frontpage label.

                        The problem is , i can emit signal from TCP_server to update the count on the FrontPage label, as i just explain in my previous post.. and i also already created the slot functions of FrontPage, and this slot should connected with the TCP_server signal for update, now here is problem, the signal & slot need Object1 (TCP_server) and Object 2 (FrontPage), and as FrontPage object is created on the mainwindow.cpp, im stuck on how make this to work coz FrontPage object is not available/visible to TCP_Server,

                        jsulmJ 1 Reply Last reply
                        0
                        • VikramSamyV VikramSamy

                          @KillerSmath said in accessing and reading public variable of object1 from another object2 or object3:

                          finally emit a signal from TCP_Server to update the count on frontpage label.

                          The problem is , i can emit signal from TCP_server to update the count on the FrontPage label, as i just explain in my previous post.. and i also already created the slot functions of FrontPage, and this slot should connected with the TCP_server signal for update, now here is problem, the signal & slot need Object1 (TCP_server) and Object 2 (FrontPage), and as FrontPage object is created on the mainwindow.cpp, im stuck on how make this to work coz FrontPage object is not available/visible to TCP_Server,

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @VikramSamy Is TCP_Server accessible in main window? If so do the connection there.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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