Results 1 to 3 of 3

Thread: Add QMenuBar to new windows

  1. #1

    Default Add QMenuBar to new windows

    Hello,

    I am writing a program.
    I have a QMainWindow and a function called createMenus() that returns a QMenuBar*.

    Within the class QMainWindow, I call: this->setMenuBar(createMenus());

    So I have a beautiful MainWindow with a menu in the top.

    Now, in "File", I want to put a button that creates a new window (presumably a QWidget?).
    And I would like to show the same MenuBar in the top part of my new window.

    void QMainWindow::createWindow()
    {
    QWidget *window = new QWidget;

    QVBoxLayout *center_box = new QVBoxLayout;
    center_box->setContentsMargins(0,0,0,0);
    center_box->setSpacing(0);

    #ifndef Q_OS_MAC
    QMenuBar *newMenu = createMenus();
    center_box->setMenuBar(newMenu);
    #endif

    window->setLayout(center_box);
    }

    This works (in Linux), as a QMenuBar is shown in the new window.
    The aftereffect is that the QMenuBar in the QMainWindow has disappeared.

    Can someone direct me to the best implementation to create multiple windows with the same QMenuBar?

    Kind regards,
    Luca

  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: Add QMenuBar to new windows

    If you need to have a menu bar in the second window, just use QMainWindow instead of QWidget.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Add QMenuBar to new windows

    There isn't anything in the code you have posted that would result in the QMainWindow's menu bar "disappearing". And there isn't anything in the code that would cause the QWidget "window" to become visible, because you don't give it a parent when you create it, nor do you call its show() method. Perhaps there is something happening in the createMenus() method that would result in this side effect.

    QMainWindow already has a QMainWindow::menuBar() method, which creates and returns a menu bar for you to populate with menu items. Calling QMainWindow::setMenuBar() replaces this built-in menubar with your own.

    If your createMenus() method is actually retrieving the QMainWindow menubar through a call to QMainWindow::menuBar() and returning that instance, then putting that instance into another widget as its menu bar will of course remove it from the original parent (QMainWindow). The same widget instance cannot be shared among more than one parent widget. In other words, if you're doing this:

    Qt Code:
    1. QMenuBar * MainWindow::createMenus()
    2. {
    3. QMenuBar * pMB = menuBar();
    4.  
    5. // add menus to pMB
    6.  
    7. return pMB;
    8. }
    To copy to clipboard, switch view to plain text mode 

    then this would cause the "disappearing menu" effect you see when you add that menubar to a different widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QMenuBar just didn’t show up on Windows Mobile 6
    By myfifth in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 22nd February 2011, 09:51
  2. QMenuBar and QMenu
    By thedoctor in forum Qt Programming
    Replies: 0
    Last Post: 21st December 2009, 22:01
  3. Styling QMenubar???
    By anupamgee in forum Qt Programming
    Replies: 5
    Last Post: 29th April 2009, 11:01
  4. How to use QMenuBar in Qt/Embedded on Windows CE
    By jrelax in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 14th April 2009, 04:14

Tags for this Thread

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.