vinay patel
vinay patel

Reputation: 175

Changing selection behavior of a QTableView

I am using QTableView in Qt and I have one table in which each cell has different text color. I have selection behavior select entire row. But when I select any row, text color changes to white for the selected row. I don't want to change text color while row is selected. I want original color to be displayed when I select any row.

I tried to use stylesheet but it is also changing entire row text color.

I am posting here sample code

QTableView * pQTableView  = new QTableView();
QStandardItemModel *model = new QStandardItemModel(5,3);


    pQTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    pQTableView->setModel(model);
    for(int row = 0;row < 5;row++)
          for(int column = 0; column < 3; column++)
          {
             QModelIndex index1= model->index(row,column);

              QVariant  value("Swaminarayan");
              model->setData(index1, value,Qt::DisplayRole );

          }
    QModelIndex index1= model->index(0,0);
    QVariant Obj(Qt::green);
    model->setData(index1,Obj,Qt::TextColorRole );

     index1= model->index(0,1);
    QVariant Obj1(Qt::red);
    model->setData(index1, Obj1,Qt::TextColorRole );
    pQTableView->show();

Here you can see color of first cell is green and second sell is red once we select first row color changes to white.

Upvotes: 0

Views: 4657

Answers (2)

Zaid
Zaid

Reputation: 688

You could also just set the selection mode to NoSelection for the TableView. You could then use the itemClicked() signal to get the item index and set its color as you wish for each item in the row. Something like this:

connect( myTableView,
    SIGNAL( clicked( const QModelIndex &) ), 
    this, 
    SLOT( onItemClicked(const QModelIndex &) ) ) ; 
//....

void DataModel::onItemClicked(const QModelIndex &index)
{
  //get the clicked item
  QStandardItem *clickedItem = myDataModel->itemFromIndex(index);

  // get the row
  int selectedRow = clickedItem->row();

  // for each col change the color as you want
  for(int c = 0, colCount = myDataModel->columnCount(); c < colCount; ++c)
  { 
    QStandardItem *itemToChange = myDataModel->item( selectedRow, c);
    QBrush brush;
    brush.setColor(Qt::red);
    itemToChange ->setData(brush, Qt::ForegroundRole);
  }
}

Upvotes: 1

Georgii Iesaulov
Georgii Iesaulov

Reputation: 69

It seems you should implement your own table view based on QTableView and reload data() function:

QVariant YourTableViewClass::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()) {
        return QVariant();
    }
    /* any other checks here */

    switch(role) {
        case Qt::BackgroundRole:
            return QColor(/* background colour here */);
        case Qt::ForegroundRole:
            return QColor(/* foreground colour here */);
        case Qt::DisplayRole:
            /* any other actions here */
        default:
            break;
    } /* switch(role) */

    return QVariant();
}

For Qt::BackgroundRole and Qt::ForegroundRole you can implement your colours.

Please see http://qt-project.org/doc/qt-5.0/qtcore/qabstracttablemodel.html for QAbstractTableModel for reference. Hope it helps.

Upvotes: 0

Related Questions