Garmaine Staff asked 1 year ago

I am writting this post because I have a structural problem with Qt5 and my C++ application. My app contains around 4000 lines of codes, which is quite big for me as it is my first real project. But now I am struggling to keep the code clean, as I didn't use any design pattern like MVC at the beginning.

I have heard about MVC and model view programming with qt5 but it is still very abstract to me.My question refers to my project structure which is the following:

  • I have a UI class, which inherits from QMainWindow. This class has several custom member objects, which correspond to the each different pages of the application. I use a QStackedWidget to switch between those classes, and get the right display.
  • On the other hand, I have a singleton class called "Controller" which contains both data and the calculations.

Thus I try to make all the computation from the user interaction in the Controller class, but I still have to update the data from the UI side, which seems weird to me.

For example, let's say in my Page1UI class, I have lots of QLineEdit field, like name, age, adress etc.. If I want to update the corresponding fields name_, age_, and adress_ on the Controller class side, when the user click the submit button, I would write this slot in the PAGE1UI.cpp:

void PAGE1UI::updateData(){
   //Controller is a singleton
   Controller::getInstance()->setName(this->lineEditName_.getText());
   Controller::getInstance()->setAge(this->lineEditAge_.getText());
   Controller::getInstance()->setAdress(this->lineEditAdress_.getText());
}

And this slot would be connected to the submit button signal clicked(). So how should it be designed in a good way? It seems to be very UI dependent. Hope that I was clear enough!

Thanks.

Antoine