Results 1 to 3 of 3

Thread: how to highlight the Icon in QListWidget when cursor is over the Icon(not clicked)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class ItemDelegate : public QStyledItemDelegate
    4. {
    5. public:
    6. ItemDelegate(QObject* parent = 0) : QStyledItemDelegate(parent)
    7. {
    8. }
    9.  
    10. void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    11. {
    12. Q_ASSERT(index.isValid());
    13.  
    14. QStyleOptionViewItemV4 opt = option;
    15. initStyleOption(&opt, index);
    16.  
    17. if (opt.state & QStyle::State_MouseOver)
    18. opt.icon = opt.icon.pixmap(opt.decorationSize, QIcon::Active); // <--
    19.  
    20. QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);
    21. }
    22. };
    23.  
    24. int main(int argc, char* argv[])
    25. {
    26. QApplication app(argc, argv);
    27. list.setItemDelegate(new ItemDelegate(&list)); // <--
    28. list.viewport()->setAttribute(Qt::WA_Hover); // <--
    29.  
    30. QIcon icon;
    31. icon.addPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-no-16.png"), QIcon::Normal); // <--
    32. icon.addPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-yes-16.png"), QIcon::Active); // <--
    33.  
    34. for (int i = 0; i < 10; ++i)
    35. {
    36. item->setText(QString("item %1").arg(i));
    37. item->setIcon(icon); // <--
    38. list.addItem(item);
    39. }
    40. list.show();
    41. return app.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: how to highlight the Icon in QListWidget when cursor is over the Icon(not clicked

    thanks guru

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.