5

In my code there is a bunch of calls which try to create QIcons from QStyle standard pixmaps, like:

QIcon groupIcon;
groupIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirClosedIcon ),
                     QIcon::Normal, QIcon::Off );
groupIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirOpenIcon ),
                     QIcon::Normal, QIcon::On );

While this works correctly, in that using the icon for a model's Qt::DecorationRole shows either an open or closed icon based on the item's expanded state, it has two issues:

  1. It's not hi-dpi friendly, and the icons are tiny
  2. QStyle::standardPixmap is marked as obsolete, with QStyle::standardIcon being described as the preferred approach.

I'm unsure how to translate the above code to QStyle::standardIcon though.

QIcon groupIcon( style()->standardIcon( QStyle::SP_DirClosedIcon ) );

works nicely for closed items, and looks great on hidpi. But I can't see how I would add the SP_DirOpenIcon state. There's no equivalent method like "QIcon::addIcon" like there is QIcon::addPixmap.

What's the correct approach to take here, which is hi-dpi friendly and future proof?

2
  • Any luck finding another solution? If yes, would you mind to share it? If not, you might accept the answer after all, as it is correct and it works.
    – scopchanov
    Commented Nov 2, 2018 at 19:45
  • No - I haven't been able to find an approach here which is hi doing friendly. We actually just gave up on using QStyle::standardIcon as a result and ship custom replacement icons instead. I'd still love to find an answer which meets the main criteria here - that the resultant icon is scalable and works correctly on low/hi dpi displays.
    – ndawson
    Commented Nov 2, 2018 at 19:50

1 Answer 1

1

how to translate the above code to QStyle::standardIcon

To be able to use QStyle::standardIcon instead of QStyle::standardPixmap, select the particular pixmap from the icon with QIcon::pixmap.

Here is an example I have prepared for you of how to change your code in order to accomplish that:

QIcon groupIcon;
QSize sz(16, 16);

groupIcon.addPixmap(style()->standardIcon(QStyle::SP_DirClosedIcon).pixmap(sz),
                     QIcon::Normal, QIcon::Off);
groupIcon.addPixmap(style()->standardIcon(QStyle::SP_DirOpenIcon).pixmap(sz),
                     QIcon::Normal, QIcon::On);

Here 16 is the requested size. Please note, that:

The pixmap might be smaller than requested, but never larger.

hence adjust this value accordingly.

4
  • The problem with this approach is that it relies on knowledge of the icon size in advance, and so loses QIcon's ability to scale icons at render time. So unfortunately this approach isn't hi-dpi compatible.
    – ndawson
    Commented Sep 17, 2018 at 0:41
  • @ndawson, are you aware of any other approach?
    – scopchanov
    Commented Sep 17, 2018 at 0:43
  • Unfortunately no, that's what I'd like to find!
    – ndawson
    Commented Sep 17, 2018 at 9:40
  • @ndawson, exactly. Like it or not, this is how it is done if you want to translate your code to use standardIcon instead of standardPixmap.
    – scopchanov
    Commented Sep 17, 2018 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.