Results 1 to 8 of 8

Thread: General application structure with Qt

  1. #1
    Join Date
    Aug 2011
    Location
    Finland
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question General application structure with Qt

    Yo!

    I have a couple of years of experience in maintaining Qt programs, but have never created a non-trivial application from scratch. Now I suddenly find myself confused by requirements for the topmost architecture. Once I get up to speed I'm sure I'll be able to add stuff to the software without problems, but how do I structure the entire thing, starting right from main.cpp?

    This exact question was asked in this thread, but the answers are not satisfactory. I do not want to use the full, heavy model/view approach, with all the Qt classes that have "Model", "Delegate" or "View" in the name. Yet obviously I want some kind of wall of separation between UI and program logic.

    Currently main() instantiates QApplication, QMainWindow and my own general manager class. The mainwindow holds some common controls directly as member variables and will hold instances of sub-views with their own UI components. The manager opens configuration files, handles communication protocols and generally makes things happen. Anything in the UI part of the program will be subclassed from QWidget while anything in the logic part will inherit QObject.

    Thus, the questions are --
    • Where should I instantiate the mainwindow and manager classes?
    • Does either of the instances hold a pointer to the other?
    • How do I implement an interface between them -- where will the connect() commands go?

    Pray grant succour, grand architectural design is quite vexing.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: General application structure with Qt

    There are an infinite number of ways to do this. Without knowing more about your application, it's impossible to narrow things down much. However, a common approach that works well for many applications is to simply encapsulate your manager class within your mainwindow class. This makes establishing connections and other communications between the two parts simple, and keeps them separate at the same time. Just be careful not to intermingle UI code and computational, model or manager code to the point where they're difficult to separate. Sometimes it is useful to create small "bridge" classes that add a further abstraction layer between these components; other times, this is overkill.

  3. #3
    Join Date
    Aug 2011
    Location
    Finland
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General application structure with Qt

    Quote Originally Posted by SixDegrees View Post
    However, a common approach that works well for many applications is to simply encapsulate your manager class within your mainwindow class.
    Well, having the mainwindow instance simply keep the sole instance of the manager class as a member variable would certainly work, but somehow I see this as an inversion of authority... the manager is supposed to be the king in the application.

    QMainWindow does not have to be instantiated in main() does it? That is, I can make a class that merely owns both the manager and the main window?

  4. #4
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: General application structure with Qt

    This is very application specific, so I can just share my own experience with you. This is from a network application interacting with a server:

    I have two top level classes:

    // the view
    MyMainWindow: public QMainWindow

    // the controller (+ lightweight model)
    MyApplication: public QObject

    with the following responsibilities:

    MyMainWindow:
    • creates widgets
    • forwards almost any request to MyApplicaiton (through signals)

    MyApplication:
    • creates and shows MyMainWindow
    • handles signals from MyMainWindow
    • delegates tasks to other top level classes (singletons or private members)

    and my main() looks like this:

    Qt Code:
    1. int main(int argc, char** argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. MyApplication application;
    6. application.create();
    7.  
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: General application structure with Qt

    Take a look at Pure MVC framework. If you dig about you will find a couple of c++ versions*of this framework. I am using a c++ version (I had to modify it a bit) For my Qt project. It simplifies development of large apps so much, I couldn't imagine developing without it!

  6. #6

    Default Re: General application structure with Qt

    Since the introduction of QML, you can save vast amount of development time by only stepping into C++ for certain tasks that require it.
    We've prepared a comprehensive guide how to create and structure QML-driven apps, check it out here: https://v-play.net/apps/avoid-cpp-models-qt

    Cheers,
    GTDev

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,942
    Thanks
    231
    Thanked 619 Times in 609 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: General application structure with Qt

    Resurrecting long-dead posts with your advertising spam will get you bounced off the forum.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,252
    Thanks
    36
    Thanked 1,508 Times in 1,459 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: General application structure with Qt

    Quote Originally Posted by GTDev View Post
    Since the introduction of QML, you can save vast amount of development time by only stepping into C++ for certain tasks that require it.
    This is true to a certain extent, it depends on definitions of "require it".

    Inexperienced developers often interpret it in the sense of "if performance requires it", forgetting about the much more common requirement of stability of code quality.

    JavaScript is good for prototyping and demos, essentially code that needs to be written in a short time but won't have to be maintained.

    Once code reaches the state of when it is expected to updated (fixed and/or new features) it becomes far more sensible to do it in C++.
    There are far more tools available to analyze and check C++ code and a lot of JavaScript tooling is specific to its use in web browsers.

    Even without special tools, the C++ compiler alone can prevent a lot of misery due to its far stricter handling of identifiers.
    For example the following JavaScript code has a simple typo a C++ compiler would have generated an error for:
    Qt Code:
    1. function add(param1, param2) {
    2. var result = 0;
    3.  
    4. resut = param1 + param2;
    5.  
    6. return result;
    7. }
    8.  
    9. console.log("add(1,2)=" + add(1, 2));
    To copy to clipboard, switch view to plain text mode 
    A JavaScript engine will happy accept "resut" as a new local variable, leading to the rather surprising result that "1+2" is "0".

    The equivalent C++ code
    Qt Code:
    1. #include <iostream>
    2.  
    3. int add(int param1, int param2) {
    4. int result = 0;
    5.  
    6. resut = param1 + param2;
    7.  
    8. return result;
    9. }
    10.  
    11. int main() {
    12. std::cout << "add(1, 2)=" << add(1, 2) << std::endl;
    13.  
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 
    leads to
    Qt Code:
    1. /tmp/test.cpp: In function ‘int add(int, int):
    2. /tmp/test.cpp:6:5: error: ‘resut’ was not declared in this scope
    3. resut = param1 + param2;
    4. ^~~~~
    5. /tmp/test.cpp:6:5: note: suggested alternative: ‘result’
    6. resut = param1 + param2;
    7. ^~~~~
    8. result
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 12
    Last Post: 26th June 2011, 11:26
  2. Replies: 4
    Last Post: 5th September 2010, 10:08
  3. Planning the general structure of a C++ program
    By dohzer in forum General Programming
    Replies: 4
    Last Post: 20th August 2010, 14:53
  4. General: Structure of a Qt mainwindow program
    By kaizimir in forum Newbie
    Replies: 6
    Last Post: 7th September 2009, 18:33
  5. Qt Programming in general
    By Holy in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2008, 10:35

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.