PDA

View Full Version : QAbstractListModel



Archa4
9th February 2011, 07:29
I tried many times, and now i have this declaration:
class ListModel : public QAbstractListModel

{
Q_OBJECT
public:
ListModel(QVector<QStringList> &my2darray, QObject *parent = 0)
: QAbstractListModel(parent), vector(my2darray){}

int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;

private:
QVector<QStringList> &vector;
};

I would like to know, what can i return as QVariant? Only strings? i need to return a custom_widget i created (it consists of 4 labels). I tried this code:
QVariant ListModel::data(const QModelIndex &index, int role) const

{
if (!index.isValid())
return QVariant();

if (index.row() >= 20)
return QVariant();

if (role == Qt::DisplayRole)
{
int a = index.row();
return Custom_Widget(vector[a][0], vector[a][1], vector[a][2], "");
}
else
return QVariant();
}

But it didn't worked. How should I alter my code to get it working?

Lykurg
9th February 2011, 08:33
You can return evertything as a QVariant. See the documentation of QVariant, Q_DECLARE_METATYPE and qRegisterMetaType.

wysota
9th February 2011, 08:46
If your intention is that you are going to show this custom widget in a view in place of the model index for which you return the widget then it will not work. One thing is that your syntax is not correct (you'd have to return a pointer to the widget which would result in an instant memory leak) and second is that the views will simply not understand what you want from them. What is the final result that you want to achieve?

Archa4
9th February 2011, 09:27
OK, so I want to create a List of Widgets. Widgets are custom (I already wrote a class where they can be created). I need to implement this using the MVC principle.
I try to fill that list with my custom widgets, that are provided by Custom_Widget class (where i can create new custom widgets), but i have no idea how to do it.
Could someone please tell me, do i need to subclass the QItemDelegate to show my custom widgets in the list, if yes, then how do i get the delegate to show my custom widgets?
I want to know, if there even is a need to subclass QAbstractListModel.

I found few examples, where i learned, that it is possible to use QListWidget/QTableWidget and delegates, according to MVC principle (i think one example is Star Rating in demos). If i need to subclass QItemDelegate, do i need to set the Editor, even though i don't need to edit the data that will be shown in the list?

If you could tell me, i would also want to know in theory what should i do to achieve my task? Example: u need main.cpp, some_delegate.cpp, and custom_widget.cpp. In those files u'll have to implement these things... and so on.

wysota
9th February 2011, 11:39
A "list" is a data model. It can live well wthout a view. Returning widgets from the model rarely makes sense. If you want to show a list of widgets then use QScrollArea instead of the model-view framework.

Archa4
9th February 2011, 13:47
The problem is My superior told me to do this one using MVC..

stampede
9th February 2011, 14:15
The problem is My superior told me to do this one using MVC..
Told you to do that using Qt Model-View framework, or told you to implement MVC pattern (using Qt) ? :)

Archa4
9th February 2011, 14:16
I think they are the same :)

stampede
9th February 2011, 14:24
I think they are the same
Not really ;) Design pattern is something abstract, it's an idea - you can have the same design pattern implemented in different programming languages.
Have you heard about "Gang Of Four" (Gamma, Helm, Johnson, Vlissides) ? I really recommend this book: Design Patterns: Elements of Reusable Object-Oriented Software (http://my.safaribooksonline.com/book/software-engineering-and-development/patterns/0201633612)
Here is nice explanation of MVC pattern: MVC link (http://www.enode.com/x/markup/tutorial/mvc.html)

wysota
9th February 2011, 14:43
I think they are the same :)

Qt ItemViews is not an implementation of MVC. It is based on MVC but lacks the controller component. Thus it is commonly called "model-view framework". I don't know what your superior told you but either you are approaching the problem from a wrong end (basing your ui on widgets instead of something else) or your superior is not aware of the fact that what you are doing can't be done efficiently in Qt's model-view framework. You should have a serious conversation with your superior about it.