Results 1 to 3 of 3

Thread: Why are functions like exec() declared static in the QApplication class?

  1. #1
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Why are functions like exec() declared static in the QApplication class?

    I am looking at qt-x11-opensource-src-4.3.0-snapshot-20070315\src\gui\kernel\qapplication.h and I find that at line 235 the exec() function has been declared static. So are many other functions like font(), palette() and even setFont(), setPalette() etc.

    My understanding about static functions in C++ is that their only need is when a member function of a class can/needs to be called without an instance of that class, like for example the currentDateTime() function which is a member of the QDateTime class but can be called without an instance of that class. In fact, this function has no particular meaning when called upon an instance of that class since it does not create, access, modify or delete that instance in any way. So I can understand why currentDateTime() is a static member of QDateTime.

    But I do not understand how exec(), font(), palette(), setFont(), setPalette() etc can be static when to my understanding these can be called only upon an instance of the QApplication class. Although for a Qt application there is only once instance of a QApplication class, the exec() and other functions are by definition dependent on this particular instance and have no meaning when separated from this instance. For example, setFont() can set the default application font only for the current application running and that means only an existing instance of QApplication. If there is no existing instance of QApplication, whose default font will setFont() modify?

    Therefore I feel that it is meaningless to declare these functions as static. Please anyone confirm or disprove this hypothesis of mine. If my hypothesis is confirmed I can file a bug. Thank you.
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why are functions like exec() declared static in the QApplication class?

    Well, I would say the reason why it is done this way is because QApplication is a kind of "Singleton". At any point in time, there may not be more than one QApplication in a Qt application. (Which kind of makes sense :-)

    Things like QApplication::setFont() and bretheren have an interesting meaning too if not QApplication is yet allocated; You could create a functions "setupDefaults()" that sets fonts/styles/pallettes and this kind of stuff, and be able to call it at any time. If for example no QApplication object exists yet, those settings will be stored away and used as soon as the next QApplication is instanciated.


    In addition to those, you will find static accessors such as QApplication::focusWidget() those functions are nice to have as a static because they are usefull in places where it would be a hassle to ship a pointer to the current application to (and using qApp or QApplication::instance() everywhere just would look ugly. ;-) Besides the semantics for that call is always valid: if no QApplication instance is instanciated there simply is no focusWidget which means the function returns 0.


    Now, this is probably not "by-the-book" object oriented design, but I would call it rather sensible. Mind you, QApplication is a very special case, so you will not find these design choices in too many places (and you should not of course) but here I think they make life just a little bit easier, so I am ok with it :-)



    Have a nice day

  3. #3
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Why are functions like exec() declared static in the QApplication class?

    Thanks for that reply. I guessed as much that QApplication being a only-one-instance-at-a-time class would be the reason. But really, though I can conceptually understand your idea of a setApplicationDefaults() function which sets defaults for an application which is going to be started, who is going to write such a function if he/she is not going to create a QApplication object later?

    There is only the small convenience of not having to use qApp or "this" whenever you have to set or read the properties of a QApplication. But then, we have no choice about using "this" or QWidget * or whatever whenever we have to set or read the properties of another QObject. So it might well have been the same for QApplication. When we write so many "this"-es for those objects, what's with a few for a QApplication object?

    Anyway, your explanation is valid and I accept it. If anyone else has any other interesting perspectives I (and I presume you too) would be glad to hear it of course.
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50
  3. Static functions and class members
    By Raistlin in forum General Programming
    Replies: 5
    Last Post: 22nd December 2006, 10:00
  4. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26
  5. Qt 4.1 and KDE 3.5.1 on OSX 10.2.8
    By Ptero-4 in forum Installation and Deployment
    Replies: 6
    Last Post: 6th February 2006, 02:44

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.