[Qt-interest] Returning data from models
phil prentice
philp.cheer at talktalk.net
Tue Apr 7 19:25:40 CEST 2009
Thanks Benjamin & Scott for your help...I will look at your suggestions and
have a play.
Thanks again
Phil
On Tuesday 07 April 2009 14:08, you wrote:
> Well i think you should take a look at
> QVariant::fromValue(const T& value);
>
>
> in conjunction with:
>
> Q_DECLARE_METATYPE
>
> Hope i understood you right
>
> Benjamin Sonnemann
>
> On Tue, Apr 7, 2009 at 2:53 PM, phil prentice
<philp.cheer at talktalk.net>wrote:
> > Hi
> > If this is more a C++ question then a QT one, then I apologise up front
> > now
> > and you can ignore me!!! I have a declared a class derived from
> > QAbstractListModel. The code works fine, but as a novice I am just
> > trying to
> > improve what I have. My problem is that I have a list data associated
> > with my model and I am using the roles UserRole+n to access the data.
> > Here is some of the code:-
> >
> > class BottomArrowModel : public QAbstractListModel
> > {
> > Q_OBJECT
> >
> > public:
> > BottomArrowModel(QObject *parent = 0);
> >
> > /* Over-ride functions. */
> > bool setData(const QModelIndex &index, const QVariant &value,
> > int role = Qt::EditRole);
> > bool insertRows(int position, int rows,
> > const QModelIndex &index = QModelIndex());
> > bool removeRows(int position, int rows,
> > const QModelIndex &index = QModelIndex());
> > int rowCount(const QModelIndex &parent = QModelIndex()) const;
> > QVariant data(const QModelIndex &index, int role = Qt::DisplayRole)
> > const;
> > inline void setZoomFactor(int zoom) {m_zoomFactor = zoom;
> > emit layoutChanged(); };
> > inline int zoomFactor() {return m_zoomFactor;};
> >
> > private:
> > int m_zoomFactor; /* 1, 2 or 4 */
> > QStringList instNameList; /* My data consists of a list of items each
> > */
> > QStringList facNameList; /* one having an entry for the 6 items on */
> > QStringList remarkList; /* the left. */
> > QStringList commentList;
> > QStringList componentList;
> > QStringList signalList;
> > };
> >
> >
> > bool BottomArrowModel::setData(const QModelIndex &index,
> > const QVariant &value, int role)
> > {
> > if (index.isValid())
> > {
> > switch(role)
> > {
> > case Qt::UserRole: /* Instrument name. */
> > instNameList.replace(index.row(), value.toString());
> > break;
> > case Qt::UserRole+1: /* Facility name. */
> > facNameList.replace(index.row(), value.toString());
> > emit dataChanged(index, index);
> > /* Let the views know of changes..data changed signal emitted.
> > */ break;
> > case Qt::UserRole+2: /* Remark. */
> > remarkList.replace(index.row(), value.toString());
> > emit dataChanged(index, index);
> > /* Let the views know of changes..data changed signal emitted.
> > */ break;
> > case Qt::UserRole+3: /* Comment. */
> > commentList.replace(index.row(), value.toString());
> > emit dataChanged(index, index);
> > /* Let the views know of changes..data changed signal emitted.
> > */ break;
> > case Qt::UserRole+4: /* Component. */
> > componentList.replace(index.row(), value.toString());
> > emit dataChanged(index, index);
> > /* Let the views know of changes..data changed signal emitted.
> > */ break;
> > case Qt::UserRole+5: /* Signal. */
> > signalList.replace(index.row(), value.toString());
> > emit dataChanged(index, index);
> > /* Let the views know of changes..data changed signal emitted.
> > */ break;
> > default: /* Ignore anything else. */
> > return false;
> > }
> > return true;
> > }
> > return false;
> > }
> >
> >
> > /* Use model to obtain data. */
> > QVariant BottomArrowModel::data(const QModelIndex &index, int role) const
> > {
> > if (!index.isValid())
> > return QVariant();
> >
> > if (index.row() >= facNameList.size())
> > return QVariant();
> >
> > switch(role)
> > {
> > case Qt::UserRole: /* Instrument name. */
> > return instNameList.at(index.row());
> > break;
> > case Qt::UserRole+1: /* Facility name. */
> > return facNameList.at(index.row());
> > break;
> > case Qt::UserRole+2: /* Remark string. */
> > return remarkList.at(index.row());
> > break;
> > case Qt::UserRole+3: /* Comment string. */
> > return commentList.at(index.row());
> > break;
> > case Qt::UserRole+4: /* Component string. */
> > return componentList.at(index.row());
> > break;
> > case Qt::UserRole+5: /* Signal string. */
> > return signalList.at(index.row());
> > break;
> > default: /* Ignore anything else. */
> > return QVariant();
> > }
> > }
> >
> > My concern lies in that the above works OK, but what if I had say 100
> > different items to update and they were all of a different type. I am
> > sure that there is probably a better way, maybe using QLIst<T>?
> >
> > The thing is ..is that data and setData uses the object QVariant to pass
> > the
> > data in and out. Is there a way to pass a class object (myClass) as
> > Qvariant
> > and store these objects in a list QList<T>. Then my delegate and view
> > etc could access the model the once to get the data for one item in the
> > list in the form of myClass. That class could be used to access the
> > individual items
> > of data in the model. I could also probably set the data in a similar
> > read/write way.
> >
> > What I have got just does not seem very clever...can anybody educate me
> > here,
> > or point me to an example?
> >
> > Thanks
> >
> > Phil Prentice
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at trolltech.com
> > http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list