Change GUI objects from a scound class



  • Hi,
    I have a standard Qt Widgets Application.

    I want to access methods in my MainWindow class from a other class.
    (I wrote methode to change the button names, for example)
    The problem is I have to use this instance of the GUI which I instantiate in the main because if I instance a new objekt of my MainWindow class I can not change the button attributes.

    Is that right that I have to use this instance?
    I tried to use singleton for this but im not able to solve it.
    Has anybody a working example how to change GUI attributes from a other class as the mainwindow.cpp?? Maybe you can upload it?

    Thanks guys


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should take a look at the Signals and Slots chapter of Qt's documentation as well as the examples.

    You should also describe a bit more your application workflow so we may help you more easily



  • Hi and thank You form your answer,

    Not sure if I can solve this with signal and slots.

    My program has two classes one for the GUI witch inherits from QObject and Ui and my second class TcpCon which is used to open a socket to a Java socket server.

    I start the GUI in main with MainWindow w; w.show();

    And finally I want to access the gui from my second class TcpCon.

    I'm tried to bring the variable w to my TcpCon class implement a singleton from MainWindow but my c++ skills are not very good to do that.
    Now I hope that anybody have an example for me. Or give me please a hint how to do it easily:)

    Cheers


  • Moderators

    There's no need for a singleton here. Just pass an instance of one class to the other (via constructor or a proper setter method), e.g.

    class TcpCon : public /* whatever */ {
        ... //the usual stuff
    public:
       inline void setUI(MainWindow* window) { ui = window; }
    private:
       MainWindow* ui = nullptr
    }
    
    //and then:
    void main() {
       ... //other stuff
       MainWindow w;
       w.show();
       TcpCon tcp;
       tcp.setUi(&w); //now you can use ui inside tcp via ui->
    }
    


  • Hi Chris,

    thank you very much I was able to solve it :). I tried something similar before but I think I have done sth. wrong with all these pointer stuff..

    Cheers


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.