PDA

View Full Version : C++/QT Design



xmeister
23rd March 2009, 08:11
Hi, hoping to get some help re. code design.
I've been codeing on and off for a few years now, started with HTML, Javascript, PHP then moved onto C, C++. Also dabbled in VB and Delphi. Just so you know where I'm kind of up to I know all the basics of C++ and have a basic understanding of encapsulation, polymorphism etc but I'm still struggling a bit getting everything sorted in my head where I don't have to stop and think about it before I do something.

So anyway my issue is really with OOP I guess and how to organise my code accross multiple files. I'm at the stage where I want to start doing larger projects but just never sure of the correct way to do things. I should mention I'm using QTCreator for UI design and codeing.

eg. If I have a project consisting of a widget stack, maybe 5-10 pages, each interacting with each other and drawing data from an SQlite database. SO I have files MyWindow.ui, MyWIndow.cpp, MyWindow.h and I have the MyWindow class.

Should I -
- keep each page of the widget stacks code to it's own .cpp and header files. ie a page_1_class.cpp, page_1_class each with it's own class. If so what is the best way to access the base class which is the class all the widgets belong to.

- keep all the pages code in the base class (declare all my functions etc in the base header file) and just put the definitions in there own .cpp files.

- keep all the QT related things (anything that directly interacts with a QWidget) to the base class and just farm out the data manipulation stuff.

- Is it acceptable to create individual widget projects for each page and then just add these projects to the widget stack on a mainwindow.

Any help or push in the right direction appreciated.
Cheers

PaceyIV
29th March 2009, 11:55
I also have interest in this topic.
Anyone can help us?

ComaWhite
30th March 2009, 12:16
My personal opinion and I do this for all my projects. I put all my classes in their own .h/.cpp no matter how big or some, or convience. Because when the classes grow and you stop adding more classes, it's a pain to scroll through it and nitpick at it.

And by having them in their own, I know exactly where to go and easily add/modify them. I also put all my function in alphabetic order.

I but it functions in visibility order:

public:
protected:
private:

And a-z in their own section. And seperate the functions from the variables. But now I'm using pimpl to keep my classes in BC mode.

Lykurg
31st March 2009, 08:10
I totally agree with ComaWhite: One class one pair of cpp/h files. Alphabetical order of the function is nice, but since my editor as a good outlining windows, I am too lazy to to so:o

But I you struggle how exactly organize your code, just have a look how others keep their code. Look for example at the projects of KDE (kate, dolphin, etc). Thay have a good code design with an standard.

xmeister
31st March 2009, 12:40
Thanks for the reply's.

I think your right, I need to study some code by people who know what they're doing. I'll have a look and see how I go :).

Thanks again.

ComaWhite
1st April 2009, 02:58
You can click on my project link in the bottom and see mine. You have to click more at the top and go to my git repo, be sure to look at the experimental branch. Since its the one I'm working in right now

xmeister
2nd April 2009, 02:39
Thanks again for the help,
I checked out some various app's/projects incuding yours ComaWhite and it's all starting to come together. I was struggling with organisation which browsing through other code really helped with.
My other main problem was, I think I was still in procedural mode and even though I've understood the concepts of OOP for quite a while I wasn't really applying it appropriately. This coupled with a lack of organisation in my code meant once a project started to grow I was getting lost in my own code.
I've had a couple "AHA" moments now though after seeing how others do things, going back over some basics and just coding some small projects and concentrating on making sure I utilise objects properly and more than I was.

Cheers
Xmeister