PDA

View Full Version : [Qt Creator] Coding style.[Help]



"BumbleBee"
6th February 2011, 12:14
Hey guys,here I'm again with some questions! :D

Well,I have been reading the "C++ GUI with Qt" book and it's not too hard.
I understand the most parts of the code...

But when I don't use the book and try to code something small myself(like a calculator),I have difficulties in 1)where to put specific parts of the code(like:i know that i will put decleration in a .h file and definition in .cpp,but I get confused when it becomes complicated..),are there any rules about what to put where?

2)And one more thing:As I understood,for each new widget/dialog/window i make I have to make 2 new files(.h & .cpp for it)as well,right?

Thank you and please explain if possible where different parts of the code should go!:o

helloworld
6th February 2011, 17:03
These questions are more related to C++ in general, and not so much to Qt/Qt Creator really, but that's ok anyway. ;)

Like you said; typically when you create a new class, you create a header file (.h) containing the class/function declarations + data members, and a C++ file (.cpp) containing the body of each function (their definitions). It is more or less standard practice to have one class declaration per header file and its definitions in a corresponding source file, but it is not strictly required. For example, it is common to find smaller helper objects grouped together in the same file with other objects.

You can also place the implementation of a function in the header file itself (you see it in a lot of examples and tutorials) -- and many times it is perfectly valid to do so, e.g., short getters and setters in the header is common and in the case of template classes, it is actually the only way to do it (simplified statement, but anyway...). However, as classes get longer and more complicated, it is generally a good idea to try to do as much "code separation" as possible.

Not sure if that was of any help to you.

nish
6th February 2011, 17:38
Hey guys,here I'm again with some questions! :D

Well,I have been reading the "C++ GUI with Qt" book and it's not too hard.
I understand the most parts of the code...

You have started reading that book too early. Take a good C++ book first. Then make a mid size simple program in C++. Come back to this book.

"BumbleBee"
6th February 2011, 18:23
helloworld[B]:Yeah,that was nice.I got what you mean.thnx
[B]nish:Hey man,what is the point in doing so?
Do you think my question relies on C++ experience?I don't think so,because Qt does have different tecniques!

wysota
6th February 2011, 19:24
nish:Hey man,what is the point in doing so?
Do you think my question relies on C++ experience?I don't think so,because Qt does have different tecniques!
No, you are completely wrong. Qt "inherits" everything from C++ as you are using C++ to code with Qt. Qt doesn't have any "different techniques". So to refer to your original question - you don't need to have a .h and a .cpp file for a new widget, you don't need to put any declarations in any .h files nor anything like that. You need to follow rules defined by your C++ compiler that is compliant with the C++ standard. You need to familiarize yourself with C++ before doing anything serious with Qt, otherwise you'll end up where you are now - as soon as you step beyond some book or tutorial, you won't know where to go next. Also remember that C++ is also just a tool, just like Qt. You need something more to be a programmer than to learn C++ syntax. This is exactly the same for any other programming language. So after a C++ book I'd recommend some book or course about programming as such. Learning how computers work internally is also a good thing to do.

"BumbleBee"
6th February 2011, 20:00
But what do you mean familiarize myself with C++?
Do I have to code code/read everything to move on?
Well,I think that all the things I know are enough..and others hae told me that it's ok.

wysota
6th February 2011, 20:19
But what do you mean familiarize myself with C++?
I mean to understand why and how things work with C++. For instance why you declare things on heap and not on stack and what consequences it has. Or what are the weaknesses of exceptions in C++. Or what exactly happens if you don't implement your own constructor/destructor/copy constructor/assignment operator for a class. Things like that. Then it comes to the process of building an application - what exactly happens to the source code until it becomes the final binary you can execute.


Well,I think that all the things I know are enough..and others hae told me that it's ok.
If you are asking the things you are asking then you probably don't know enough.

SixDegrees
6th February 2011, 20:48
But what do you mean familiarize myself with C++?


He means you need to learn C++.

Michael Druckenmiller Sr
13th October 2011, 20:56
Back to Coding Style...

Right now... LEARN...

Get it to work, first. Worry about "Style" later.

*IF* you were in a commercial setting your company will probably cite a Style Document or have one of their own.

But, at this point you are too far down on the learning curve to worry about style, as such.

Now, Syntax... That's a different animal altogether... :)