Qt development without object-oriented programming
-
wrote on 16 Jun 2022, 13:02 last edited by
Hello,
I develop an application with Qt and sometimes I wonder if I'm doing it correctly.My program is quite linear, I press a button and I execute all the actions that follow to reach my goal. But I'm struggling with the object-oriented approach, dividing each action into distinct classes and managing the private/public attributes, the access methods to my attributes. I often need attributes from one class to another.
In short, my program contains only classes and I wondered if this is normal or if it is common to use C++ as an imperative paradigm (like C).
Can I do without object-oriented programming in C++ with Qt ?
Thanks for your advices.
-
Hi,
That's rather an architecture issue. You do not need to create different objects for each and every action you want to execute.
Usually you would start by creating units that represent different aspects of your application and then the objects that matches them.
-
wrote on 20 Jun 2022, 07:35 last edited by Mick_1
Sorry I didn't answer before,
What do you mean? I don't have a class by actions but per group of actions.
For example, a class that takes care of the link with the GUI, a class for reading and extracting data in a given folder, a class that generates reports and a "Process" class.When we develop in object-oriented mode, we usually reuse our classes several times? Technically, I only need one instance of each class and a linear operation.
Except from the class that bridges the gap between the GUI (QML) and C++, is it a mandatory to code only with classes?
-
Sorry I didn't answer before,
What do you mean? I don't have a class by actions but per group of actions.
For example, a class that takes care of the link with the GUI, a class for reading and extracting data in a given folder, a class that generates reports and a "Process" class.When we develop in object-oriented mode, we usually reuse our classes several times? Technically, I only need one instance of each class and a linear operation.
Except from the class that bridges the gap between the GUI (QML) and C++, is it a mandatory to code only with classes?
wrote on 20 Jun 2022, 08:35 last edited by@Mick_1
Your list of classes is not unreasonable, at least initially. Over time you may want to divide these down into multiple classes and/or add other classes.It does not matter that classes can be re-used several times, but your examples may not be and will only be instantiated once. That is OK, it does not obviate the benefits of writing classes or mean you should not do so because you will only have one object of that class.
There are too may advantages to using classes to list them all. What is your alternative? To have every function free/global? To dream up naming conventions to prevent similar names clashing?
Yes, classes may have to expose
public
methods for the outside world to access. Makepublic
just what needs to be public, makeprivate
/protected
what does not. Judicious architectural design of your program should mean you do not have too many public attributes. If you find every class is accessing loads of things in every other class you may have a design which could be improved. Using Qt's signals and slots can help in keeping classes quite separate and not needing to know about each directly. -
wrote on 20 Jun 2022, 09:06 last edited by
Thanks for your feedback.
I'm happy with what I've done so far, I think I've coded some pretty clean stuff respecting the few points you listed in your post.
I'm an embedded programmer and so I'm mostly used to code in C and so in procedural. That's why sometimes I feel like I'm not doing it right and I'd like to go back to procedural even in C++.
I understand, I'm going to train with the object-oriented philosophy, I like it anyway, but I wanted to know if it's not a received idea to code necessarily in OOP when using C++.
-
Thanks for your feedback.
I'm happy with what I've done so far, I think I've coded some pretty clean stuff respecting the few points you listed in your post.
I'm an embedded programmer and so I'm mostly used to code in C and so in procedural. That's why sometimes I feel like I'm not doing it right and I'd like to go back to procedural even in C++.
I understand, I'm going to train with the object-oriented philosophy, I like it anyway, but I wanted to know if it's not a received idea to code necessarily in OOP when using C++.
wrote on 20 Jun 2022, 09:18 last edited by JonB@Mick_1
Well, C++ is just a superset of C so you do not have to to do any OOP if you really don't want to --- except e.g. where the outside world demands you do so, by passing objects around.Give C++ a go. It does not take long to realise that the ability to compartmentalise into classes is an improvement over plain C. BTW, if it helps any,
class
in C++ is technically the same thing as/based on/extended fromstruct
in C. Objects are juststruct
s, with a bunch of member variables and functions defined inside them.
1/6