PDA

View Full Version : highlighting hovered QListWidget item



joval
28th July 2012, 19:19
Me again.

I have QListWidget where I want to highlight hovered item. The problem is, when I try to do so using CSS, it doesn't work - background color isn't changing on hover. My code:


QStringList files = Settings->recentFiles();
int numRecentFiles = qMin(files.size(), 5);
for (int i = 0; i < numRecentFiles; i++)
{
listWidget->addItem(new QListWidgetItem(files[i]));
}

listWidget->setMouseTracking(true);
listWidget->setStyleSheet("QListWidget::item:hover {background-color:yellow;}");

I've also tried to succeed with signal-slot mechanism and I must say, almost did it. The problem is, when cursor leaves the widget, highlight stays at the last hovered element, because I don't know how to use the 'viewport left event'. I would be grateful if someone could provide me some example. (lol, I ended this sentence with semicolon and pushed Ctrl+S).

Thanks in advance

EDIT: Well, right after posting I answered myself the first question. Instead of
listWidget->setStyleSheet("QListWidget::item:hover {background-color:yellow;}"); I should write
this->setStyleSheet("QListWidget::item:hover {background-color:yellow;}");
but I don't like the effect.

Second question still active though.

amleto
28th July 2012, 19:55
this example shows 'background' not 'background-color'

joval
29th July 2012, 11:40
this example shows 'background' not 'background-color'
For a single attribute, it is recommended to use specified names.


I managed to achieve the wished effect. Firstly, I was not satisfied with CSS hovering because background wasn't expanding to margins, so I changed margin to padding and now it's ok.

Problem solved (I still don't know how to use viewport, but it's a thing for another story ;))