Important: Please read the Qt Code of Conduct -

Accessing gui from other class

  • My main class is "socketserver1" and i try to access it's gui from "MyServer" class. I created a signal on MyServer class: . And to my socketserver1(mainwindow) i added a slot to add log: . On my socketserver1 i added a connect to connect signal from server to slot from my socketserver1: . However, when on second class i do "emit addToLog("ABCDEF"); it doesn't fire the slot and nothing happens.

  • anyone? i need to fix this

  • Are you emitting the signal after you make the connection? If you are testing by emitting the signal in MyServer constructor, the connection will not have been made yet.

  • @Paul-H. i am emitting the signal when the server can list on port, so it will add to the consolelog that listening on that port started. I also will add to add to consolelog when incommingConnection is emitted. But whenever i emit the signal, it just doesn't put anything on the ConsoleLog. The connection is made in main thread, before the server starts. Even if i put to start the serveron push of a button (so i connect it before) it doesn't fire the slot.

  • Ok, I apparently don't understand everything that is going on in your program.
    But if I create a bare bones QObject MyServer class as follows:

    #include "myserver.h"
    MyServer::MyServer(QObject *parent) : QObject(parent)
    void MyServer::emitTest(const QString toadd)
        emit addToLog(toadd);

    Then add a call to emitTest in the socketserver1 class after the connect:

        MyServer *server = new MyServer(this);
        connect(server,SIGNAL(addToLog(const QString)),this,SLOT(addLog(const QString)));

    the slot is fired as expected. That is why I thought that the signal was being emitted before it was connected to the slot.

    Edit: You mention using threads, which I am not experienced with. Does it make a difference if you make the MyServer *server a member variable of socketserver1 class instead of declaring it locally in the constructor?

Log in to reply