Results 1 to 5 of 5

Thread: First serious project using Qt - I need some guidelines

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default First serious project using Qt - I need some guidelines

    Hey everyone,

    This past semester, I did a project for my programming class, using the C language with the Allegro graphical library. Anyway, I've been reading up and doing Qt tutorials for some weeks, and now wish to embark on a real project: porting that project to C++ and Qt.

    Well, I've been having a hard time starting out. I have a general vision of how I want the interface to be, but I'm still a bit new to the whole OO paradigm, so I'm afraid I'm approaching the problem from the wrong angle. I tinkered with it for a bit, but just decided it was a better idea to start from scratch.

    I have my code hosted at github: https://github.com/JPaquim/ATC-Qt
    The C/Allegro version is in the c-allegro folder, I'm sorry about the Portuguese comments and readme, but the professor wanted it in Portuguese.

    The project is a game like some out there, based on the monitoring and control of airplanes entering and leaving an airspace. So far I've made a few classes, namely a MainWindow (in order to have a menu bar and tool bars, along with a status bar) from a Trolltech tutorial, a MyWidget (QWidget which will be the central widget in the MainWindow) where I'll put the interface, an Airspace (which will be the heart of the program, a QWidget that will control the airspace graphical representation, containing the various entry gates, runways and airplanes.

    I'm having a hard time figuring out how to progress right now... I want to take care of the loading of the airspace's configuration file first, and then take care of drawing the elements loaded from that file (gates and runways), but what is the correct way of doing that? Should I just load from the configuration file in the Airspace class's constructor?

    Any help and comments would be welcome,
    Cheers

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: First serious project using Qt - I need some guidelines

    Configuration could probably a few hours worth of lectures on its own. Do you have many stages of configuration in your app? Now is a good time to think about the process of configuration, and how many different steps there are, how many different classes will be affected. If there are multiple things that need to be configured, is order of configuration important?

    There should probaly be separate class(es) to do the file handling and data manipulation of the config. Some related class would then forward the relavent data on to Airspace ()
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: First serious project using Qt - I need some guidelines

    Oh, it's just a simple configuration file with a description of the airspace, a map if you want to call it that. Right now I just want to be able to load it in, and populate the various variables with its contents. I'll think about making it possible for the user to create their own maps in the future, right now I just want to load it.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: First serious project using Qt - I need some guidelines

    In that case, just stick to a class for reading the file and returning some data. The owner of this class should know where to pass the data to.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,329
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: First serious project using Qt - I need some guidelines

    It probably is not appropriate to do the configuration in the Airspace constructor, but in an Airspace::onConfigureAirspace( const AirspaceConfiguration & ) slot. In the MainWindow, in the slot that handles the triggered() signal from the QAction attached to the "Load Configuration" menu item, you retrieve the name of the configuration file, pass it to the bool AirspaceConfiguration::load( const QString & fileName ) method, and then emit an airspaceConfigurationChanged( const AirspaceConfiguration & ) signal:

    Qt Code:
    1. // MainWindow.h:
    2.  
    3. class MainWindow :: public QMainWindow
    4. {
    5. O_OBJECT;
    6.  
    7. // usual constructor stuff, etc.
    8.  
    9. signals:
    10. void airspaceConfigurationChanged( const AirspaceConfiguration & config );
    11.  
    12. protected slots:
    13. void onLoadConfigurationTriggered();
    14.  
    15. protected:
    16. Airspace airspace;
    17. AirspaceConfiguration currentConfig;
    18. };
    19.  
    20. // MainWindow.cpp:
    21.  
    22. MainWindow::MainWindow( QWidget * parent )
    23. : QMainWindow( parent )
    24. {
    25. // set up menus, actions, etc
    26. // ...
    27. connect( this, SIGNAL( airspaceConfigurationChanged( const AirspaceConfiguration & ) ), &airspace, SLOT( onConfigureAirspace( const AirspaceConfiguration & ) ) );
    28.  
    29. // more setup if needed
    30. }
    31.  
    32. void MainWindow::onLoadConfigurationTriggered()
    33. {
    34. QString fileName = QFileDialog::getOpenFileName();
    35. if ( !fileName.isEmpty() )
    36. {
    37. if ( currentConfig.load( fileName ) )
    38. emit airspaceConfigurationChanged( currentConfig );
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

    This way, all of the different parts of the program are separated, each with a well-defined function, and with minimal inter-dependence on other parts of the program. Communicating by signals and slots (in the Qt way) means that the two partners on each end don't need to know anything about each other, only that they share the connection with its well-defined protocol.

    By making an AirspaceConfiguration class, you now have a way to change the configuration independently of other parts of the game. Presumably, the Airspace class itself is responsible for implementing all the rules (like, two planes can't be at the same place at the same time, on the same runway headed towards each other, etc.). You could then create a GUI for designing a new AirspaceConfiguration independently of the rest of the program, so instead of only being able to load a configuration from a file, you could change it interactively and emit the same signal.

    Likewise, I presume you would implement a series of other classes to represent Aircraft, Gate, Runway, Taxiway, Terminal, or whatever else makes sense in the context of the game.

Similar Threads

  1. How to convert QT Project to CMake project?
    By Kevin Hoang in forum Qt Programming
    Replies: 2
    Last Post: 29th March 2011, 09:48
  2. Error handling guidelines?
    By bitflyer in forum Newbie
    Replies: 1
    Last Post: 2nd June 2010, 16:09
  3. Replies: 1
    Last Post: 3rd December 2009, 23:34
  4. Help regarding guidelines
    By Nishant in forum Newbie
    Replies: 4
    Last Post: 20th October 2009, 19:49

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
  •  
Qt is a trademark of The Qt Company.