Results 1 to 6 of 6

Thread: QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

  1. #1

    Default QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

    I have an application generally structured as the following:

    Qt Code:
    1. class MultiEditor : public QWidget
    2. {
    3. ...
    4. MultiSplitter *mSplitter;
    5. ...
    6. }
    7.  
    8. MultiEditor::MultiEditor( Main *main, QWidget * parent )
    9. {
    10. ...
    11. mActions[SplitHorizontally] = action = new QAction(tr("Split To Right"), this);
    12. action->setShortcut( tr("Ctrl+P, 3", "Split To Right"));
    13. connect(action, SIGNAL(triggered()), this, SLOT(splitHorizontally()));
    14. settings->addAction( action, "editor-split-right", editorCategory);
    15. ...
    16. }
    17.  
    18. void MultiEditor::splitHorizontally()
    19. {
    20. ... do something on &mSplitter (above);
    21. }
    To copy to clipboard, switch view to plain text mode 

    and a class MainWindow:

    Qt Code:
    1. MainWindow::MainWindow(Main * main)
    2. {
    3. ...
    4. mEditors = new MultiEditor(main);
    5. setCurrentMultiEditor(mEditors);
    6. ...
    7. createActions();
    8. mMenu = createMenus();
    9. this->setMenuBar(mMenu);
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    and a class SubWindow that creates a widget:

    Qt Code:
    1. SubWindow::SubWindow( QVariant * splitterData = 0, QWidget * parent = 0 )
    2. {
    3. ...
    4. sEditors = new MultiEditor(Main::instance(), this);
    5. setCurrentMultiEditor(sEditors);
    6. ...
    7. #ifndef Q_OS_MAC
    8. QMenuBar *newMenu = main->createMenus();
    9. newMenu->setSizePolicy(QSizePolicy ::Expanding , QSizePolicy ::Fixed );
    10. windowLayout->addWidget(newMenu);
    11. #endif
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 

    and the actual menu constructor:

    Qt Code:
    1. QMenuBar * MainWindow::createMenus()
    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->addAction( currentMultiEditor()->action(MultiEditor::SplitHorizontally) );
    14. ...
    15. return menuBar;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Let's suppose that I have the MainWindow and a open SubWindow. Both create a MultiEditor with a Splitter inside. All this works good on Ubuntu: every action is associated with the right Splitter. If I click on the SubWindow's Splitter, for example, and I trigger the action "splitHorizontally", the SubWindow's MultiEditor:splitHorizontally() is triggered, and the SubWindow's Splitter is affected.

    This does not happen on Mac. Here, if I click on a SubWindow's Splitter, the mSplitter of the MainWindow is affected. I believed that clicking on a mSplitter, I would focus that mSplitter, so the MAC's parentless menu would act on whatever mSplitter focused. But it happens that it get stucked with the mSplitter on the MainWindow.

    Actually, could it be that I was thinking that the function currentMultiEditor() was dynamic (every time the action was triggered, a different MultiEditor was called), but in the truth the multiEditor is fixed when creating the menu (in MAC the menu is created only on the MainWindow, so the currentMultiEditor() would be MainWindow's mEditors, and that would still be anytime the action were triggered from anywhere)?

    What is the best design for you? Should I create new menus on MAC too? How?

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

    Default Re: QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

    I am not sure I understand your setup, but if you have one set of actions that can apply to any of multiple widgets, then the slots need to know which which widget to affect.

    How to you track that currently?

    Cheers,
    _

  3. #3

    Default Re: QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

    Super-thank you, anda_skoa

    well, I have a currentMultiEditor() function that gives me the focused editor.
    It works fine in Linux, and every time I build a new widget (window), I re-set my currentMultiEditor() manually (meaning: I run explicitly a setCurrentMultiEditor() function).

    Still, on MAC, the action is working on one only widget (the MainWindow (the first widget created)), so I don't understand if the menu retrieve the currentMultiEditor() only when it is built, or if it runs the function every time).

    Particularly, in the beginning currentMultiEditor() is an instance called mEditors.
    The following widget (created later) is called sEditors.

    So, when creating the menu in MAC, the currentMultiEditor() == mEditors.
    After I create a new widget (window), I setCurrentMultiEditor(sEditors), I call the action, and the action modifies mEditor again.

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

    Default Re: QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

    Quote Originally Posted by LucaDanieli View Post
    so I don't understand if the menu retrieve the currentMultiEditor() only when it is built, or if it runs the function every time).
    This is C++, the function is called whenever the thread executing the code reaches its invocation.

    Your code snippets show one call in MainWindow::createMenus(), so it is called when createMenus() is called.

    If you also call it in the slots connected to the actions, then it is also called when the actions are triggered.

    Cheers,
    _

  5. #5

    Default Re: QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

    Thank you!
    You are saving the day.

    So basically, I have to update the menu (re-creating it) whenever a new widget is focused?
    Do you think that that would be a good design?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT: MAC menu shared among multiple QWidgets only triggers MainWindow's functions

    Depends on your needs.

    You could also have a single menu and switch in the slots.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 6th December 2016, 20:52
  2. Defining shared class member functions only once
    By Phlucious in forum General Programming
    Replies: 23
    Last Post: 16th January 2012, 22:17
  3. Painting in multiple QWidgets simultaneously
    By edepetete in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2011, 17:21
  4. Multiple QWidgets
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 23rd March 2010, 11:21
  5. Navigate between QWidgets and MainWindow
    By pcaeiro in forum Qt Programming
    Replies: 2
    Last Post: 5th August 2009, 12:05

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.