Reputation: 41
I want selected Cells to have a different background color. By default there is only a thin underline in the selected cell.
I've tried this:
table->setStyleSheet("QTableView {selection-background-color: #0000FF; selection-color: #00FF00;}
but it only changes the Color that is shown while the pointer is on a Cell. After the pointer is away, wether I select the cell by table->selectRow(selRow)
there is only the underline. Probably it looks differen on other plattforms.
There are a lot of thread with equal topic, but most answer is using the Stylesheet above. Nothing worked, only the "moseover Color" changes.
Thanks in advance, Regards Matthias
Upvotes: 2
Views: 11469
Reputation: 1350
table->setStyleSheet("QTableView:item:selected {background-color: #XXYYZZ; color: #FFFFFF}\n"
"QTableView:item:selected:focus {background-color: #3399FF;}")
Unfortunately there doesn't seem to be a "nofocus" property, so you just have to set the colour for all selected items, then override the focused colour back to the default. #3399FF
is what a colour picker revealed the default highlight background colour was for my setup, so I used that. You can substitute with whichever colour you like.
The color: #FFFFFF
sets the text colour to something custom when the selection loses focus. It's white for me when I have focus, so I just keep it as white when it loses focus. You can use whatever colour you like, or remove that part to use the default.
Upvotes: 2
Reputation: 6320
This is what I did.
stylesheet = "QTableView{selection-background-color: " + highlight + ";"
stylesheet += "selection-color: white; show-decoration-selected: 10}\n"
stylesheet += "QTableView::item:focus{border: 1px solid yellow;"
stylesheet += "background-color:"+highlight+"}"
table->setStyleSheet(stylesheet);
The selection color does the one item that is selected while the item focus will color the rest of the items that should be highlighted.
This works for the selected cells like having a selected row. If you want something for "mouse over" you may have to use "hover" in the style sheet. Hope this can give you ideas.
Upvotes: 2
Reputation: 43
class BackgroundDelegate : public QStyledItemDelegate {
public:
explicit BackgroundDelegate(QObject *parent = 0)
: QStyledItemDelegate(parent){}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
// Fill the background before calling the base class paint
// otherwise selected cells would have a white background
QVariant background = index.data(Qt::BackgroundRole);
if (background.canConvert<QBrush>())
painter->fillRect(option.rect, background.value<QBrush>());
// the comment below makes selection transparent
//QStyledItemDelegate::paint(painter, option, index);
// To draw a border on selected cells
if(option.state & QStyle::State_Selected) {
painter->save();
QPen pen(Qt::black, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
int w = pen.width()/2;
painter->setPen(pen);
painter->drawRect(option.rect.adjusted(w,w,-w,-w));
painter->restore();
}
}
};
then
table->setItemDelegateForColumn(2, new BackgroundDelegate(this));
Upvotes: 2
Reputation: 246
You need to use a custom delegate to paint selected cells as you want them.
Have a look at the QAbstractItemView::setItemDelegate()
method and the QItemDelegate
class. You'll need to override the QItemDelegate::paint()
method. The paint method takes a QStyleOptionViewItem
structure - you can use this to determine if the item you're being asked to paint is selected.
The Qt docs for QItemDelegate::paint
have sample code that does exactly that.
Upvotes: 0