Nominate our 2022 Qt Champions!

Access single instance from all class

  • I am trying to access awesome object from another class . I declare this in main()

    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        QtAwesome* awesome = new QtAwesome(&app);
        app.setWindowIcon(awesome->icon( "clipboard" ));
        Login w;;
        return app.exec();

    Now how do I access awesome object from Login or another class ? Is it best way to initialize a class in main() if I want to access from another class ?

  • Moderators

    Pass the pointer to Login class in some method.

    If you want to have a global, single instance object, take a look at Singleton pattern. Just a word of warning: singletons add global state to the application, which makes testing and bug fixing harder (sometimes) - I recommend keeping singleton numbers as low as possible.

  • Hi @hiren21

    You can pass awesome pointer to login class when you create a object of login.

    login w(awesome);


Log in to reply