how to highlight the Icon in QListWidget when cursor is over the Icon(not clicked)
Hello friends,
I am working in QListWidget for selecting Icons as like QPushButton ... (configDialog) now the problem is like pushbutton i have to highlight the icon when the cursor is over the icon but not clicked
Re: how to highlight the Icon in QListWidget when cursor is over the Icon(not clicked
It doesn't seem to be supported out of the box by the model-view framework. It only takes normal, selected and disabled modes of the icon into account. However, here's one way to work the limitation around:
Code:
#include <QtGui>
class ItemDelegate : public QStyledItemDelegate
{
public:
ItemDelegate
(QObject* parent
= 0) : QStyledItemDelegate
(parent
) {
}
void paint
(QPainter* painter,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
Q_ASSERT(index.isValid());
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
if (opt.
state & QStyle::State_MouseOver) opt.
icon = opt.
icon.
pixmap(opt.
decorationSize,
QIcon::Active);
// <--
}
};
int main(int argc, char* argv[])
{
list.setItemDelegate(new ItemDelegate(&list)); // <--
list.viewport()->setAttribute(Qt::WA_Hover); // <--
icon.
addPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-no-16.png"),
QIcon::Normal);
// <-- icon.
addPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-yes-16.png"),
QIcon::Active);
// <--
for (int i = 0; i < 10; ++i)
{
item
->setText
(QString("item %1").
arg(i
));
item->setIcon(icon); // <--
list.addItem(item);
}
list.show();
return app.exec();
}
Re: how to highlight the Icon in QListWidget when cursor is over the Icon(not clicked