PDA

View Full Version : Model/View one index.column as hidden UserRole



doitux
26th July 2008, 11:25
Hi

I wanna switch from item-based QTreeWidget to model/view QTreeView.
I have a 3-column-model. I need to display model-column 0 as Qt::UserRole of view-column 0, model-column 1 as Qt::DisplayRole of view-column 0 and model-column 2 as Qt::DisplayRole of view-column 1.

Before in the treewidget i did it like this:

QTreeWidgetItem *item = new QTreeWidgetItem();
item->setData(0, Qt::UserRole, index.column(0).toString());
item->setData(0, Qt::DisplayRole, index.column(1).toString());
item->setData(1, Qt::DisplayRole, index.column(2).toString());


The model is build like this:


void myLessonNotesModel::refresh()
{
setQuery("SELECT noteid, date, content FROM note ORDER BY date DESC");
setHeaderData(0, Qt::Horizontal, QObject::tr("Datum"));
setHeaderData(1, Qt::Horizontal, QObject::tr("Notiz"));
}

Can anybody tell me how to connect the model columns to different view columns in different roles?

Best regards
Felix

wysota
26th July 2008, 11:41
Reimplement the data() method and return proper data from within there. Reimplement columnCount() to report the proper number of columns. Alternatively implement a very simple proxy model based on QSortFilterProxyModel where you will do the same without touching your base model.

doitux
26th July 2008, 13:08
Thx. I will try QSortFilterProxyModel.