Results 1 to 9 of 9

Thread: Subclassing QApplication for global members and methods.

  1. #1
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Subclassing QApplication for global members and methods.

    Hello,

    In the application I'm currently developping, I need to access (through setMode and getMode methods), from any classes, to a variable 'mode' that shows in which mode the application is.
    I want other classes to be warned about changes of the value of this variables.
    That's why I subclasses QApplication.
    I cannot though access to this variable, this :

    qDebug()<<QApplication_ac::instance()->getMode(); //here, the problem !

    give the error :

    class QCoreApplication' has no member named 'getMode'
    qDebug()<<QApplication_ac::instance()->getMode();


    How can I solve my problem ?

    Many thanks for answer.

    Here is the code :

    qapplication_ac.h :

    Qt Code:
    1. #ifndef QAPPLICATION_AC_H
    2. #define QAPPLICATION_AC_H
    3.  
    4. #include <QApplication>
    5.  
    6. enum Mode {NOTHING=0,
    7. PICKING_DETAIL,
    8. PANNING,
    9. ROTATION,
    10. PICKING_POINT,
    11. ABOUT_TO_CLOSE,
    12. POLYGON_FINISHED
    13. };
    14.  
    15. class QApplication_ac:public QApplication
    16. {
    17.  
    18. Q_OBJECT
    19.  
    20. public:
    21. QApplication_ac(int &argc, char *argv[]); //constuctor
    22. Mode mode;
    23. Mode getMode(){ return mode;}
    24. void setMode(Mode modeToSet);
    25.  
    26. signals:
    27.  
    28. void modeChanged(Mode newMode,Mode oldMode); //other classes will be connected to this signal
    29.  
    30. };
    31.  
    32. #endif // QAPPLICATION_AC_H
    To copy to clipboard, switch view to plain text mode 

    qapplication_ac.cpp :

    Qt Code:
    1. #include "qapplication_ac.h"
    2.  
    3. QApplication_ac::QApplication_ac(int &argc, char *argv[]):
    4. QApplication(argc, argv)
    5.  
    6. {
    7. mode=NOTHING;
    8. }
    9.  
    10. void QApplication_ac::setMode(Mode modeToSet){
    11.  
    12. emit modeChanged(modeToSet,getMode());
    13.  
    14. mode=modeToSet;
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclassing QApplication for global members and methods.

    You need to cast the pointer to your type as QCoreApplication::instance() returns a pointer to QCoreApplication. For convenience you can redefine the qApp macro to return your type directly.

    Qt Code:
    1. #ifdef qApp
    2. #undef qApp
    3. #define qApp qobject_cast<QApplication_ac*>(QCoreApplication::instance())
    4. #endif
    5.  
    6. ... = qApp->getMode();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Subclassing QApplication for global members and methods.

    So simple answer ! The year 2015 will start well b'cause you !
    Many thanks.

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclassing QApplication for global members and methods.

    How come the code has compiled?
    Qt Code:
    1. void QApplication_ac::setMode(Mode modeToSet){
    2.  
    3. emit modeChanged(modeToSet,getMode());
    4.  
    5. mode=modeToSet;
    6. }
    To copy to clipboard, switch view to plain text mode 
    modeToSet is an enum and it does not have a method getMode(). The compiler should protest. As to qApp, another simple method is creating your own replacement for qApp, say TheApp (a global variable):
    Qt Code:
    1. QApplication_ac *TheApp = nullptr;
    To copy to clipboard, switch view to plain text mode 
    and setting it in the QApplication_ac ctor to this. Now, you can call TheApp->getMode().

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subclassing QApplication for global members and methods.

    modeToSet is an enum value.
    getMode() is a function that returns an enum value.
    modeChanged() is a signal that takes two enum values as arguments: these are provided separated by a comma (not a period).

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QApplication for global members and methods.

    The actual question is: why derive from QApplication?
    Why make a tight coupling with a totally unrelated class?
    Why not just have an application or library specific singleton?

    Subclassing QApplication for something like that just makes it impossible to use any such code in unittests (they create the application object via macros).

    Cheers,
    _

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclassing QApplication for global members and methods.

    Quote Originally Posted by anda_skoa View Post
    Subclassing QApplication for something like that just makes it impossible to use any such code in unittests (they create the application object via macros).
    It's not that bad, you can create your own application object if you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Subclassing QApplication for global members and methods.

    Quote Originally Posted by wysota View Post
    It's not that bad, you can create your own application object if you want.
    Of course you can, it has a virtual destructor
    I am just saying that it is usually a bad idea.

    Cheers,
    _

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Subclassing QApplication for global members and methods.

    Quote Originally Posted by anda_skoa View Post
    Of course you can, it has a virtual destructor
    I am just saying that it is usually a bad idea.
    I meant regarding the unit tests.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Hello members
    By Murari_ctls in forum General Discussion
    Replies: 1
    Last Post: 17th December 2013, 01:43
  2. Replies: 2
    Last Post: 1st August 2011, 07:30
  3. How to use the Qt3 Support members in Qt4?
    By Cutey in forum Qt Programming
    Replies: 9
    Last Post: 11th August 2008, 07:27
  4. subclassing QApplication
    By nupul in forum Newbie
    Replies: 2
    Last Post: 19th April 2006, 20:29
  5. <QtGui/QApplication> vs. <QApplication>
    By seneca in forum Qt Programming
    Replies: 5
    Last Post: 25th January 2006, 11:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.