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. Can't set fixed/minimal width for QMenuBar CornerWidget

Can't set fixed/minimal width for QMenuBar CornerWidget

Scheduled Pinned Locked Moved Solved General and Desktop
qt6layout issuesc++layoutqmenubar
5 Posts 2 Posters 1.1k 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.
  • B Offline
    B Offline
    bibasmall
    wrote on 12 Oct 2022, 12:12 last edited by bibasmall 10 Dec 2022, 12:22
    #1

    Hello everyone!
    I want to place window control buttons (close, minimize, etc.) in the menu bar. So I made a QHBoxLayout and added these fixed size buttons to it. Then I made a QWidget and set a layout for that widget and in the end I set that widget as a CornerWidget and set a fixed height for the entire menu bar and a fixed width for the corner widget.
    The height is OK, but if set maximum/fixed width for the corner widget, my corner widget looks clipped and there is huge padding on the right.
    e6926354-9646-48cd-a955-a364ca91580c-image.png
    If I don't do anything with the width, my fixed size buttons are somehow stretched horizontally:
    37b3d374-3bca-4da9-8728-2ee7ba46ffca-image.png

    How do I place the buttons in the right corner?

    Here is my QMenuBar-subclass constructor body:

    setMaximumHeight(22);
    
    QWidget* menuWidget = new QWidget(this);
    
    QHBoxLayout* menuWidgetLayout = new QHBoxLayout(menuWidget);
    menuWidget->setLayout(menuWidgetLayout);
    //menuWidget->setFixedWidth(70);
    
    menuWidgetLayout->addWidget(new WindowCtrlButton("qButtonMinimizeWindow"));
    menuWidgetLayout->addWidget(new WindowCtrlButton("qButtonMaximizeRestoreWindow"));
    menuWidgetLayout->addWidget(new WindowCtrlButton("qButtonCloseWindow"));
    
    setCornerWidget(menuWidget, Qt::TopRightCorner);
    
    setContentsMargins(0, 0, 0, 0);
    menuWidget->setContentsMargins(0, 0, 0, 0);
    menuWidgetLayout->setContentsMargins(0, 0, 0, 0);
    cornerWidget()->setContentsMargins(0, 0, 0, 0);
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 12 Oct 2022, 13:01 last edited by Chris Kawa 10 Dec 2022, 13:04
      #2

      As the documentation says the size of the corner widget space is determined based on the widget's sizeHint(). In your case this means the sum of minimum sizes of the buttons plus layout margins. Even if you force the size to be smaller the sizeHint stays the same, so your widget is smaller but is placed in the left side of the space determined by larger sizeHint.

      So basically to make the widget smaller, instead of fixing the widget size, make the buttons minimum sizes smaller.

      Btw. some unrelated pointers:

      • QWidget* menuWidget = new QWidget(this); Don't pass the parent if you're going to use the widget as a corner widget anyway. Tab widget is gonna take ownership of it anyway, so you're paying the unnecessary cost of reparenting here.
      • menuWidget->setLayout(menuWidgetLayout); This is redundant. You've passed the widget to the layout's constructor, so the layout is already set on the widget. This does nothing here.
      • setContentsMargins(0, 0, 0, 0);, menuWidget->setContentsMargins(0, 0, 0, 0); and cornerWidget()->setContentsMargins(0, 0, 0, 0); Those do nothing. Widgets have a 0 content margin by default. Only layouts don't.
      1 Reply Last reply
      2
      • B Offline
        B Offline
        bibasmall
        wrote on 13 Oct 2022, 20:26 last edited by
        #3

        @Chris-Kawa, I'm afraid this doesn't work, if I understand you correctly.
        In my QPushButton subclass, I have set min and max size and set minimal size policy and also re-implemented sizeHint(), but without any success. I even tried setting the button size fixed with the fixed size policy, but they still stretch. Same for QPushButton.
        But thank you very much for your answer and especially for the additional comments.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 13 Oct 2022, 21:05 last edited by
          #4

          It works for me. What if you use a plain QPushButton without text? Is it still this wide? Can you post the code of your button class?

          B 1 Reply Last reply 14 Oct 2022, 08:40
          0
          • C Chris Kawa
            13 Oct 2022, 21:05

            It works for me. What if you use a plain QPushButton without text? Is it still this wide? Can you post the code of your button class?

            B Offline
            B Offline
            bibasmall
            wrote on 14 Oct 2022, 08:40 last edited by
            #5

            @Chris-Kawa, oh, pardon my blindness, it really works. I found that the stylesheet is resetting the width of the button in my case.

            1 Reply Last reply
            0

            1/5

            12 Oct 2022, 12:12

            • Login

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