Important: Please read the Qt Code of Conduct -

[solved]Dynamically updating the screen at the client side as soon as data collected from the server side

  • hello every one,
    I want data that has been passed from the server end to the client to appear on the screen at the client side as soon as the client receives the data every time. I don't want to perform some operation to make it appear on the screen ( i mean usage of buttons). can anybody help.
    thanks :)

  • I'd say: Create a background thread that, in a loop, listens on the connection for incoming data from the server and that will emit a signal as soon as new data has arrived.

    Then, in your GUI class add a slot that will receive and handle the updates. Just connect that slot to the background thread's signal an your are done...

    @class MyBackgroundThread : public QThread
    void run(void);

    void newDataArrived(int x);

    /* ... */

    void MyBackgroundThread:run(void)

        int x = nowReadTheNewData();
        emit newDataArrived(x);

    @class MyGuiClass : public QMainWindow
    void processNewData(int x);

    /* ... */

    void MyGuiClass::processNewData(int x)
    @main(int argc, char **argv)
    QApplication *app = new QApplication(argc, argv);

    MyBackgroundThread *bgThread = new MyBackgroundThread();
    MyGuiClass *guiWindow = new MyGuiClass();
    connect(bgThread , SIGNAL(newDataArrived(int)), guiWindow, SLOT(processNewData(int)), Qt::QueuedConnection);


  • thanks :)
    Actually i am new to Qt..If you provide the code snippet it would be of great help..

  • Well, I gave a (very rough) outline above.

    Other than that, I would highly recommend you read this, if you are new to Qt:

  • thanks a lot.. :) :)

  • Hello one problem. I am using the function connect in main class.. but getting the error: ‘connect’ was not declared in this scope. i tried included the header file of QObject class. pls somebody help

  • Is your "main" class derived from QObject? If not, it probably should be ;-)

    More specifically, for your "main" Window, you probably want to inherit from QMainWindow...


    Also, if you are new to Qt, you definitely should read this article thoroughly:

  • @Mulder
    Hello sir,
    now i am able to connect the signals and slot. can you please specify how to run a thread in background. now when i am calling the run function of the thread the main window is disappearing .
    thank you

  • One option is to create your own MyBackgroundThread class and make it inherit from QThread. Then simply re-implement the run() method and put the code there that you want to run in the background. Then, from the "main" thread, create an instance of the MyBackgroundThread and call start() to make the thread start. Do not call run() from the "main" thread directly, because this would run the run() method inside the "main" thread. Instead, calling start() will create a new thread in which the run() method will be run. Also see the code I posted above!


    Also don't forget this:

  • @MuldeR
    what i thought of doing is in thread class creating data member of mainwindow class type and in run method is passing calling the function of mainwindow object where all tcp realted connection and receiving data from the server will take place, Is it possible?

  • The creation of your GUI widgets and all (direct) access to your GUI widgets necessarily has to happen in "main" thread! At the same time, setting up your TCP connection, waiting for new messages to arrive over this connection as well as processing those messages can happen in another ("background") thread. That's why you would normally make your "background" thread emit Signals whenever new information is available! These signals will be picked up by the "main" thread in a suitable Slot function, from which you can access/update any GUI widgets that need to be updated. The queued connection type of Qt does exactly that: It processes signals (i.e. calls slot functions) in the context of the thread to which the target object belongs, rather than in the context of the thread which emitted the signal. Again, please see my example above for details on how you can do all that...

  • @Mulder
    hello sir,
    Now problem with the thread is solved . I am able to connect background and front end through signals and slot, but problem is regarding with the tcpsocket and tcpserver, when i put tcpsocket->connecttohost() inside run,
    i am getting the run time error
    QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "" and after that program unexpectedly ends I am not sure of where to put this line of code
    tcpSocket->connectToHost(); i even removed forever loop inside and tried but it was not of any use .. please help
    thanks :)

  • [quote]i am getting the run time error QAbstractSocket::connectToHost() called when already looking up or connecting/connected to “” and after that program unexpectedly ends I am not sure of where to put this line of code[/quote]

    What is "the" run time error? What error do you get exactly?

    Also, the connection would be established before the loop, inside the loop (if connection succeeded) you read/process the incoming messages, but do NOT re-connect every time...

    Anyway, here is a small QTcpSocket example found on the net:

  • @Mulder,
    thank you sir for the link.. that was really helpful,
    now i am not getting the above error, now the new run time error is
    Cannot create children for a parent that is in a different thread.
    specially comes when i write this part of the code
    socket =new QTcpSocket(this); that too comes specially because i am passing thread object("this" parameter) if i remove or won't in create the socket at run time the socket object is not identified itself, i have seen many people getting the same error, still not finding the rite answer.

  • Moderators

    MuldeR: Why a background thread? The socket classes are asynchronous, so why not do this in the UI thread?

  • sir whether
    connect(&socket,SIGNAL(readyRead()),this,SLOT(readData(QTcpSocket *)));
    are same?

  • @Mulder,
    thanks a lot sir,
    problem solved :) :)

  • [quote author="Tobias Hunger" date="1364115931"]MuldeR: Why a background thread? The socket classes are asynchronous, so why not do this in the UI thread?[/quote]

    It's of course only one possible way to do it. If he needs to decode/process the "messages" in some way or if he needs to assemble payload from multiple "messages" that are incoming over TCP connection or if most of the messages won't change the user-visible state, then it might be better to do the messages processing + program state update in a background thread and only send a Signal to the GUI thread when a new event (that the user needs to know about) has occurred. Keeps the GUI thread free from "lengthy" calculations. Also gives a better GUI/View vs. Model/Businesslogic separation. But yes, all that may be "overkill" for small apps...

Log in to reply