Results 1 to 11 of 11

Thread: Is it possible to move main menubar down?

  1. #1
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Thumbs down Is it possible to move main menubar down?

    I want to place some graphic/banner above the main QMenuBar provided by QMainWindow. Does anyone know a way to do this? I tried setting the x,y geometry in QMenubar and QMainWindow, but it didn't change a thing - it seems the menubar is stuck in the top left-hand corner of the window.

    Has anyone ever tried subclassing QMenuBar to do this? Or is there a different way I should go about this...
    Software Engineer



  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to move main menubar down?

    The menu bar is probably positioned using a layout. You can peek at the sources of QMainWindow class to look for the layout and how it is used and then gain access to the layout (for example by calling it by name, if necessary) and insert an additional widget in a place you want it in.

  3. The following user says thank you to wysota for this useful post:

    gfunk (29th August 2006)

  4. #3
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Is it possible to move main menubar down?

    Interesting... I tried accessing the QMainWindowLayout object and tried adding buttons to it, but it would just crash. Ick... I wonder if anyone has actually delved into this. the QMainWindowLayout class looks like a monster, 2500 lines... all that docking widget support code...

    Alternatively, maybe it would be easier to just re-implement the menubar into a dialog so that it can be embedded elsewhere on the main window. I wonder if QToolButtons with just text inside, placed on a QToolBar, will look sufficiently similar to the menubar...
    Software Engineer



  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to move main menubar down?

    Not a very good idea. I would stick to the layout thing. Just try to correct your code, I'm sure it'll work.

  6. The following user says thank you to wysota for this useful post:

    gfunk (29th August 2006)

  7. #5
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Is it possible to move main menubar down?

    Well, here's what I quickly tried to come up with: (there's a lot more code that I had to expose in order to get into QMainWindowPrivate - included below)

    Qt Code:
    1. QMainWindowPrivate* p = reinterpret_cast<QMainWindowPrivate*>(d_ptr);
    2. QLayoutItem *li0 = p->layout->takeAt(0);
    3. QLayoutItem *li1 = p->layout->takeAt(0);
    4. QLayoutItem *li2 = p->layout->takeAt(0);
    5. p->layout->addWidget(new QPushButton);
    6. p->layout->addWidget(li0->widget());
    7. p->layout->addWidget(li1->widget());
    8. p->layout->addWidget(li2->widget());
    To copy to clipboard, switch view to plain text mode 

    Naturally it doesn't crash right away; probably when it's drawing. Wondering if this is the right way to go. Seems ugly to have to remove the previous items and then re-add them after my pushbutton (but I suppose it must be done that way since no insertWidget method exists). Just wondering if this is the right way to go...

    (ugliness included to get this to compile):
    Qt Code:
    1. #include "C:/Qt/4.1.4/src/gui/widgets/qmainwindowlayout_p.h"
    2. #include "C:/Qt/4.1.4/src/gui/kernel/qwidget_p.h"
    3. class QMainWindowPrivate : public QWidgetPrivate
    4. {
    5. Q_DECLARE_PUBLIC(QMainWindow)
    6. public:
    7. inline QMainWindowPrivate()
    8. : layout(0), toolButtonStyle(Qt::ToolButtonIconOnly)
    9. { }
    10. QMainWindowLayout *layout;
    11. QSize iconSize;
    12. bool explicitIconSize;
    13. Qt::ToolButtonStyle toolButtonStyle;
    14. void init();
    15. };
    To copy to clipboard, switch view to plain text mode 
    Software Engineer



  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to move main menubar down?

    I would try to reach proper objects by calling them by name. They are all QObject and as such they are accessible through QObject::findChildren().

  9. The following user says thank you to wysota for this useful post:

    gfunk (29th August 2006)

  10. #7
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Is it possible to move main menubar down?

    Ah, you're right, I can get the layout much easier... even calling layout() works. But I still can't seem to add anything to that QMainWindowLayout.
    Software Engineer



  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to move main menubar down?

    Did you try simply using QMainWindow::setMenuWidget() and passing a complex widget composed of your custom data and a QMenuBar laid out vertically?

    Qt Code:
    1. +-------------------+
    2. |QWidget w. v-layout|
    3. | +---------------+ |
    4. | | CUSTOM WIDGET | |
    5. | +---------------+ |
    6. | | QMenuBar | |
    7. | +---------------+ |
    8. +-------------------+
    To copy to clipboard, switch view to plain text mode 

    Edit: Note that it's available since Qt4.2. In earlier versions you have to stick with mangling with the layout directly.

  12. The following user says thank you to wysota for this useful post:

    gfunk (30th August 2006)

  13. #9
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Is it possible to move main menubar down?

    Oh wow! I was using 4.1 and had no idea they added that to 4.2. Thanks, I will have to check that out.
    Software Engineer



  14. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it possible to move main menubar down?

    This should do the trick in Qt 4.1:
    Qt Code:
    1. menuBar()->setParent(statusBar());
    To copy to clipboard, switch view to plain text mode 
    I can't assure it won't break something but it's worth try..
    J-P Nurmi

  15. #11
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Is it possible to move main menubar down?

    Quote Originally Posted by jpn
    This should do the trick in Qt 4.1:
    Qt Code:
    1. menuBar()->setParent(statusBar());
    To copy to clipboard, switch view to plain text mode 
    Interesting. I tried it and noticed that it could make the menubar take up the window space of any widget that I called setParent() on. Unfortunately, the menubar QAction's can't pop up any dialogs, ending up in crash. Ah well, was definitely worth the try.
    Software Engineer



Similar Threads

  1. Replies: 5
    Last Post: 4th August 2006, 23:44
  2. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  3. Replies: 3
    Last Post: 31st March 2006, 18:38
  4. Main window
    By Michiel in forum Qt Tools
    Replies: 1
    Last Post: 20th March 2006, 23:54
  5. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.