TableView cell showing data and image
-
Im new to QT so theres probably something that i am missing here, but i have created the following class
@
// Log for database
// DateTime, CurrentUser, LogType, DescriptionLogModel::LogModel(QSqlDatabase database, QObject *parent)
: QSqlRelationalTableModel(parent, database)
{
connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SIGNAL(countChanged()));
connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SIGNAL(countChanged()));}
QVariant LogModel::data(const QModelIndex& idx, int role) const
{
QVariant value= QSqlQueryModel::data(idx, role);// Column 1 is for icon if (idx.column()==1 && role==Qt::DecorationRole) { QString iconstring = QString("%1").arg(idx.data().toString()); QIcon icon = QIcon(QPixmap(iconstring)); value= icon; } return value;
}
@in my main class I set the header data as follows
@
m_logModel->setTable(QLatin1String("Logs"));
if (m_logModel->lastError().isValid())
return false;
m_logModel->setHeaderData(1,Qt::Horizontal,"",Qt::DecorationRole);
m_logModel->setHeaderData(2,Qt::Horizontal,QObject::tr("Date"));
m_logModel->setHeaderData(3,Qt::Horizontal,QObject::tr("User"));
m_logModel->setHeaderData(4,Qt::Horizontal,QObject::tr("Description"));
@The problem i am having is that cell 1 is showing both the returned icon as well as the result from that database.
what am i doing incorrect? -
Hi and welcome to devnet,
Do you mean that you get both the text from the database and the icon your are providing ? If so it's normal, the decoration doesn't hide the text.
You would need to either not return the text when asks for the display role, add a QIdentityProxyModel that does that (keeps your model clean) or create a QStyledItemDelegate that doesn't paint the text.
Hope it helps
-
Yes you can, but are you sure you won't need it elsewhere ? That's why you should rather use QIdentityProxyModel or a custom delegate. Doing so your model will be easily reusable.