Results 1 to 2 of 2

Thread: OSX - setMenuBar() - EXC_BAD_ACCESS error - how to re-setMenuBar()

  1. #1

    Default OSX - setMenuBar() - EXC_BAD_ACCESS error - how to re-setMenuBar()

    I am trying to create an application that shows a QMainWindow and multiple QWidget as additional windows. Each window's menu is slightly different. My problem is on MAC.
    Basically, I have to update some actions.
    Each time I create a new window, my constructor is like:

    Qt Code:
    1. QMenuBar * MainWindow::createMenus( MultiEditor * thisWindow )
    2. {
    3. QMenuBar *menuBar;
    4. QMenu *menu;
    5. QMenu *submenu;
    6.  
    7. #ifdef Q_OS_MAC
    8. menuBar = new QMenuBar(0);
    9. #else
    10. menuBar = new QMenuBar();
    11. #endif
    12.  
    13. menu = new QMenu(tr("&File"), this);
    14. menu->addAction( thisWindow->action(MultiEditor::Undo) );
    15.  
    16. return menu;
    17. }
    To copy to clipboard, switch view to plain text mode 


    So, thisWindow is fixed in the moment I create a new window.
    What happens is that, if I change focus and click on a different window, the action doesn't update (it applies to my previous window), as thisWindow has not changed with the focus.

    So I was thinking of resetting the menuBar with a function updateMenuBar(menu) each time the focus changes, and I wrote this:


    Qt Code:
    1. MainWindow::updateMenuBar( QMenuBar * menu )
    2. {
    3. #ifdef Q_QS_MAC
    4. this->setMenuBar(menu)
    5. #endif
    6. }
    To copy to clipboard, switch view to plain text mode 

    When I create the MainWindow, the this->setMenuBar(menu1) works fine and menu1 is set.
    When I create additional windows, this->setMenuBar(menu2) or (menu3) or (...) are set correctly in the moment of the creation.

    But when I change window, and then focus, and I call the function updateMenuBar(menu1) for example, the application crashes and I get the error:

    Thread 1: EXC_BAD_ACCESS (code=1, ...) within the updateMenuBar(...) method.

    Does any of you know if I can continuously re-setMenuBar? I really don't understand what is the best approach for MAC.

    Kind regards,
    Luca

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: OSX - setMenuBar() - EXC_BAD_ACCESS error - how to re-setMenuBar()

    setMenuBar() of QMainWindow will take ownership of the menu and will delete it, you should not reuse it. The solution will be to create a new menu every time the focus changes and then set it as menu bar. Write a factory function to create menus, something like

    Qt Code:
    1. createMenuBar1()
    2. {
    3. QMenuBar * menu = new QMenuBar();
    4.  
    5. // add actions
    6.  
    7. return menu;
    8. }
    9.  
    10. {
    11. ...
    12. updateMenuBar(createMenuBar1());
    13. ...
    14. updateMenuBar(createMenuBar2());
    15. ...
    16. updateMenuBar(createMenuBar3());
    17. ...
    18. updateMenuBar(createMenuBar1());
    19. ...
    20. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 3
    Last Post: 19th November 2014, 19:44
  2. Webview and EXC_BAD_ACCESS
    By jdgrant in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2011, 09:33
  3. : error: [\NokiaQtSDK\Symbian\SDK\epoc32\release\gcce\udeb\V ideo.exe] Error 1
    By ranjit.kadam in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 1st May 2011, 22:11
  4. EXC_BAD_ACCESS error when widget is displayed
    By sparticus_37 in forum Qt Programming
    Replies: 0
    Last Post: 19th August 2010, 06:55
  5. Replies: 5
    Last Post: 17th July 2008, 13:12

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.