PDA

View Full Version : Lots of .cpp files or just one?



December
15th June 2007, 15:07
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.

Michiel
16th June 2007, 09:31
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.

jacek
18th June 2007, 17:31
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.


with QT4 I should be using connect() statements in the initializing function for my class?
There are also automatic connections (http://doc.trolltech.com/4.2/designer-using-a-component.html#automatic-connections).