Results 1 to 3 of 3

Thread: Lots of .cpp files or just one?

  1. #1
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Lots of .cpp files or just one?

    Hi All,

    I've noticed that most projects I compile from source have a lot of .cpp files, many of them with only a few functions in them. One of my own projects is getting a little more complex and I was wondering what the best way to proceed would be.

    I have a project and am now adding a new part to it. To accommodate the new stuff I am adding a new tab to an existing QTabWidget, which take up the majority of the main window.

    I could write a new class that creates the new tab and handles most of the things inside it. The only thing it would need from the main program is some of the settings information, and the main program would need to pass info to this class once in a while.

    Should I be doing it this way, or should I simply add the tab in the ui file and put the code for in in the main class on the same .cpp file as all the other stuff?

    Also.. sorry for sticking a second question on a thread.. but I know this has been answered, I just wanted to make sure I understood it.

    In QT3 Designer I used to add custom slots. From what I understand, with QT4 I should be using connect() statements in the initializing function for my class?

    Thanks for any help in advance.

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Lots of .cpp files or just one?

    Encapsulating the parts of your application that have minimal interconnectivity is what OOP is all about. Sounds good to me.

    However, this won't work correctly if you create the whole main window (including tabs and content) with Designer. Designer will put the member-vars of all widgets (of all tabs) in a single class. If you want to separate the content of the different tabs in the way you describe, you have to create this content by code.

    Each page of the QTabWidget has its own container widget. A pointer to this widget is the only thing your new class would need to create the content inside it. I imagine your MainWindow class would create the tabs, the new class instances and passes them the correct container widget (possibly through the constructor).

    As for what you call 'custom slots', it worked the same way in Qt 3 as it now does in Qt 4. With the connect() function. The difference is that you can't write your own code with Designer anymore. And thus you can't connect a signal with a custom slot with the mouse like you could before.

    Edit: It just occurred to me that you can simply build custom composite widgets with Designer. If you design each tab-widget separately in Designer, they will still be nicely separated. So you don't have to code them by hand.
    Last edited by Michiel; 16th June 2007 at 14:51. Reason: spelling error
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    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: Lots of .cpp files or just one?

    Quote Originally Posted by December View Post
    I've noticed that most projects I compile from source have a lot of .cpp files, many of them with only a few functions in them. One of my own projects is getting a little more complex and I was wondering what the best way to proceed would be.
    Splitting your project into multiple .cpp files can reduce compilation time, because you have to recompile only changed files. On other hand, with this approach compiler can't perform some cunning optimisations.

    Quote Originally Posted by December View Post
    with QT4 I should be using connect() statements in the initializing function for my class?
    There are also automatic connections.

Similar Threads

  1. No .h or .cpp Files Generated
    By gwb in forum Qt Tools
    Replies: 7
    Last Post: 21st April 2007, 19:26
  2. Replies: 5
    Last Post: 22nd September 2006, 08:04
  3. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28
  4. uic not creating seperate .h and .cpp files
    By Kapil in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2006, 10:43

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.