Results 1 to 7 of 7

Thread: QMainWindow height wrong when menu is global

  1. #1
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QMainWindow height wrong when menu is global

    I have a QtGui application with a straightforward menu bar. It works fine on Windows (XP/Vista/W7) and on Ubuntu 11.04.

    On Ubuntu 12.04 (Unity) the menu bar is automatically moved by the window manager (presumably) to the global menu, where it works correctly, but the window isn't resized to allow for the missing menu bar. Instead, the centralWidget is moved up, leaving a gap at the bottom.

    I've been unable to find any way to fix this. I don't seem to be able to find a way to adjust the height of the window programmatically. I've tried QMainWindow::setGeometry(...) and QMainWindow::adjustSize() (at the end of the mainWindow constructor) by way of experiment, but neither seems to make any difference.

    Am I missing something obvious?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMainWindow height wrong when menu is global

    I am not sure how Unity does that and whether it's reasonable to expect the QMainWindow to notice and adapt. First, be sure the gap at the bottom is not an empty status bar.

    It's possible the window manager probably isn't hijacking the menu bar until the window is shown. Try creating a slot that resizes the window, and arrange to trigger it with a zero-length single shot timer in the constructor.
    Qt Code:
    1. // end of constructor
    2. QTimer::singleShot(0, this, SLOT(fiddleSize()));
    3. }
    4.  
    5. void fiddleSize() {
    6. // compute the desired size.
    7. resize(...);
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow height wrong when menu is global

    Thanks for that suggestion.

    No, it's not an empty status bar. I actually have two main windows (not open at the same time), one with and one without a status bar, and they both have the same problem. In the case of the one without a status bar there's just a gap at the bottom, and in the other case there's a gap between the centralWidget and the status bar.

    I tried your suggestion and I'm afraid it didn't work. I even added a 1000 msec delay and it still didn't work.

    On the face of it this might suggest that QMainWindow has no knowledge of the fact that the menu bar is missing, but then the gap would be at the top, not the bottom.

    Oh well, it's not the end of the world. The product's currently aimed at a Windows market. I develop it in Linux (and then rebuild under Windows) because it makes for much easier communication with the server, but I've no immediate plans for a Linux version so I'll come back to this issue later. Perhaps by then Qt will have adapted QMainWindow to cope with this situation.

    Many thanks for your help anyway!

    PS One other thing has just occurred to me as possibly relevant. The resize() (or setGeometry(), or whatever) has no effect at all. The initial size of the QMainWindow is set in Qt Creator, after which it seems to be impossible to change it programmatically. This is why I thought I must be missing something. Any thoughts on this?
    Last edited by Exeunt Omnes; 13th June 2012 at 11:33. Reason: Added PS

  4. #4
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow height wrong when menu is global

    I decided in the end simply to over-ride the Unity menu bar handling by adding
    Qt Code:
    1. CoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar, true);
    To copy to clipboard, switch view to plain text mode 
    to main() before the QMainWindow is created. It's non-standard for Unity, but it works and it looks right.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMainWindow height wrong when menu is global

    Quote Originally Posted by Exeunt Omnes View Post
    PS One other thing has just occurred to me as possibly relevant. The resize() (or setGeometry(), or whatever) has no effect at all. The initial size of the QMainWindow is set in Qt Creator, after which it seems to be impossible to change it programmatically. This is why I thought I must be missing something. Any thoughts on this?
    That sheds a different light: if you are actually unable to resize the window at all then removing the menu bar will move the content up but the lower bound of the window cannot follow. Since a QMainWindow is fully resizable by default then there is something you are doing to constrain your main window itself, or to its central widget or layout.

    Does this stand-alone example behave properly when Unity steals the menu bar?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow: public QMainWindow {
    4. Q_OBJECT
    5. public:
    6. MainWindow(QWidget *p = 0): QMainWindow(p) {
    7. QAction *exitAct = new QAction(tr("&Exit"), this);
    8. connect(exitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
    9. QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    10. fileMenu->addAction(exitAct);
    11.  
    12. QFrame *frame = new QFrame(this);
    13. frame->setFrameStyle(QFrame::Box);
    14. setCentralWidget(frame);
    15. }
    16. };
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication app(argc, argv);
    21. MainWindow m;
    22. m.show();
    23. return app.exec();
    24. }
    25. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow height wrong when menu is global

    Well, that worked!

    I'll see if I can fathom out what is preventing the resizing. If not, I'll be back, but in any case many thanks for your help!
    Last edited by Exeunt Omnes; 14th June 2012 at 12:37.

  7. #7
    Join Date
    Jun 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow height wrong when menu is global

    It looks as though the problem is connected with my using Qt Designer for the QMainWindow. I've adapted the example to use a custom QFrame created using Qt Designer, and that works, but as long as there's a setupUi() call in the mainWindow constructor the QMainWindow size seems to be fixed by the UI and programmatically unchangeable.

    If I'm right about this the solution seems to be to decouple the UI for the centralWidget from the mainWindow setup by moving it to a QFrame created in Qt Designer (so I can keep the existing xml code and use Qt Designer for maintenance and updates), but set the menu, centralWidget (and status bar if any) programmatically in the mainWindow constructor. This is a bit of a pain, but it could be a lot worse!

Similar Threads

  1. how to change the position of menu of QMainWindow
    By yxmaomao in forum Qt Programming
    Replies: 11
    Last Post: 17th September 2016, 16:12
  2. Replies: 1
    Last Post: 16th May 2011, 06:11
  3. Replies: 10
    Last Post: 28th February 2011, 19:53
  4. Replies: 1
    Last Post: 18th February 2009, 08:34
  5. how to modify menu item height
    By blackfox in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2008, 10:02

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.