PDA

View Full Version : MVC or not



Archa4
7th February 2011, 13:46
I will describe what's happening in my program.
I have a main.cpp, list.cpp and custom_widget.cpp.
In main.cpp, as usually it is, i have only:
list->show();
In list.cpp i create a list widget. Then i get information from XML, and call custom_widget.cpp to create custom widgets and then i add them to the list.
In custom_widget.cpp i only create new custom widgets.

My question - is this an example of MVC or not? I think that something is wrong. My supervisor told me i should do as follows:
in list.cpp i need to fill an array with data and pass it to custom_widget.cpp
In custom_widget.cpp i need to create custom widgets and pass them back.
Then in List.cpp i need to fill the list.
Is he right?

franz
7th February 2011, 13:52
QListWidget uses Model/View internally, but the way I understand you have it set up is probably not MVC. Read the Model/View (http://doc.trolltech.com/latest/modelview.html) documentation for more information on model/view setups.

Archa4
7th February 2011, 13:55
I already read those, but they are written about QListView and such, and not about QListWidget... I cannot understand what part of my realization is wrong.

high_flyer
7th February 2011, 13:58
I have a main.cpp, list.cpp and custom_widget.cpp.
The file names in your project are not relevant to the issue.
One can only guess what is going on (as is code) in any of the files, and if they have classes in them, and what these classes do an how.

Nothing and everything in what you said could be related to MVC, but probably nothing, since you did not say anything about data models.

Did you read the Qt MVC docs (http://doc.trolltech.com/4.7/model-view-programming.html)?


Is he right?
If he is the boss, he is right! ;)
But seriously, you are talking about design lines, but provide to little (relevant) information to help you.

Archa4
7th February 2011, 14:03
The thing is, as i understood, to set the data in those QListView u need to use delegates, but using QListWidget u can use something like:

listWidget->setItemWidget(listWidget->item(a), new Custom_Widget(titleString, dateString, aboutString, pictureString));
Is there anyway to use models while using QListWidget?

franz
7th February 2011, 14:05
Hmm. QListWidget is a so called 'convenience class'. It is a QListView with it's model (a QAbstractListModel derivative) built into it. Internally, the QListWidget is definitely Model/View, but the code using it probably isn't. To make your implementation according to MVC, you would need to implement your own QAbstractItemModel derivative and feed it to a view. MVC is really about splitting up responsibilities: wherever you have to worry about displaying (view), do only displaying, wherever you have to worry about the data (model), do only data management. Using QListWidget more or less forces you in the 'wrong' i.e. non-MVC direction.

Also, using delegates is different from setting item widgets. Moreover, using delegates is probably the better course of action to take.