#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();
}
#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); // <--
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QListWidget list;
list.setItemDelegate(new ItemDelegate(&list)); // <--
list.viewport()->setAttribute(Qt::WA_Hover); // <--
QIcon icon;
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)
{
QListWidgetItem* item = new QListWidgetItem;
item->setText(QString("item %1").arg(i));
item->setIcon(icon); // <--
list.addItem(item);
}
list.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks