Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top