Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QApplication in each class?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QApplication in each class?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samopn
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        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!!

        Greetz, Jeroen

        1 Reply Last reply
        0
        • S Offline
          S Offline
          samopn
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            samopn
            wrote on last edited by
            #5

            ... sorry, forgot to say

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

            Sam

            1 Reply Last reply
            0
            • C Offline
              C Offline
              clochydd
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved