Results 1 to 5 of 5

Thread: Checkable QActions (menu items) with Icons don't show check indicators.

  1. #1
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Checkable QActions (menu items) with Icons don't show check indicators.

    Is it true that if I add an icon to a checkable QAction, the icon is displayed INSTEAD OF the check indicator? See the accompanying image and code excerpt. Is there a way to display both? This is with Qt 4.8.5 on Windows.

    WinIconMenu1-Detail.png

    Qt Code:
    1. void IconUtils::addWindowIconActions (QMenu* parMenu, QActionGroup* actGrp)
    2. {
    3. actGrp->setExclusive (true);
    4.  
    5. const QStringList iconNames = windowIconNames();
    6. const int cnt = iconNames.count();
    7.  
    8. for (int inx = 0; inx < cnt; ++inx)
    9. {
    10. const QString iconName = iconNames [inx];
    11.  
    12. QAction* winIconAction = new QAction (actGrp);
    13. winIconAction -> setObjectName (iconName);
    14. winIconAction -> setIcon (windowIconPixmap (iconName));
    15. winIconAction -> setData (QVariant (inx));
    16. winIconAction -> setCheckable (true);
    17. winIconAction -> setChecked (inx == 0);
    18.  
    19. parMenu->addAction (winIconAction);
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Thank you in advance,
    Phil Weinstein, CADSWES

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checkable QActions (menu items) with Icons don't show check indicators.

    Quote Originally Posted by philw View Post
    Is it true that if I add an icon to a checkable QAction, the icon is displayed INSTEAD OF the check indicator?
    In your case that is true.

    More precisely: it depends on the widget style.
    Some styles display the check indicator separately independent of whether an icon is set or not.
    Some other styles, like in your case the Windows style, show the icon in pressed/released state instead.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    philw (6th October 2013)

  4. #3
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checkable QActions (menu items) with Icons don't show check indicators.

    Anda, thanks. I took your response to mean that the various widget styles would all have the same problem. I did experiment with that anyway. Here are some screenshots, with each of the available Window Styles in our Qt 4.8.5 build on Windows 7.

    (1) Animated ...
    (2) Montage ...

    DetailB-Anim.gifDetailB-Montage.jpg

    (BTW, I don't see any difference between the three Windows styles).

  5. #4
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checkable QActions (menu items) with Icons don't show check indicators.

    I DID ACCOMPLISH THIS — showing a CHECK INDICATOR (exclusive, radio-button type) on checkable QAction menu items having Icons. I’m creating the check indicator graphic myself — augmenting the application-level icon QPixmaps. BELOW are the three pieces of code which implement this.

    SEE RESULT EXAMPLE:

    2013-10-07a.png

    http://cadswes2.colorado.edu/~philw/...013-10-07a.png

    CODE SAMPLES BELOW:

    #1 class QMenuIconSizeProxyStyle : public QProxyStyle … allows a QMenu to have wider-than-normal icons on menu items (QActions).

    #2 QIcon checkMenuIcon (const QPixmap& sourcePmap, QSize& iconSize) … Create a two-state QIcon from a source QPixmap, adding on a check indicator graphic

    #3 void addWindowIconActions (QMenu* menu, QActionGroup* actGrp) … builds a menu of exclusive items for a list of application-level icons. (uses both items above).

    Qt Code:
    1. // QProxyStyle sublass to be installed on a QMenu needing to support
    2. // wider-than-usual icons. It overrides the QStyle::PM_SmallIconSize
    3. // property with the width of the QSize provided by the client.
    4.  
    5. class QMenuIconSizeProxyStyle : public QProxyStyle
    6. {
    7. private:
    8. QSize _iconSize; // note: only the width is currently used.
    9.  
    10. public:
    11. QMenuIconSizeProxyStyle() : QProxyStyle() {}
    12.  
    13. void setIconSize (const QSize& iconSize) { _iconSize = iconSize; }
    14.  
    15. int pixelMetric (QStyle::PixelMetric metric,
    16. const QStyleOption* option=NULL,
    17. const QWidget* widget=NULL) const override
    18. {
    19. if (metric == QStyle::PM_SmallIconSize)
    20. return (_iconSize.width());
    21.  
    22. // Return base class result
    23. return QProxyStyle::pixelMetric (metric, option, widget);
    24. }
    25. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QIcon checkMenuIcon (const QPixmap& sourcePmap, QSize& iconSize)
    2. {
    3. // Create and return a QIcon composed from two modified versions of
    4. // the given "source" QPixmap -- for ON and OFF states -- suitable for
    5. // checkable QActions (menu items) having an icon. [In Qt 4.8.5,
    6. // checkable QActions with icons don't show a drawn check indicator].
    7. //
    8. // The computed pixmaps have additional space to the left where a
    9. // bullet (filled black circle) is drawn on the "ON" image. This
    10. // appearance is intended for exclusive (radio button type)
    11. // QActions (menu items) -- and looks like the Cleanlooks style.
    12. //
    13. // The QSize of the computed pixmaps (including a check indicator area)
    14. // is returned in the iconSize reference parameter.
    15.  
    16. const QSize srcSize = sourcePmap.size();
    17. const int srcWid = srcSize.width();
    18. const int srcHgt = srcSize.height();
    19. const int indWid = srcHgt; // indicator width
    20.  
    21. iconSize = QSize (srcWid + indWid, srcHgt);
    22. QPixmap iconPmap (iconSize);
    23. iconPmap.fill (Qt::transparent);
    24.  
    25. QPainter painter (&iconPmap);
    26. painter.drawPixmap (indWid, 0, sourcePmap);
    27.  
    28. QIcon retIcon;
    29. retIcon.addPixmap (iconPmap, QIcon::Normal, QIcon::Off);
    30.  
    31. const double radius (indWid / 8.0);
    32. const QPointF cen (indWid/2.0, srcHgt/2.0);
    33. painter.setBrush (QBrush (Qt::black));
    34. painter.drawEllipse (cen, radius, radius);
    35.  
    36. retIcon.addPixmap (iconPmap, QIcon::Normal, QIcon::On);
    37. return (retIcon);
    38. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Build a QMenu with checkable (mutually exclusive) icon QActions.
    2. // The 'checkMenuIcon' utility is used to add a "radio button"-type
    3. // check indicator to the left of each source QPixmap.
    4.  
    5. void addWindowIconActions (QMenu* menu, QActionGroup* actGrp)
    6. {
    7. actGrp->setExclusive (true);
    8. QSize maxIconSize;
    9.  
    10. // Create a checkable Icon QAction (menu item) for each available
    11. // application window icon; insert into the given QMenu and QActionGroup.
    12.  
    13. const QStringList iconNames = windowIconNames();
    14. const int cnt = iconNames.count();
    15.  
    16. for (int inx = 0; inx < cnt; ++inx)
    17. {
    18. const QString iconName = iconNames [inx];
    19.  
    20. QSize iconSize;
    21. const QPixmap pmap = windowIconPixmap (iconName);
    22. const QIcon menuIcon = checkMenuIcon (pmap, iconSize); // SEE ABOVE
    23. maxIconSize = maxIconSize.expandedTo (iconSize);
    24.  
    25. QAction* winIconAction = new QAction (actGrp);
    26. winIconAction -> setObjectName (iconName);
    27. winIconAction -> setIcon (menuIcon);
    28. winIconAction -> setData (QVariant (inx));
    29. winIconAction -> setCheckable (true);
    30. winIconAction -> setChecked (false);
    31.  
    32. menu->addAction (winIconAction);
    33. }
    34.  
    35. // Instantiate a QProxyStyle for a modified QStyle::PM_SmallIconSize
    36. // and install that on the QMenu. SEE PRIOR FUNCTION.
    37.  
    38. QMenuIconSizeProxyStyle* proxyStyle = new QMenuIconSizeProxyStyle;
    39. proxyStyle->setIconSize (maxIconSize);
    40. menu->setStyle (proxyStyle);
    41. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checkable QActions (menu items) with Icons don't show check indicators.

    Quote Originally Posted by philw View Post
    Anda, thanks. I took your response to mean that the various widget styles would all have the same problem.
    Yes and no

    The style will display the checked state such that it fits into the targetted look&feel. If the system's visualisation for check marks on action with icons is to show the icon in a "pressed" state, then this is what the style will draw as well.

    Quote Originally Posted by philw View Post
    (BTW, I don't see any difference between the three Windows styles).
    Then it is most likely that Windows applications do not normally show a check marker along side the icon but show the checked state by changing the icon's visualization instead.

    KDE's Oygen style for example uses a separate check marker, independent of whether there is an icon or not.

    Cheers,
    _

Similar Threads

  1. Problems with QActions and Icons
    By franco.amato in forum Qt Programming
    Replies: 5
    Last Post: 15th September 2013, 11:33
  2. Replies: 2
    Last Post: 8th April 2013, 06:40
  3. Strange behavior of checkable menu items..
    By jiapei100 in forum Qt Tools
    Replies: 1
    Last Post: 28th October 2009, 19:58
  4. Combobox with checkable items
    By qtneuling in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 14:42
  5. checkable items in QTableView
    By cgorac in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2008, 22:45

Tags for this Thread

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.