Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QApplication in each class?



  • Hi

    I'm a newbie at QT and this is my first post so forgive the stoopid question.

    Do I need to declare QApplication in each class that needs it or can I pass it as a reference from the calling class (... main())

    I've tried with this sort of class declaration

    @void myclass(QApplication app)@

    ...and also

    @void myclass (QApplication *app)@

    ... but they both produce compile errors, either at the declaration line or further on when I try to use it..

    Obviously I can declare this in each class but then I have to pass (argc, argv)

    Am I missing something obvious?

    Cheers

    Sam



  • Hi Sam, and welcome to DevNet!

    You only need to use this:

    @
    #include <QApplication>

    int main(int argc, char *argv[])
    {

    QApplication a(argc, argv);
    grafOkapi w;
    w.show();
    
    return a.exec&#40;&#41;;
    

    }

    @

    in your main.cpp!

    What's your version of Qt and your OS?



  • Hi,
    The most important rule is: THERE MAY ONLY BE 1 QAPPLICATION!!!
    To make it easy for argument use and other cool QApplication stuff (translator e.g.) you may access the QApplication by the qApp pointer. Just include <QApplication> or QtCoreApplication and you may access all members.
    Also the arguments of the program are accessible via the qApp pointer.
    BTW: Welcome to Qt!!



  • Ahh, thanks guys

    Clochydd, I am actually trying to put everything in classes outside of main (including the show() and exec()) so I can just have a simple class call, and in one of my various attempts to make it work it it was failing at those points.

    Jeroentje, I have come across qApp but didn't realise that it was the pointer. I'll give that a whirl at lunchtime and see how I get on (learning this is my lunchtime relaxation... I really must get out more ;-)

    Cheers

    Sam



  • ... sorry, forgot to say

    My OS is Kubuntu and I'm using QT4. I will upgrade to QT5 before doing anything more.

    Sam



  • In my experience, Qt5 will work perfectly with Kubuntu!
    Happy coding, Sam :)
    Please mark your thread as solved by editing your first post and add a prepending [SOLVED] to the title


Log in to reply