Solved accessing and reading public variable of object1 from another object2 or object3
-
@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.
-
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.
-
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!!!
-
This post is deleted! -
//additional
here is my code to destroy all threads after server stoppedQList <QThread*> thread_list=TCP_Server::findChildren <QThread*>(); foreach (QThread *elem, thread_list) elem->exit(0);
-
@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.
-
@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.
-
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. -
@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*/
-
@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,
-
@VikramSamy Is TCP_Server accessible in main window? If so do the connection there.