Second call to setMenuBar does nothing
-
Hi, ALL,
I'm trying to develop a program with wxWidgets onn top of Qt.I have somewhat outdated Qt version - 5.12 and the recent wxWidgets.
I'm testIng on the Gentoo platform (Linux).
The program I'm trying to develop is based on the MDI paradigm and so it has different views for a different types of documents. Every view should have its own menu bar.
When the program starts - there is nothing. Only basic menu bar and the toolbar.
When the user clicks the tool on the toolbar a new menu bar is made and should be set as well as new toolbar should be created.The toolbar is created, the view and the document are made, but the menu bar is not changed.
It looks like the general recommendation is to have only one menu bar for the life time of the program. However it is not the case here.
Basically I'm calling the setMenuBar() on the main frame, but the call has no effect.
Does anybody know how to properly update the menu bar of the program with the brand new menu bar?
Thank you.
@oneeyeman1 said in Second call to setMenuBar does nothing:
a new menu bar is made and should be set
So, you're trying to replace existing menu bar in main window? Please show the code where you're doing this.
-
@oneeyeman1 said in Second call to setMenuBar does nothing:
I'm trying to develop a program with wxWidgets on top of Qt.
Surely the menu bar is a wxWidgets issue and not a Qt issue.
-
Hi, ALL,
I'm trying to develop a program with wxWidgets onn top of Qt.I have somewhat outdated Qt version - 5.12 and the recent wxWidgets.
I'm testIng on the Gentoo platform (Linux).
The program I'm trying to develop is based on the MDI paradigm and so it has different views for a different types of documents. Every view should have its own menu bar.
When the program starts - there is nothing. Only basic menu bar and the toolbar.
When the user clicks the tool on the toolbar a new menu bar is made and should be set as well as new toolbar should be created.The toolbar is created, the view and the document are made, but the menu bar is not changed.
It looks like the general recommendation is to have only one menu bar for the life time of the program. However it is not the case here.
Basically I'm calling the setMenuBar() on the main frame, but the call has no effect.
Does anybody know how to properly update the menu bar of the program with the brand new menu bar?
Thank you.
-
@oneeyeman1
As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?@JonB said in Second call to setMenuBar does nothing:
@oneeyeman1
As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?Is there a siimple standalone Qt program I can try?
wxWidgets do not do anything - just calls setMenuBar and that's it (after checking that existing and the new are not the same).
There is no crash, no assert - menu bar is stays the same.
Maybe there was a bug in 5.12?
IgorsGentoo /home/igor/dbhandler_new/Debug # emerge -pv qtcore * Last emerge --sync was 3y 55d 17h 58m 58s ago. These are the packages that would be merged, in order: Calculating dependencies... done! [ebuild R ] dev-qt/qtcore-5.15.2-r14:5/5.15.2::gentoo USE="icu -debug -old-kernel -systemd -test" 0 KiB Total: 1 package (1 reinstall), Size of downloads: 0 KiB * IMPORTANT: 20 news items need reading for repository 'gentoo'. * Use eselect news read to view new items.
Thank you.
-
@JonB said in Second call to setMenuBar does nothing:
@oneeyeman1
As @ChrisW67 has written, is this a wxWidgets issue? Does it work in a small, standalone Qt program with no wxWidgets?Is there a siimple standalone Qt program I can try?
wxWidgets do not do anything - just calls setMenuBar and that's it (after checking that existing and the new are not the same).
There is no crash, no assert - menu bar is stays the same.
Maybe there was a bug in 5.12?
IgorsGentoo /home/igor/dbhandler_new/Debug # emerge -pv qtcore * Last emerge --sync was 3y 55d 17h 58m 58s ago. These are the packages that would be merged, in order: Calculating dependencies... done! [ebuild R ] dev-qt/qtcore-5.15.2-r14:5/5.15.2::gentoo USE="icu -debug -old-kernel -systemd -test" 0 KiB Total: 1 package (1 reinstall), Size of downloads: 0 KiB * IMPORTANT: 20 news items need reading for repository 'gentoo'. * Use eselect news read to view new items.
Thank you.
@oneeyeman1
You just write a small, standalone example Qt widgets program withsetMenyBar()
, calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know. -
@oneeyeman1
You just write a small, standalone example Qt widgets program withsetMenyBar()
, calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.@JonB said in Second call to setMenuBar does nothing:
@oneeyeman1
You just write a small, standalone example Qt widgets program withsetMenyBar()
, calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.Doesn't Qt comes with a set of sample code?
The ones people can quickly build and test something if it doesn't work or just check something?Thank you.
-
@JonB said in Second call to setMenuBar does nothing:
@oneeyeman1
You just write a small, standalone example Qt widgets program withsetMenyBar()
, calling it twice or maybe using the Qt MDI stuff if that is necessary.. Just without wxWidgets. Then either it works or you have small code people can look at. Without code nobody can know.Doesn't Qt comes with a set of sample code?
The ones people can quickly build and test something if it doesn't work or just check something?Thank you.
@oneeyeman1 said in Second call to setMenuBar does nothing:
Doesn't Qt comes with a set of sample code?
Qt comes with "hundreds" of examples, and the web is full of further "hundreds" of examples. I guess you could Google
setMenuBar()
if you want to try to find some code calling that.If I understand your claim right, something which includes
QMenuBar *menuBar1, *menuBar2; menuBar1 = new QMenuBar(); mainWindow->setMenuBar(menuBar1) menuBar2 = new QMenuBar(); mainWindow->setMenuBar(menuBar2)
with appropriate items added to the menubars (perhaps
menuBar1->addMenu("menuBar1")
etc,) so we can distinguish them does not work correctly, so I would start from that. -
@oneeyeman1 said in Second call to setMenuBar does nothing:
Doesn't Qt comes with a set of sample code?
Qt comes with "hundreds" of examples, and the web is full of further "hundreds" of examples. I guess you could Google
setMenuBar()
if you want to try to find some code calling that.If I understand your claim right, something which includes
QMenuBar *menuBar1, *menuBar2; menuBar1 = new QMenuBar(); mainWindow->setMenuBar(menuBar1) menuBar2 = new QMenuBar(); mainWindow->setMenuBar(menuBar2)
with appropriate items added to the menubars (perhaps
menuBar1->addMenu("menuBar1")
etc,) so we can distinguish them does not work correctly, so I would start from that.@JonB said in Second call to setMenuBar does nothing:
@oneeyeman1 said in Second call to setMenuBar does nothing:
Doesn't Qt comes with a set of sample code?
Qt comes with "hundreds" of examples, and the web is full of further "hundreds" of examples. I guess you could Google
setMenuBar()
if you want to try to find some code calling that.If I understand your claim right, something which includes
QMenuBar *menuBar1, *menuBar2; menuBar1 = new QMenuBar(); mainWindow->setMenuBar(menuBar1) menuBar2 = new QMenuBar(); mainWindow->setMenuBar(menuBar2)
with appropriate items added to the menubars (perhaps
menuBar1->addMenu("menuBar1")
etc,) so we can distinguish them does not work correctly, so I would start from that.That code snippet you put in is the exact replica.
What I'm claiming is that after the second call to setMenuBar() I will still see the first one displayed. (That's to clarify what's "not work correctly" means).I will try to find some small sample code to test. But I was hoping that you can pint me to some Qt sample from distribution that I can use for testing, as I presume they are the smallest possible ones.
Than you.
-
Hi,
I found the sample here: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/mainwindows/mdi?h=5.15. I will see if I can make t fail as I explained t.Now how can I build it?
Just run:
gcc -o mdi *.cpp
?
I'd guess it will fail miserably... ;-)
Thank you.
-
Hi,
I found the sample here: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/mainwindows/mdi?h=5.15. I will see if I can make t fail as I explained t.Now how can I build it?
Just run:
gcc -o mdi *.cpp
?
I'd guess it will fail miserably... ;-)
Thank you.
@oneeyeman1
You should install Qt Creator and open the.pro
file as a (qmake) project. You need to set up your Compilation Kit, hopefully it will auto-detect the gcc stuff you have. -
Hi guys,
I didn't install Qt-Creator.Instead I ran "qmake". It generated Makefile.
Then I ran "make". And this is what I got:
igor@IgorsGentoo ~/sample $ make g++ -c -pipe -O2 -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I/usr/lib64/qt5/mkspecs/linux-g++ -o main.o main.cpp In file included from mainwindow.h:53, from mainwindow.h:53, ................................................................ from main.cpp:5: mainwindow.h:51:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 51 | #include <QtWidgets> | ^ mainwindow.h:53:24: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 53 | #include "mainwindow.h" | ^ mainwindow.h:54:22: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 54 | #include "mdichild.h" | ^ In file included from mainwindow.h:54, ...................................................................................................... from mainwindow.h:53, from main.cpp:5: mdichild.h:54:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 54 | #include <QTextEdit> | ^ ^Cmake: *** wait: No child processes. Stop. make: *** Waiting for unfinished jobs.... make: *** wait: No child processes. Stop. igor@IgorsGentoo ~/sample $
How do I fix it?
Thank you.
-
Don't recursivly include headers, use include guards to make sure the header is not included more than once. Basic c++ stuff, nothing Qt specific.
-
Hi guys,
I didn't install Qt-Creator.Instead I ran "qmake". It generated Makefile.
Then I ran "make". And this is what I got:
igor@IgorsGentoo ~/sample $ make g++ -c -pipe -O2 -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -I/usr/lib64/qt5/mkspecs/linux-g++ -o main.o main.cpp In file included from mainwindow.h:53, from mainwindow.h:53, ................................................................ from main.cpp:5: mainwindow.h:51:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 51 | #include <QtWidgets> | ^ mainwindow.h:53:24: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 53 | #include "mainwindow.h" | ^ mainwindow.h:54:22: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 54 | #include "mdichild.h" | ^ In file included from mainwindow.h:54, ...................................................................................................... from mainwindow.h:53, from main.cpp:5: mdichild.h:54:21: error: #include nested depth 200 exceeds maximum of 200 (use -fmax-include-depth=DEPTH to increase the maximum) 54 | #include <QTextEdit> | ^ ^Cmake: *** wait: No child processes. Stop. make: *** Waiting for unfinished jobs.... make: *** wait: No child processes. Stop. igor@IgorsGentoo ~/sample $
How do I fix it?
Thank you.
@oneeyeman1
I do not know whether you made changes to that example or there is a problem with it (which I would doubt).In any case, here is a completely standalone single source file program which changes the menubar:
#include <QApplication> #include <QMainWindow> #include <QMenuBar> #include <QTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow w; QMenuBar *menuBar1 = new QMenuBar(); menuBar1->addMenu("menuBar1"); w.setMenuBar(menuBar1); QMenuBar *menuBar2 = new QMenuBar(); menuBar2->addMenu("menuBar2"); w.setMenuBar(menuBar2); w.show(); QMenuBar *menuBar3 = new QMenuBar(); menuBar3->addMenu("menuBar3"); QTimer::singleShot(3000, [&w, menuBar3]() { w.setMenuBar(menuBar3); }); return a.exec(); }
Presumably you can compile and link this, perhaps just as you have done for any wxWidgets you currently have.
The code starts with one menubar, replaces it with a second one immediately, and then replaces it with a third after 3 seconds. Needless to say it works as expected for me (Ubuntu 22.04, Qt 6.x, and I would expect it to work with any Qt version including your Qt5). You might start by checking this for yourself. If you then think that, say, MDI is required to show a problem then it will need altering for that.
-
@oneeyeman1
I do not know whether you made changes to that example or there is a problem with it (which I would doubt).In any case, here is a completely standalone single source file program which changes the menubar:
#include <QApplication> #include <QMainWindow> #include <QMenuBar> #include <QTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow w; QMenuBar *menuBar1 = new QMenuBar(); menuBar1->addMenu("menuBar1"); w.setMenuBar(menuBar1); QMenuBar *menuBar2 = new QMenuBar(); menuBar2->addMenu("menuBar2"); w.setMenuBar(menuBar2); w.show(); QMenuBar *menuBar3 = new QMenuBar(); menuBar3->addMenu("menuBar3"); QTimer::singleShot(3000, [&w, menuBar3]() { w.setMenuBar(menuBar3); }); return a.exec(); }
Presumably you can compile and link this, perhaps just as you have done for any wxWidgets you currently have.
The code starts with one menubar, replaces it with a second one immediately, and then replaces it with a third after 3 seconds. Needless to say it works as expected for me (Ubuntu 22.04, Qt 6.x, and I would expect it to work with any Qt version including your Qt5). You might start by checking this for yourself. If you then think that, say, MDI is required to show a problem then it will need altering for that.
@JonB ,
When I try to compile my program I'm doing "../configure && make -j3".
I can copy the code you posted and just run make, but I presume it will fail as it need the include and library paths.
Can you give me a proper way to build?
Thank you.
-
Don't recursivly include headers, use include guards to make sure the header is not included more than once. Basic c++ stuff, nothing Qt specific.
What's interesting is that the sample code I'm trying to compile is not using setMenuBar() call.
I wonder how it works without it...
Thank you.
-
What's interesting is that the sample code I'm trying to compile is not using setMenuBar() call.
I wonder how it works without it...
Thank you.
@oneeyeman1
You do not normally needsetMenuBar()
.QMainWindow
creates one if you accessmenuBar()
. Then again, I do not know of any code which swaps/uses multiple menubars, which you say is your use case. -
@oneeyeman1
You do not normally needsetMenuBar()
.QMainWindow
creates one if you accessmenuBar()
. Then again, I do not know of any code which swaps/uses multiple menubars, which you say is your use case.@JonB ,
Imagine you are working in the IDE,
You are wring a source code and then realize you want too add some bitmap.
You go to add one, but you menu bar needs to change accordingly.
Thais just one example. In the MDI world its actually expected to change menu bar in accordance to the document you have open.
Thank you.
-
This is mostly done through https://doc.qt.io/qt-6/qaction.html#visible-prop and https://doc.qt.io/qt-6/qaction.html#enabled-prop but not by replacing the entire menu bar...
Please provide a minimal, compilable example to reproduce your problem (without wxwhatever) - @JonB 's example works fine so proove us and Qt wrong.