The view displays items according to their indices - it'll display items [0 - columnCount()] x [0 - rowCount()] fetching the data using QAbstractItemModel::data. If you wish to implement your own view instead of using a default one, you have to fetch and display the data yourself using whatever means you find usefull. In general it means reimplementing all pure abstract methods and painting on the viewport (reimplementing the paintEvent). The concept of delegates is at your disposal if your view can easily differenciate between items - you can then use the standard item view infrastructure and implement the delegate (or use a default one) - in such situation data fetching will take place in the delegate (you'll be given proper indexes by the view).
By the way, I think your "data" method is invalid.You are returning a QPair, but you need to make it a QVariant and QPair is not castable to QVariant (and it doesn't make much sense to return QPair as a single role anyway, it's better to use mutliple roles and split the pair).
Are you sure you need such a complex data type? Maybe QVariantMap would be enough? Or maybe a custom structure? Let's see...
QList <QPair<QString,QPair<QImage,QPair<QStringList,QStringList> > > > mainMap;
QList <QPair<QString,QPair<QImage,QPair<QStringList,QStringList> > > > mainMap;
To copy to clipboard, switch view to plain text mode
This equals to:
QList<QPair<QString, QPair<QImage, customType> > > mainMap;
// where
struct customType {
};
QList<QPair<QString, QPair<QImage, customType> > > mainMap;
// where
struct customType {
QStringList first;
QStringList second;
};
To copy to clipboard, switch view to plain text mode
If you continue to split the pairs like this, you'll get:
QList<customType> mainMap;
// where
struct customType {
};
QList<customType> mainMap;
// where
struct customType {
QString first;
QImage second;
QStringList third;
QStringList fourth;
};
To copy to clipboard, switch view to plain text mode
You can notice that all above types are castable to QVariant, so you can also use:
QList<QList<QVariant>>
QList<QList<QVariant>>
To copy to clipboard, switch view to plain text mode
but QList<QVariant> is also castable to QVariant, so you can use
QList<QVariant>
QList<QVariant>
To copy to clipboard, switch view to plain text mode
instead of that complex QPair combination.
Of course it is cleaner to do it using "customType", like so:
//...
private:
QList<customType> _data;
};
//
int row = index.row();
if(role==Qt::DisplayRole){
switch(index.column()){
case 0: return _data.at(row).first;
case 1: return _data.at(row).second;
case 2: return _data.at(row).third;
case 3: return _data.at(row).fourth;
}
}
// ...
}
class MyModel::QAbstractTableModel{
//...
private:
QList<customType> _data;
};
QVariant MyModel::data(const QModelIndex & index, int role = Qt::DisplayRole ) const {
if(!index.isValid()) return QVariant();
//
int row = index.row();
if(role==Qt::DisplayRole){
switch(index.column()){
case 0: return _data.at(row).first;
case 1: return _data.at(row).second;
case 2: return _data.at(row).third;
case 3: return _data.at(row).fourth;
}
}
// ...
}
To copy to clipboard, switch view to plain text mode
This way you get a four column model consisting of a string, an image and two lists of strings in a much cleaner way than returning the whole bundle at once.
You can then in the view fetch the data and render it:
//...
QModelIndex givenindex;
// this is the item you have to render int row = givenindex.row();
QString first
= givenindex.
sibling(row,
0).
data(Qt
::DisplayRole).
toString();
QImage second
= givenindex.
sibling(row,
1).
data(Qt
::DisplayRole).
toImage();
QStringList third
= givenindex.
sibling(row,
2).
data(Qt
::DisplayRole).
toStringList();
QStringList fourth
= givenindex.
sibling(row,
3).
data(Qt
::DisplayRole).
toStringList();
option.initFrom(this);
myCustomRenderer(painter, option, first, second, third, fourth);
//...
//...
QModelIndex givenindex; // this is the item you have to render
int row = givenindex.row();
QString first = givenindex.sibling(row, 0).data(Qt::DisplayRole).toString();
QImage second = givenindex.sibling(row, 1).data(Qt::DisplayRole).toImage();
QStringList third = givenindex.sibling(row, 2).data(Qt::DisplayRole).toStringList();
QStringList fourth = givenindex.sibling(row, 3).data(Qt::DisplayRole).toStringList();
QStyleOptionViewItem option;
option.initFrom(this);
myCustomRenderer(painter, option, first, second, third, fourth);
//...
To copy to clipboard, switch view to plain text mode
Bookmarks