Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStyleOptionMenuItem draws a white straight line between the menu item's text and icon
Forum Updated to NodeBB v4.3 + New Features

QStyleOptionMenuItem draws a white straight line between the menu item's text and icon

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qmenuqstyleoptionmenuitemqstyle
5 Posts 3 Posters 930 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Borislav
    wrote on last edited by
    #1

    I am trying to create a custom-looking menu item and for that I am using the QWidgetAction class. The goal is to have a menu action that looks exactly the same as an ordinary one, but also add a button with an icon on the right end which will configure the action's options. Thus, I have subclassed QWidgetAction and I have the following createWidget definition:

    QWidget *createWidget(QWidget *parent) override {
            QMenu *parentMenu = qobject_cast<QMenu *>(parent); // Assume for simplicity that it is always a QMenu
            MenuItem *menuItem = new MenuItem(parentMenu, icon(), text(), "Ctrl+A");
    
            QHBoxLayout *layout = new QHBoxLayout();
            layout->setContentsMargins(0, 0, 0, 0);
            layout->setSpacing(0);
            layout->addWidget(menuItem, 20);
            QPushButton *optionsButton = new QPushButton(QIcon("../qtListApp/square.png"), QString());
            layout->addWidget(optionsButton, 0);
    
            QWidget *container = new QWidget(parent);
            container->setLayout(layout);
            return container;
        }
    

    And MenuItem inherits QWidget and looks exactly like a QMenu item. In order to achieve this, I have overriden the paintEvent method:

     void paintEvent(QPaintEvent *event) override {
            QPainter painter(this);
    
            QStyleOptionMenuItem opt;
            opt.initFrom(this);
            opt.text = text; // This is passed via the constructor of MenuItem
            opt.menuItemType = QStyleOptionMenuItem::Normal;
            opt.icon = icon; // This is passed via the constructor of MenuItem
            opt.palette = parentMenu->palette();
            opt.reservedShortcutWidth = fontMetrics().size(Qt::TextSingleLine, shortcutText).width();
    
            if(rect().contains(mapFromGlobal(QCursor::pos()))) {
                opt.state |= QStyle::State_Selected;
            }
    
            style()->drawControl(QStyle::CE_MenuItem, &opt, &painter, this);
        }
    

    And everything looks good, apart from the following straight white line defect between the icon and text:

    fb5e8539-186f-4817-a73c-087db70b3c27-image.png

    What could cause this? Thanks in advance

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      What is opt.type just before you draw?

      P.S.
      Unrelated but fontMetrics().size(Qt::TextSingleLine, shortcutText).width(); should be opt.fontMetrics.size(Qt::TextSingleLine, shortcutText).width();

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • B Offline
        B Offline
        Borislav
        wrote on last edited by Borislav
        #3

        It is SO_MenuItem (4) just before style()->drawControl is called, if that is what you meant.

        Thanks for the remark on the fontMetrics, fixed that.

        mrjjM 1 Reply Last reply
        0
        • B Borislav

          It is SO_MenuItem (4) just before style()->drawControl is called, if that is what you meant.

          Thanks for the remark on the fontMetrics, fixed that.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Borislav

          Hi
          Do you have any stylesheets applied ?
          Or is the dark look a platform theme ?

          B 1 Reply Last reply
          1
          • mrjjM mrjj

            @Borislav

            Hi
            Do you have any stylesheets applied ?
            Or is the dark look a platform theme ?

            B Offline
            B Offline
            Borislav
            wrote on last edited by Borislav
            #5

            @mrjj
            I have the following stylesheet applied to a containing widget

            QMenu, QMenu QPushButton { background-color: #303030; color: white }
            

            When I remove the QMenu selector, the white line disappears (hence it starts looking like this)
            10a42cae-0e1b-4d97-88b6-eacbaec7e038-image.png

            I cannot say whether the white line really disappears or it just blends with the white color of the menu item.


            After applying the same style with a QMenu::item selector, I get the following:
            5ffa874d-36f1-4742-a31a-078469ec6e2f-image.png
            The ordinary menu items are colored, but those created by my class are not. It appears as if the ::menuItem selector does not work on them.


            I also tried setting the following style rule on the container returned by the createWidget function

            container->setStyleSheet(".MenuItem {background-color: #303030; color: white; }");
            

            and it oddly makes the custom menu items get shifted to the left and the hover effects on them do not seem to work any longer. Applying padding/margin does not fix it:

            4132b0aa-b742-4af9-aa91-eeeb961b6aab-image.png

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved