Results 1 to 6 of 6

Thread: QMenuBar doesn't show up properly

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QMenuBar doesn't show up properly

    Hello!

    I have a problem with a QMenuBar. It doesn't show up properly. Instead of all the QMenu and QAction inside it, it only shows a ">>" symbol like this:
    menu.jpg

    I can press the symbol and a menu of menus shows up and everything works fine. But this is still a problem. I tried playing around with the size of the widget the QMenuBar is inside, removed all relevant style sheet entries and reduced the contents of the QMenuBar to a single QMenu with one QAction inside. Nothing helps. This happens only on one of two PCs where I tested it (Win XP does not work, Win7 works). The code to produce the menu looks like this:

    Qt Code:
    1. // Build the menu bar.
    2. QMenuBar* menuBar = new QMenuBar();
    3. QHBoxLayout* menuLayout = new QHBoxLayout(ui.menuWidget);
    4. menuLayout->setMargin(0);
    5. menuLayout->addWidget(menuBar);
    6. menuLayout->addStretch(1);
    7. ui.menuWidget->setLayout(menuLayout);
    8.  
    9. QMenu* fileMenu = menuBar->addMenu(tr("&File"));
    10.  
    11. QAction* saveStateAction = fileMenu->addAction(tr("&Save State"));
    12. saveStateAction->setToolTip(tr("Saves the state history."));
    13. saveStateAction->setShortcut(QKeySequence(tr("Ctrl+S")));
    14. connect(saveStateAction, SIGNAL(triggered()), this, SLOT(saveStateHistory()));
    15.  
    16. // ...and more QMenus and QActions follow.
    To copy to clipboard, switch view to plain text mode 


    Any advice?

    thanks,
    Cruz

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenuBar doesn't show up properly

    If you want a menu bar, maybe you could use QMainWindow instead? It already has a menu bar and proper layout to handle it.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenuBar doesn't show up properly

    I guess I could figure it out. I would want to hide() away the tool bars and the status bar. And I'm using designer to create the gui. Would I just design the central widget and then call ui.setupUi(centralWidget()) in the ctor of the QMainWindow? But even if I do this, the problem described above remains unsolved.


    Added after 13 minutes:


    Quote Originally Posted by Cruz View Post
    and then call ui.setupUi(centralWidget()) in the ctor of the QMainWindow?
    No, this doesn't work. I get a segfault right after start. I suppose the central widget doesn't exist yet. So if using a QMainWindow, how do I manage to apply my designed ui to the central widget?
    Last edited by Cruz; 19th February 2014 at 12:54.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenuBar doesn't show up properly

    If you have your widget in designer, you probably have a class that does with it, right?

    Just create an instance of that class and set it on the main window using QMainWindow::setCentralWidget()

    You can alternatively create the whole main window in designer, then the code stays
    Qt Code:
    1. ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenuBar doesn't show up properly

    Ok yes I could reuse the class I already have for the ui, but then I would separate out half of my gui into a main windows class, where I set up the toolbar and link the central widget, and the other half would remain in my "old" QWidget that currently does it all. I would not like this separation.

    The second alternative of designing the whole main windows sort of defeats the purpose of using a QMainWindow, because I would just replace what's already been set up automatically and most likely run into the very same problem I described above.

    Before taking the bullet and going with option one, isn't there an explanation + solution for the problem why the menu bar doesn't show up right?

    Like this it works perfectly:

    Qt Code:
    1. MyWindow::MyWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QWidget* cw = new QWidget();
    5. ui.setupUi(cw);
    6. setCentralWidget(cw);
    7.  
    8. QMenuBar* menuBar = new QMenuBar();
    9. setMenuBar(menuBar);
    10.  
    11. QMenu* fileMenu = menuBar->addMenu(tr("&File"));
    12.  
    13. QAction* saveStateAction = fileMenu->addAction(tr("&Save State"));
    14. saveStateAction->setToolTip(tr("Saves the state history."));
    15. saveStateAction->setShortcut(QKeySequence(tr("Ctrl+S")));
    16. connect(saveStateAction, SIGNAL(triggered()), this, SLOT(saveStateHistory()));
    17.  
    18. [...]
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cruz; 19th February 2014 at 14:49.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenuBar doesn't show up properly

    Quote Originally Posted by Cruz View Post
    Ok yes I could reuse the class I already have for the ui, but then I would separate out half of my gui into a main windows class, where I set up the toolbar and link the central widget, and the other half would remain in my "old" QWidget that currently does it all. I would not like this separation.
    Not necessarily. You could just use your widget and pass in the menu bar or create a menu bar in your widget and set it as the main window's menu bar.

    Quote Originally Posted by Cruz View Post
    The second alternative of designing the whole main windows sort of defeats the purpose of using a QMainWindow, because I would just replace what's already been set up automatically and most likely run into the very same problem I described above.
    Well, I would have just copied the contents of your widget in designer into a QMainWindow based designer form and the code into the main window class.
    There would be no "replacing".

    Quote Originally Posted by Cruz View Post
    Before taking the bullet and going with option one, isn't there an explanation + solution for the problem why the menu bar doesn't show up right?
    You could try a couple of things:
    1) not adding a stretch
    2) setting the menu bar's horizontal size policy to expanding
    3) check that your ui.menuWidget is in a layout and that it spans the whole width of the window

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    Cruz (20th February 2014)

Similar Threads

  1. why addSeparator() doesn't work on QMenuBar
    By yxmaomao in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2008, 08:44
  2. Show QPixmap into QMenuBar
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 28th May 2008, 11:03
  3. [B]How to show Pixmap on QMenuBar??[/B]
    By bigbigmoon in forum Newbie
    Replies: 2
    Last Post: 17th April 2007, 06:55
  4. show the backgroundpixmap of QMenuBar??-Qt-3.3.6
    By bigbigmoon in forum Newbie
    Replies: 10
    Last Post: 4th April 2007, 03:45
  5. where could QPopupMenu show besides QMenuBar !
    By bigbigmoon in forum Newbie
    Replies: 4
    Last Post: 2nd November 2006, 01:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.