Results 1 to 5 of 5

Thread: Reponsabilities of QApplication and QMainWindow classes

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Reponsabilities of QApplication and QMainWindow classes

    Hi everybody,

    What are the main differences of responsability of QApplication and QMainWindow classes ? To give you more details, I wonder what kind of code can be implemented in QApplication and what kind must not be.

    I ask thoses questions because I am working on an application, not developped by me, and many functions are coded in a class inherited from QApplication (slots, signals, timers). The application runs but I wonder if it is well architectured and if that kind of way of coding can be problematic.

    Thanks in advance for your answers.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reponsabilities of QApplication and QMainWindow classes

    From the docs

    The QApplication class manages the GUI application's control flow and main settings.
    It contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application's initialization and finalization, and provides session management. It also handles most system-wide and application-wide settings.
    For any GUI application that uses Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any time.
    The QMainWindow class provides a main application window, with a menu bar, dock windows (e.g. for toolbars), and a status bar.
    Main windows are most often used to provide menus, toolbars and a status bar around a large central widget, such as a text edit, drawing canvas or QWorkspace (for MDI applications). QMainWindow is usually subclassed since this makes it easier to encapsulate the central widget, menus and toolbars as well as the window's state. Subclassing makes it possible to create the slots that are called when the user clicks menu items or toolbar buttons.
    Hope this cleared your doubt

  3. #3
    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: Reponsabilities of QApplication and QMainWindow classes

    To explain a bit more. QApplication object is always available in your application through the static QCoreApplication::instance() call (or qApp macro), making it a good placeholder for objects, which otherwise would need to be global. This gives an additional benefit, because sometimes when you construct some global objects (for example timers), you get a message, that you can't construct such an object without a QApplication object. This is a way to bypass such messages.

    The main window object should only hold data related to the main window itself, whereas the application object can hold data related to different components of the application. To go a little further, the application object can wrap the main window object too, which could make the resulting code clearer and simpler, look at an example of a main() function with the main window (be it QMainWindow or any other widget) wrapped into the application:
    Qt Code:
    1. #include "myapplication.h"
    2. int main(int argc, char **argv){
    3. MyApplication app;
    4. return app.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 

    You can also implement your application object to return various objects used by different components as static pointers, for example a pointer to the main window. This makes access to the widgets you need simpler and less error prone. Consider this snippet:
    Qt Code:
    1. MyWidget *wid = qobject_cast<MyWidget*>(parent()->parent()->parent()->parent());
    To copy to clipboard, switch view to plain text mode 
    This could be substituted by:
    Qt Code:
    1. MyWidget *wid = MyApplication::myWidget();
    To copy to clipboard, switch view to plain text mode 

    or even as a regular function call - without static pointers (some people don't like them):
    Qt Code:
    1. MyWidget *wid = qApp->myWidget();
    To copy to clipboard, switch view to plain text mode 

    To summarise, in my opinion you can put in the application object everything you consider "application wide" and in the main window only those things, which are actually used by the main window or its direct subclasses. A different approach comes when ypu are lazy -- you put everything in the main window and use QApplication without subclassing, as it is a little more time consuming to make a good design out of it.

  4. The following 2 users say thank you to wysota for this useful post:

    Cesar (1st October 2006), yellowmat (4th September 2006)

  5. #4
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Reponsabilities of QApplication and QMainWindow classes

    Thanks Wysota,

    Your answer contains all details I need.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reponsabilities of QApplication and QMainWindow classes

    One more thing: QMainWindow is just a widget and you can create many different instances, as for QApplication --- there can be only one

Similar Threads

  1. Replies: 18
    Last Post: 22nd February 2006, 20:51

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.