1

I am implementing an application that is like Facebook in Qt5. For now in my App I just want to view profiles of people, add new ones and view detailed info of a profile. I decided on a MVC archetecture for my app. I want to know how the views/controllers can interact with the model classes

This is what I have so far: I have a few view/controller classes (as QT seems to mash both into one) which are:

  • SingleProfileView (QWidget) <- show 1 profile
  • MainView (QWidget)<- Main view which has navigation options ..etc
  • ProfileListView (QWidget)<- Which shows the list of all profiles in my app at the moment

Each view is implemented using stacked widgets

  • ProfileView (QWidget) has a stacked widget inside containing widgets ProfileListView and SingleProfileView
  • Mainview (QWidget) has a stacked widget that contains ProfileView

These are my data classes

  • ProfileModel <- data of 1 profile
  • ProfileListModel <- List of all profiles (contains many profiles)
  • AppModel <- Contains the ProfileList

The AppModel loads up data from a SQL DB and DB functions are abstracted.

One last class which is the "main class"

  • MyApp <- class that has all the views and data (I would think of this as the controller)

How can I pass data between the AppModel and the views?

I was thinking of having the model class be a singleton class and all the views that need the model class just get the instance of it?

What I am talking about can be found here: https://doc.qt.io/archives/qq/qq10-mvc.html

My concern is this coupling the model and view/controller classes too much? This seems like playing with a global variable.

rjasi
  • 11
  • 2
  • Hi, @rjasi Welcome to StackOverflow. Please explain your problem in short and simple. – TheParam Feb 05 '19 at 05:50
  • Hi @TheParam. Sorry if my explanation was too detailed. My problem is how should the View/Controller classes and my Model classes communicate with each other in QT? – rjasi Feb 05 '19 at 05:58
  • Views, models and delegates communicate with each other through the common interfaces. What you need is implementing your own models (override abstract functions on your own) and pass them to the corresponding views. Views are designed to handle model data. – vahancho Feb 05 '19 at 07:20

1 Answers1

0

Instead of using raw QWidgets, you'll be much better off using the abstract classes (interfaces?) provided by Qt meant for manhandling the MVC architecture. Namely, QAbstractItemView and QAbstractItemModel.

For instance,

#include <QAbstractItemModel>
class MyModel : public QAbstractItemModel
{
    // ...
};

#include <QAbstractItemView>
class MyView : public QAbstractItemView
{
    // ...
};

Once you inherit from the two classes, you can instantiate them and call QAbstractItemView::setModel to connect your model and view. Qt does all in the background so you won't need to worry about it.

For instance,

MyModel *model = new MyModel(parent);
MyView *view = new MyView(parent);
view->setModel(model);        // magic

For convenience, Qt provides some basic/partially-implemented models and views. For example, Qt has the QAbstractListModel, QAbstractTableModel, and QStandardItemModel along with QListView and QTableView. So you can choose to use these instead of going into the trouble of inheriting from the abstract item model/view.

See also: Model/View Programming

TrebledJ
  • 8,713
  • 7
  • 26
  • 48