1

I'm writing newbie Qt5(.4.0) code on OSX Mavericks. Here's my self-contained test case:

#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QMenuBar>
#include <QGridLayout>
#include <QPushButton>

int
main( int argc, char *argv[] ) {
    QApplication app( argc, argv );

    QMainWindow* mw = new QMainWindow();

    mw->menuBar()->setNativeMenuBar( false );

    QMenu* fileMenu = mw->menuBar()->addMenu( "&File" );
    QMenu* optionsMenu = mw->menuBar()->addMenu( "&Options" );

    QWidget* menuCorner = new QWidget( mw->menuBar() );

    QGridLayout* cornerLayout = new QGridLayout();

    QPushButton* newWindowButton = new QPushButton( "New Window" );

    cornerLayout->addWidget( newWindowButton, 1, 0 );

    menuCorner->setLayout( cornerLayout );

    mw->menuBar()->setCornerWidget( menuCorner );

    mw->show();

    return app.exec();
}

The "New Window" pushbutton shows up at the right side of the menubar, as intended, however the bottom half of the "New Window" pushbutton is clipped, and thus hidden, by the bottom separator line for the menubar:

enter image description here

How can I make the new corner-widget pushbutton appear fully in the menubar without getting clipped?

Thanks

lcikgl
  • 759
  • 1
  • 8
  • 20

1 Answers1

1

OSX menu bars must have a fixed height and your button doesn't fit there. Try to remove layout margins:

cornerLayout->setContentsMargins(0, 0, 0, 0);

And / or make the button smaller:

newWindowButton->setMaximumHeight(30);

Also, adjust margin to what best fits your needs:

menuCorner->setStyleSheet("margin-top: 2");

This is how it looks for me:

svlasov
  • 9,923
  • 2
  • 38
  • 39
  • Thanks @svlasov. Both of these make the "New Window" button almost fully visible, however in both cases the bottom of the button is still clipped by the bottom separator line for the menubar. In order to make the button fully visible in the menubar I have to go down to maximum height of 12, which then clips the button text and makes the button inconveniently small. Since I invoked `mw->menuBar()->setNativeMenuBar( false )`, shouldn't I be immune from the fixed height limitation of OSX menu bars? Is there a way to force the non-native menubar to accommodate the natural size of the pushbutton? – lcikgl Feb 27 '15 at 16:18
  • @lcikgl can you post a screenshot? – svlasov Feb 27 '15 at 16:34
  • 1
    I'm going to go ahead and accept the answer from @svlasov since it inspired a passable workaround, namely to invoke `mw->menuBar()->setMinimumHeight( 30 );` along with `cornerLayout->setContentsMargins( 0, 0, 0, 0 );`. This isn't perfect since 'File' and 'Options' are offset above horizontal center, plus I had to hard-wire the magic-number of 30 pixels, however the result is tolerable. It seems there should be a solution that allows Qt layout engine to automatically adjust pushbutton and menubar so they look natural without hard-wired magic numbers. Feedback still welcome on that. – lcikgl Feb 27 '15 at 17:21
  • Hello @svlasov, I just saw your request for a screenshot. I've attempted to do so however as a new poster I don't yet have enough reputation points to post images. – lcikgl Feb 27 '15 at 17:22
  • Are you trying to put button to global menu or window menu? Post screenshot url. – svlasov Feb 27 '15 at 17:59
  • I am attempting to put the button on the window menu -- note that I've called `setNativeMenuBar(false)`. I apologize however I do not yet have enough reputation points to post screenshot images. – lcikgl Feb 27 '15 at 18:55