QT:多个QWidgets共享的MAC菜单只触发MainWindow的功能

时间:2017-03-09 17:03:17

标签: qt

我的应用程序通常结构如下:

class MultiEditor : public QWidget
{
    ...
    MultiSplitter *mSplitter;
    ...
}

MultiEditor::MultiEditor( Main *main, QWidget * parent )
{
    ...
    mActions[SplitHorizontally] = action = new QAction(tr("Split To Right"), this);
    action->setShortcut( tr("Ctrl+P, 3", "Split To Right"));
    connect(action, SIGNAL(triggered()), this, SLOT(splitHorizontally()));
    settings->addAction( action, "editor-split-right", editorCategory);
    ...
}

void MultiEditor::splitHorizontally()
{
    ... do something on &mSplitter (above);
}

和一个MainWindow类:

MainWindow::MainWindow(Main * main)
{
    ...
    mEditors = new MultiEditor(main);
    setCurrentMultiEditor(mEditors);
    ...
    createActions();
    mMenu = createMenus();
    this->setMenuBar(mMenu);
    ...
}

和创建窗口小部件的类SubWindow:

SubWindow::SubWindow( QVariant * splitterData = 0, QWidget * parent = 0 )
{
    ...
    sEditors = new MultiEditor(Main::instance(), this);
    setCurrentMultiEditor(sEditors);
    ...
#ifndef Q_OS_MAC
    QMenuBar *newMenu = main->createMenus();
    newMenu->setSizePolicy(QSizePolicy ::Expanding , QSizePolicy ::Fixed );
    windowLayout->addWidget(newMenu);
#endif
    ...

}

和实际的菜单构造函数:

QMenuBar * MainWindow::createMenus()
{
    QMenuBar *menuBar;
    QMenu *menu;
    QMenu *submenu;
    ...
#ifdef Q_OS_MAC
    menuBar = new QMenuBar(0);
#else
    menuBar = new QMenuBar();
#endif
    ...
    menu->addAction( currentMultiEditor()->action(MultiEditor::SplitHorizontally) );
    ...
    return menuBar;
}

假设我有MainWindow和一个打开的SubWindow。 两者都在里面创建一个带Splitter的MultiEditor。 所有这些在Ubuntu上运行良好:每个动作都与正确的Splitter相关联。例如,如果我单击SubWindow的Splitter,并触发动作" splitHorizo​​ntally",则触发SubWindow的MultiEditor:splitHorizo​​ntally(),以及SubWindow' s分裂器受到影响。

在Mac上不会发生这种情况。在这里,如果我点击SubWindow的Splitter, MainWindow 的mSplitter会受到影响。 我相信点击一个mSplitter,我会把焦点放在那个mSplitter上,这样MAC的无父菜单就会对mSplitter所关注的事情起作用。但它恰好在MainWindow上遇到了mSplitter。

实际上,我可能认为函数 currentMultiEditor()是动态的(每次触发动作时,都会调用不同的MultiEditor),但实际上multiEditor是固定的在创建菜单时(在MAC中,菜单仅在MainWindow上创建,因此currentMultiEditor()将是MainWindow的mEditor,并且在任何时候都可以从任何地方触发操作)?

最适合您的设计是什么? 我也应该在MAC上创建新菜单吗?怎么样?

1 个答案:

答案 0 :(得分:2)

If I understood your question correctly, this looks indeed like a focus issue and I'm not surprised Linux behaves differently than macOS.

Probably on macOS even if you open a window and click somewhere, the focus remains on the parent window, thus calling the unexpected QAction slot.

I think you have 2 options:

希望有所帮助