How to set data (Qt::UserRole) into QSqlQueryModel column?
-
wrote on 2 Feb 2012, 17:22 last edited by
I have QSqlQueryModel that handling data , im trying to set Qt::UserRole to column
but i can't figure a way to do it i have implement the data method :
basically what i want is to hide the Qt::DisplayRole of ndex.column() 4 and set it to Qt::UserRole
@QVariant MyListSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
QVariant valueEmpty = "";
int j = index.column();
if (value.isValid() && role == Qt::DisplayRole && j== 4 )
{QModelIndex LinkIndex = QSqlQueryModel::index(index.row(),4);
setData(LinkIndex,value,Qt::UserRole); // this is not working and gives me erorrreturn valueEmpty;
}
else
{
return value;
}
}@it give me this error that i know what it is the implement method is not const
@ error C2662: 'MyListSqlModel::setData' : cannot convert 'this' pointer from 'const MyListSqlModel' to 'MyListSqlModel&'@but what is the right way to do it ?
-
wrote on 2 Feb 2012, 23:48 last edited by
data() is a const method. That means that you cannot modify the data of your class in that method (there are some tricks, but that's not recommended here). setData() modifies the data, this is the reason why you cannot call setData() within data().
IMHO, that is not needed. I'm not 100% sure if I understood correctly what you need, but I think it would be sufficient to just return the value of the other role:
@
QVariant MyListSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role == Qt::DisplayRole && index.column() == 4 && value.isValid())
return QSqlQueryModel::data(QSqlQueryModel::index(index.row(),4), Qt::UserRole);
else
return value;
}
@This returns the regular value for all columns but the 5th (0 based indexes).
If you're on column 4 and the DisplayRole value is valid, return the value of the UserRole instead, otherwise the original value.
-
wrote on 3 Feb 2012, 07:32 last edited by
Hi Thanks for the replay . its the right direction .. but i need to "Set"
the DisplayRole value into Qt::UserRole value ,
so later in the itemDelegate i could Query the data from the Qt::UserRole.
now i just return empty value into Qt::UserRole. -
wrote on 3 Feb 2012, 08:12 last edited by
I doubt that QSqlQueryModel supports setting user data for any role. This class is designed to represent an SQL query result. I think it doesn't even implement setData, but you'd have to check that in the source. setData is implemented for QSqlTableModel, but that class will try to store the data in the database. I think neither is what you want.
I would investigate a proxy model solution, that will allow you to "overlay" additional data on any underlying data model. You'd have to implement it yourself though.
-
wrote on 3 Feb 2012, 08:25 last edited by
Hi
thanks for replay ,
in the end all i want to do is to decorate item in the QSqlQueryModel Tableview
so i now using allso ItemDelegate from QStyledItemDelegate
that will overwrite the Text in the Item. i hope this will work .
from what i do now it is not removing the old text dont know why ..
this is the paint method in the item delegate:@if (index.isValid())
{
int j = index.column();
if(j==4)
{
QString headerText_UserRole = index.data(Qt::UserRole).toString() ;
QString headerText_DisplayRole = index.data(Qt::DisplayRole).toString() ;
painter->save();
QFont font = QApplication::font();
font.setBold(true);
painter->setFont(font);
QRect headerRect = option.rect;painter->drawText(headerRect,"");
painter->restore();
}
}QStyledItemDelegate::paint(painter, option, index);@
1/5