Set background color to a cell of a tableView
-
I guess proxyIndex is equal to QModelIndex &index?
-
I have been reading to see if I can figure out how to get the table to be highlighted correctly after it is sorted but I have not been able to figure it out. I drew a blank with having four models as suggested but I did find something else that might work....but it isn't. I think I should be able to capture the signal from when the user clicks on the header and emit a signal to reload the table. Here is what I tried but when I debug, the signal does not get called.
@QHeaderView *horizHeader;
QSqlRelationalTableModel printModel;
MyProxyModel proxy;printModel= new QSqlRelationalTableModel (this);
printModel-> setTable (mTableName);
printModel-> setRelation (2, QSqlRelation("name", "id", "LName"));printModel->select();
proxy = new MyProxyModel(this);
proxy->setSourceModel(printModel);
ui->printView->setModel(proxy);
horizHeader= ui->printView->horizontalHeader(); connect(horizHeader, SIGNAL(sortIndicatorChanged (int) ),
proxy,SIGNAL(dataChanged ( ) ));
@
I am pretty sure it is because of the wrong syntax for the dataChanged signal, but I am not sure how to make it work. Will this work? Or am I going down the wrong path? Is the model method better? If so, how do I make it work? I have no clue on how to start. -
Hello,
I implemented the data function, but my tableview stays empty. The Grid is shown, but without any data inside. Here is the code:data implementation
@QVariant proxymodel::data(const QModelIndex &index, int role) const
{
QModelIndex sourceIndex;
if (!index.isValid())
return QVariant();if (role == Qt::BackgroundRole ) {
sourceIndex = mapToSource(index);
if (sourceIndex.data(Qt::EditRole).toInt() == 1) {
return QVariant(Qt::blue);
}
}
return QVariant();
}@the filltable function
@
void Window::fillTable()
{
model = new QSqlRelationalTableModel( tableView );
model->setTable("Protokoll");
proxy = new proxymodel(tableView);model->setRelation(12,QSqlRelation("Status", "ID", "Status")); model->setEditStrategy(QSqlTableModel::OnFieldChange); tableView->setItemDelegate(new QSqlRelationalDelegate(tableView)); proxy->setSourceModel(model);
tableView->setModel(proxy);
model->select();
}
@
What is wrong? -
It looks to me as if you have not finished implementing the data() function in your proxy. You check for the role to see if it is the background role and correctly return blue if the data matches your criteria.
However, you have not handled the case where the data does not match your criteria. You have also not handled any of the cases where the role is not the background role.
I think it needs to look more like this:
@
QVariant proxymodel::data(const QModelIndex &index, int role) const
{
QModelIndex sourceIndex;
if (!index.isValid())
return QVariant();// We only wish to override the background role if (role == Qt::BackgroundRole ) { sourceIndex = mapToSource(index); if (sourceIndex.data(Qt::EditRole).toInt() == 1) { return QVariant(Qt::blue); } else { return QSortFilterProxyModel::data( index, role ); } } // Let the base class handle all other cases return QSortFilterProxyModel::data( index, role );
}
@ -
Indeed, which can be further simplified to:
@
QVariant proxymodel::data(const QModelIndex &index, int role) const
{
QModelIndex sourceIndex;
if (!index.isValid())
return QVariant();// We only wish to override the background role if (role == Qt::BackgroundRole ) { sourceIndex = mapToSource(index); if (sourceIndex.data(Qt::EditRole).toInt() == 1) { return QVariant(Qt::blue); } } // Let the base class handle all other cases return QSortFilterProxyModel::data( index, role );
}
@:-)
-
It works now! Thank you!!
-
Sorry for double post, but I have a few questions remaining. One point is that the ItemDelegate does not work anymore.
@tableView->setItemDelegate(new QSqlRelationalDelegate(this));@And last but not least I cant access every cell in my table - It's just not editable?!