[Qt-interest] Copy QMenuBar?

Israel Brewster israel at frontierflying.com
Fri Mar 26 21:11:15 CET 2010


On Mar 25, 2010, at 3:54 PM, Israel Brewster wrote:

> I have a ui form created in QtDesigner with a menu bar. When a certain slot is called, this form creates and shows another window. I want this other window to have the same menu bar as the first window, with all the actions of this menu bar still connected to the slots of the first window. So far I haven't had any luck. I am using Qt 4.6.2 on Mac OS X 10.6.2. I've tried the following:
> 
> 1) QMenuBar * otherMenu=new QMenuBar(this->menuBar()). This didn't give me any errors, but no menu either. From further research, this would seem to make sense as it would appear the copy constructor is disabled for QMenuBar.
> 
> 2) calling this->menuBar()->setParent(0) to make the menuBar global. The problem with this approach is that I can have any number of instances of my main form, and each one needs it's own menu so the signal/slot connections go to the right class. Not to mention what would happen if the first window was closed.
> 
> 3) transferring ownership of the menubar from the main window to the child window when I create the child window using setMenuBar(this->menuBar). This almost worked, but the menu items that were in the special menus (such as the preferences and about items that were in the Application menu) disappear.
> 
> Is there perhaps a way to create a menu bar from a ui form? Or is there a different approach that would work better? Thanks.

Ok, I managed to get a kludgy and ugly solution to this by modifying #3 as per the following code:

static bool firstSwitch=true;
        minView.setMenuBar(menuBar());
        if(firstSwitch)
        {
            QMenu * helpMenu=minView.menuBar()->addMenu("Help");
            helpMenu->addAction(ui->actionAbout);
            helpMenu->addAction(ui->actionPreferences);
            firstSwitch=false;
        }

This produces the desired behavior of both the main window and the secondary window having the (apparently) same menu bar. However, I rather dislike this "solution" For one thing, I'm not even sure why it works - I just know it does, which makes me rather uncomfortable. Does anyone have any ideas for the "right" way to accomplish my goal? Thanks.

> -----------------------------------------------
> Israel Brewster
> Computer Support Technician II
> Era Alaska
> 5245 Airport Industrial Rd
> Fairbanks, AK 99709
> (907) 450-7250 x7293
> -----------------------------------------------
> 
> <Israel Brewster.vcf>
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-----------------------------------------------
Israel Brewster
Computer Support Technician II
Era Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x7293
-----------------------------------------------



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100326/e0fc0590/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Israel Brewster.vcf
Type: text/directory
Size: 417 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100326/e0fc0590/attachment.bin 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100326/e0fc0590/attachment-0001.html 


More information about the Qt-interest-old mailing list