Stacking QPushButtons on the other side of a QMenuBar
Asked Answered
R

2

7

I want to stack some QPushButton objects on the other side of my QMenuBar.

This is how my window looks now: how it looks

And this is how I want it to look like (I've photoshopped the image): how i want it to look

I know that in the motif widget style, the help menu is aligned to the right, but I'm sticking with plastique, so it's not a problem for me.

I'm using Qt4.8. Any ideas?

Rourke answered 1/2, 2013 at 11:45 Comment(1)
Maybe don't use a menubar but a toolbar with text-only buttons.Steels
R
11

QMenuBar has a setCornerWidget function, that sets a widget (that may include a whole layout) as the cornet widget.

Rourke answered 1/2, 2013 at 14:47 Comment(0)
D
3

QMainWindow::setMenuWidget() can be used to set any widget as the main window's menu bar widget. Using an appropriate layout, you can use something like the following to customize the menu bar (MainWindowImpl is a sub class of QMainWindow):

void MainWindowImpl::setupMenubar() {
    QWidget* menuWidget = new QWidget(this);

    QGridLayout* menuWidgetLayout = new QGridLayout(menuWidget);
    menuWidget->setLayout(menuWidgetLayout);

    // Add the menu bar and all tool buttons to the widget
    menuWidgetLayout->addWidget(theMenubar, 0, 0, 1,1);
    menuWidgetLayout->addWidget(new QToolButton(), 0, 1, 1, 1);
    menuWidgetLayout->addWidget(new QToolButton(), 0, 2, 1, 1);

    // set the custom widget as the main window's menu widget
    setMenuWidget(menuWidget);
}

theMenubar points to the QMenuBar which contains your application's main menu bar.

Dregs answered 1/2, 2013 at 12:28 Comment(1)
Great, did not know about that one - I used my approach earlier when I wanted to add some buttons inside the menu area, but below the menu items. Your approach is indeed much simpler when the buttons should be to the right or to the left of the menu items. Accept your own answer, I think you can earn a badge :)Dregs

© 2022 - 2024 — McMap. All rights reserved.