Results 1 to 4 of 4

Thread: Calling QMainWindow::show() breaks QMenuBar::cornerWidget()

  1. #1
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Calling QMainWindow::show() breaks QMenuBar::cornerWidget()

    I have an issue that I have been able to recreate in QtCreator :

    Here is my QtCreator .ui file below. It is a empty basic QMainWindow with a QMenuBar containing 5 actions.

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>1085</width>
    10. <height>300</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget"/>
    17. <widget class="QToolBar" name="mainToolBar">
    18. <attribute name="toolBarArea">
    19. <enum>TopToolBarArea</enum>
    20. </attribute>
    21. <attribute name="toolBarBreak">
    22. <bool>false</bool>
    23. </attribute>
    24. </widget>
    25. <widget class="QStatusBar" name="statusBar"/>
    26. <widget class="QMenuBar" name="menuBar">
    27. <property name="geometry">
    28. <rect>
    29. <x>0</x>
    30. <y>0</y>
    31. <width>1085</width>
    32. <height>20</height>
    33. </rect>
    34. </property>
    35. <widget class="QMenu" name="menuFile">
    36. <property name="title">
    37. <string>File</string>
    38. </property>
    39. </widget>
    40. <widget class="QMenu" name="menuEdit">
    41. <property name="title">
    42. <string>Edit</string>
    43. </property>
    44. </widget>
    45. <widget class="QMenu" name="menuCreate">
    46. <property name="title">
    47. <string>Create</string>
    48. </property>
    49. </widget>
    50. <widget class="QMenu" name="menuWindow">
    51. <property name="title">
    52. <string>Window</string>
    53. </property>
    54. </widget>
    55. <widget class="QMenu" name="menuHelp">
    56. <property name="title">
    57. <string>Help</string>
    58. </property>
    59. </widget>
    60. <addaction name="menuFile"/>
    61. <addaction name="menuEdit"/>
    62. <addaction name="menuCreate"/>
    63. <addaction name="menuWindow"/>
    64. <addaction name="menuHelp"/>
    65. </widget>
    66. </widget>
    67. <layoutdefault spacing="6" margin="11"/>
    68. <resources/>
    69. <connections/>
    70. </ui>
    To copy to clipboard, switch view to plain text mode 

    Here is my mainwindow.cpp, my only source file in my project (except the main.cpp which I did not modify).
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QDebug>
    5. #include <QHBoxLayout>
    6. #include <QMenuBar>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. this->setGeometry(100,100,1306,722);
    14. QMenuBar* menuBar = this->menuBar();
    15.  
    16. //creating a QFrame cornerwidget containing a HBoxLayout
    17. QFrame* corner_widget_frame = new QFrame(menuBar);
    18. QHBoxLayout* corner_widget_layout = new QHBoxLayout();
    19. corner_widget_layout->setMargin(4);
    20. corner_widget_layout->setSpacing(0);
    21. corner_widget_frame->setLayout(corner_widget_layout);
    22. corner_widget_frame->setAutoFillBackground(false);
    23. corner_widget_frame->setStyleSheet("background-color: #FF0000;");
    24. menuBar->setCornerWidget(corner_widget_frame);
    25.  
    26. //bring to front
    27. setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
    28. show();
    29. activateWindow();
    30. //bring to front END
    31.  
    32. //create test widget
    33. QWidget* blue_widget = new QWidget(); //no parent
    34. blue_widget->setFixedSize(40,5);
    35. blue_widget->setStyleSheet("background-color:blue;");
    36. //create test widget END
    37.  
    38. //adding my widget to the corner_widget_frame
    39. this->menuBar()->cornerWidget()->layout()->addWidget(blue_widget);
    40. }
    41.  
    42. MainWindow::~MainWindow()
    43. {
    44. delete ui;
    45. }
    To copy to clipboard, switch view to plain text mode 

    If you run this code in the QtCreator, you will see that the blue_widget is barely visible up in the top right corner. The majority of the widget is cut off from the main window. If I resize the main window horizontally the corner widget will appear correctly permanently.

    window_bad.jpg

    However, if you put the bring to front code at the end of the file, after the blue_widget is added, the widget is correctly displayed. I saw that if I comment out the QMainWindow::show() call in the code I posted above, I do not have my problem and the window is correctly displayed (not hidden), giving me the impression that in my current case the call to QMainWindow::show() is not only causing the problem but is also useless.
    window_good.jpg

    I am working on an already existing application, I created this QtCreator project to isolate and replicate the issue. I did not add the QMainWindow::show() call myself in the application. Removing the QMainWindow::show() call in my application fixes my problem and doesn't seem to cause any issue, but I am afraid to remove it since I think it could cause other issues I don't see in my preliminary tests. Also in the application there is a certain period of time and several things happening between the time the bring to front code is executed and the widget is added to the corner widget(if it is added at all), meaning I cannot add my widget when my QMenubar is having it's corner widget created or before the QMainWindow::show() call.

    According to documentation, all QWidget::show() does is litteraly show the widget and its parents. I have tried (in my QtCreator project and the application) to call QWidget::update(), QWidget::updateGeometry() and QWidget::show() on the QMainWindow, the QMenubar, the corner widget and the (blue)test_widget after adding my (blue)test_widget, to no avail. I also tried calling menuBar->adjustSize() but I did not see any changes.

    I have also noticed that if I completely remove the QMenubar (meaning the 5 actions are gone) from my QMainWindow (with the QtCreator ui editor), the QMainWindow::show() call will not cause the issue and my corner widget will display properly.

    So what does QMainWindow::show() do under the hood exactly ? Why is it causing this issue ? Is the QMainWindow::show() necessary ?

    Thanks for the help.

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

    Default Re: Calling QMainWindow::show() breaks QMenuBar::cornerWidget()

    This is interesting, the blue widget should not show at all.

    It is created after its parent is shown, so it should require an explicit show.

    In any case the code to show the main window would usually be where the main window is being used, not inside its constructor, but of course that doesn't explain why the blue widget shows (as it shouldn't).

    Cheers,
    _

  3. #3
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Calling QMainWindow::show() breaks QMenuBar::cornerWidget()

    Hi anda,

    I made the simplest project possible, so you can copy all of it and paste it in QtCreator to see the results.

    You say that the blue widget should not show at all, does that mean that a QWidget should always call QWidget::show on himself after he is added to a QLayout ?

    For the code being in the constructor, I only did it that way to have the simplest code possible to show my issue. I understand and agree with your point.

    With all that said, you made me realize a very important discovery: calling blue_widget->show() at the end of my code in my QtCreator project does fixe the problem, but doing the same in my application does not fix the issue. This is the 1st time I see a different behavior, I thought I had manage to have the same 1:1 behavior between the QtCreator project and my application.

    Thanks for the help, I will keep looking.

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

    Default Re: Calling QMainWindow::show() breaks QMenuBar::cornerWidget()

    Quote Originally Posted by jacobP View Post
    You say that the blue widget should not show at all, does that mean that a QWidget should always call QWidget::show on himself after he is added to a QLayout ?
    No, it depends on the state of the parent when the child is created/added.

    Basically a widget shows all its children when it is shown for the first time, but if you show a widget and then add children, they remain hidden until they are shown explicitly.
    So what you are seeing is some kind of weird hybrid behavior, quite likely a bug.

    Quote Originally Posted by jacobP View Post
    For the code being in the constructor, I only did it that way to have the simplest code possible to show my issue. I understand and agree with your point.
    I was mostly pointing this out as it would then not trigger your problem.
    I.e. the weird behavior would normally not be observable.

    Cheers,
    _

Similar Threads

  1. Prevent QMenuBar in QMainWindow from shrinking
    By Radagast in forum Qt Programming
    Replies: 0
    Last Post: 3rd March 2015, 15:02
  2. Replies: 5
    Last Post: 6th May 2011, 10:51
  3. Styling QMenubar in Qmainwindow in symbian has no effect
    By jaffers in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 30th July 2010, 16:36
  4. QMenuBar hover/click clears QStatusBar in QMainWindow
    By killerwookie99 in forum Qt Programming
    Replies: 0
    Last Post: 16th November 2009, 15:09
  5. where could QPopupMenu show besides QMenuBar !
    By bigbigmoon in forum Newbie
    Replies: 4
    Last Post: 2nd November 2006, 01:03

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.