I am making an application with three libraries ITK VTK
and Qt
. Since I want all functionality in the event loop and connection of signals and slots so I defined all functionality under QMainWindow definition. Now mainwindow object became bulky and I tried to make different classes for different functionality but then all things messing in main.cpp
Idea of modularization is missing. Can you suggest how should be structure of a program?

- 548
- 2
- 6
- 19
1 Answers
I rarely have anything more in main.cpp
than creating and showing my QMainWindow
, then calling app.exec()
.
If your QMainWindow
starts to become bulky, then it means you should probably organize this code into different coherent classes (instead of moving it to main.cpp
). The basic idea is to change a code that looks like this:
class MainWindow : public QMainWindow
{
public:
void doSomething();
void foo1();
void foo2();
void foo3();
void bar1();
void bar2();
void bar3();
private:
// ...
}
To a code that looks like this:
class Foo:
{
public:
void doSomething1();
void doSomething2();
void doSomething3();
private:
// ...
}
class Bar:
{
public:
void doSomething1();
void doSomething2();
void doSomething3();
private:
// ...
}
class MainWindow : public QMainWindow
{
public:
void doSomething();
private:
Foo foo_;
Bar * bar_;
// ...
}
How do you choose the classes Foo
and Bar
and which members of MainWindow
you transfer to these new classes depends of course of your context. But the idea is to organize your code smartly so that each class have a "responsibility", and then MainWindow
can delegate each work to the class that is responsible for this work. Like in a house, there are electricians for managing electricity, plumbers for managing water, etc... and the responsibility of the house keeper is only to call the appropriate guys to do the appropriate job. In your case, you probably want a class to perform any work related to ITK
, and a class to perform any work related to VTK
, then if they become bulky also, subdivide these classes.

- 7,015
- 4
- 30
- 59
-
It is what I want to get rid off – QT-ITK-VTK-Help Jun 13 '13 at 07:06
-
1And my answer explains how you get rid of it, isn't it? If you compare the two versions, you can see that now the class `MainWindow` is much cleaner that before :) (The classes `Foo` and `Bar` can be moved to different files). Inevitably, the code has to be located somewhere, it will not be invented by the compiler. Your role is to organize it in different classes (and hence files) to make it easier to write, read, and maintain. For more information, you can read this: http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep#Architecture – Boris Dalstein Jun 13 '13 at 07:22
-
Your answer is what I did actually. But I am not sure it is right way or not cause overall things relay on a single object. Well Thanks for link. – QT-ITK-VTK-Help Jun 13 '13 at 09:12
-
@QT-ITK-VTK-Help: So the answer is: Yes, you did the right way :) To go further with you reasoning, eventually, everything is executed sequentially by the processor. But what is important is not avoiding that at the end of the day everything is relayed on a single object, but that these tasks are organized in smaller tasks, more manageable by the programmer. – Boris Dalstein Jun 13 '13 at 09:20
-
Is it not like my GUI is tightly coupled with functionality? – QT-ITK-VTK-Help Jun 13 '13 at 10:20
-
2No, because the other classes that you define can be completely independent from the GUI. For instance, you can create a class `Point`, `Curve`, or `AddressBook` completely independently from any QtGui class. But then, it is fine that your `MainWindow` just "uses" these classes as tools (hence there are no coupling). The most important idea here is that your new classes can live independently than your `MainWindow`, and can even be reused for other projects, which proves that they are not "coupled". – Boris Dalstein Jun 13 '13 at 10:29
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31712/discussion-between-qt-itk-vtk-help-and-boris) – QT-ITK-VTK-Help Jun 13 '13 at 10:38