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. QMenu styling issue

QMenu styling issue

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 10.0k Views
  • 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #1

    I'm trying to create a bit of space on the left side of a QMenu in order to put some graphical elements (an icon and a line) left of the menu items. I figured I should be able to do that using Qt stylesheets, as QMenu "supports the Box model" according to the documentation. So, I wend ahead and in my subclass of QMenu did:
    @
    setStyleSheet("padding-left: 40px");
    @

    While that creates the needed space on the left, it also creates the same space on the right of the items. I tried to get rid of that space, but I have failed so far. Can anyone here come up with a trick to create some space left of the menu items but not to the right of them?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mairesse
      wrote on last edited by
      #2

      I might be wrong, because I'm new in Qt and stuff but usually when I use QMenu (as context menus) I add actions to them, right?

      I just create the action with the desired icon I want for it and then I add it to the QMenu and it places the icon just in the left of action's text.

      [code]QAction *action = new QAction(QIcon(":/resources/myActionIcon.png"), "Action with icon", this);[/code]

      Sorry if I misunderstood.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        No, this is explicitly not what I mean. Sorry if I was unclear. I know I can add icons to menu items, but I want to add an icon to the menu itself, like this:

        !https://dl.dropbox.com/u/16442531/menumockup.png(Mockup of customized menu)!

        The icon I'm talking about is the one on the left. What I end up with so far is this:

        !https://dl.dropbox.com/u/16442531/menuscreenshot.png(Screenshot of menu)!

        Notice the excess space on the right of the menu items, as well as in the menu items themselves.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Have you tried to set the right padding space explicitly to zero ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Yes, I tried, and that yielded no difference.

            Edit: What does work (well, almost), is to reimplement sizeHint like this:
            @
            QSize IconMenu::sizeHint() const
            {
            QSize hint = QMenu::sizeHint();
            hint.setWidth(hint.width() - 80);
            return hint;
            }
            @

            It looks ok, but for the fact that now the right side of the item highlights is not correctly rendered. Instead, it is just cut-off. It is a big improvement over what I had, but I'd considder this a hack, not a solution.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              marsupial
              wrote on last edited by
              #6

              Why are you decreasing the width sizeHint by 80 instead of 40 (isn't it 40 pixels too wide on the right? Probably I'm missing something).

              Also not a very nice solution, but you could try to set a negative padding (or margin) to the right, so for example:
              @padding-right: -40px;@
              (Or margin, don't know what would work better).
              I don't know if Qt supports negative margins/paddings).

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Because with 80px, it also gets rid of the excess space inside the items. Still, I am not happy with it at all.

                I'll experiment with setting a negative right padding and/or margin.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  marsupial
                  wrote on last edited by
                  #8

                  Oh, I didn't read that part, sorry :)

                  Just one more question out of curiosity: Is it right that you end up with one times 40px padding on the left and two times 40px on the right (one within the item, one outside)? Or do you draw the individual item icons yourself and thus you have two times 40px on the left as well?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    No, I end up with the right amount of padding on the left, but on the right, I have both padding outside as well as inside the items. Perhaps I should mention that I also use a proxy style on the menu. This proxy style gives a larger value for the small icons (the icons next to the items), because I need them to be bigger than the normal 16x16 or so.

                    The only icon I draw manually it the bigger one on the top-left. The vertical line I also render myself. The rest of the dialog is rendered by Qt by calling QMenu::paintEvent(QPaintEvent* e) from my own implementation of that method.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      marsupial
                      wrote on last edited by
                      #10

                      Hm, I just played around a little with the style sheet and menus, and I as far as I can see, Qt just completly ignores the right padding and takes the left one instead.
                      So while @helpMenu->setStyleSheet("padding-right:50px");@

                      has no effect,

                      @helpMenu->setStyleSheet("padding-left:50px");@

                      has the effect that you described: It adds to both sides.

                      Maybe it indeed is a bug?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        I have filed a "bugreport":https://bugreports.qt-project.org/browse/QTBUG-26799 for this issue.

                        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