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"
  4. #include <QDebug>
  5. #include <QHBoxLayout>
  6. #include <QMenuBar>
  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();
  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);
  26. //bring to front
  27. setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
  28. show();
  29. activateWindow();
  30. //bring to front END
  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
  38. //adding my widget to the corner_widget_frame
  39. this->menuBar()->cornerWidget()->layout()->addWidget(blue_widget);
  40. }
  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.


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.

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.