Search here and others hosted as qtcentre I saw that this problem arose, but it may not seem like it works. I have a MainWindow widget with a QSplitter that contains two Panel widgets (subclasses from QFrame ). Each panel has a menu bar with identical associated QActions / Shortcuts .
I tried all combinations of ShortcutContexts with setShortcutContext () .
Contexts
WindowShortcut and ApplicationShortcut give the expected "ambiguous label overload."
So far, WidgetShortcut and WidgetWithChildrenShortcut are not doing anything.
If I activate the menu manually, they certainly work fine. I also tried to focus on parent widgets with overloaded enterEvent () .
Any ideas?
thanks.
main.h
#include <QMainWindow> #include <QFrame> QT_BEGIN_NAMESPACE class QAction; class QMenu; class QHBoxLayout; class QSplitter; class QWidget; QT_END_NAMESPACE class Pane: public QFrame { Q_OBJECT public: Pane(QWidget* parent = 0); protected: void enterEvent(QEvent *event); void leaveEvent(QEvent *event); private: void createMenus(); QMenuBar * m_menuBar; private Q_SLOTS: void split(); }; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); private: void createActions(); void createMenus(); void setupUi(QMainWindow *MainWindow); QMenu *fileMenu; QAction *exitAct; QWidget *centralwidget; QHBoxLayout *horizontalLayout; QSplitter *splitter; QFrame *frame; QFrame *frame_2; };
main.cpp
#include <iostream> #include <QApplication> #include <QMainWindow> #include <QSplitter> #include <QFrame> #include <QMenuBar> #include <QBoxLayout> #include "main.h" Pane::Pane(QWidget* parent) : QFrame(parent) { setFrameShape(QFrame::StyledPanel); setFrameShadow(QFrame::Raised); QVBoxLayout *layout = new QVBoxLayout; QFrame::setLayout(layout); m_menuBar = new QMenuBar; QWidget *m_widget = new QWidget; layout->addWidget(m_menuBar); layout->addWidget(m_widget); layout->setContentsMargins(2, 2, 2, 2); show(); createMenus(); } void Pane::enterEvent(QEvent *event) { std::cout << "enter" << std::endl; setFocus(); setStyleSheet("QFrame { border: 1px solid rgb(127, 127, 0); }"); if (focusWidget()) std::cout << "focuswidget = " << focusWidget()->objectName().toUtf8().constData() << std::endl; } void Pane::leaveEvent(QEvent *event) { std::cout << "leave" << std::endl; clearFocus(); setStyleSheet("QFrame { border: 1px solid rgb(64, 64, 64); }"); } void Pane::split() { std::cout << "split pane" << std::endl; } void Pane::createMenus() { QMenu *paneMenu = m_menuBar->addMenu(tr("&Pane")); QAction *paneSplitAct = new QAction(tr("Split"), this); paneSplitAct->setShortcut(Qt::Key_S); paneSplitAct->setShortcutContext(Qt::WidgetWithChildrenShortcut); paneSplitAct->setStatusTip(tr("Split Pane")); connect(paneSplitAct, SIGNAL(triggered()), this, SLOT(split())); paneMenu->addAction(paneSplitAct); } MainWindow::MainWindow() { setupUi(this); createActions(); createMenus(); } void MainWindow::createActions() { exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcuts(QKeySequence::Quit); exitAct->setStatusTip(tr("Exit the application")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); } void MainWindow::createMenus() { fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(exitAct); } void MainWindow::setupUi(QMainWindow *MainWindow) { if (MainWindow->objectName().isEmpty()) MainWindow->setObjectName(QString::fromUtf8("MainWindow")); MainWindow->resize(800, 600); centralwidget = new QWidget(MainWindow); centralwidget->setObjectName(QString::fromUtf8("centralwidget")); horizontalLayout = new QHBoxLayout(centralwidget); horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); splitter = new QSplitter(centralwidget); splitter->setObjectName(QString::fromUtf8("splitter")); splitter->setOrientation(Qt::Horizontal); frame = new Pane(splitter); frame->setObjectName(QString::fromUtf8("frame")); splitter->addWidget(frame); frame_2 = new Pane(splitter); frame_2->setObjectName(QString::fromUtf8("frame_2")); splitter->addWidget(frame_2); horizontalLayout->addWidget(splitter); MainWindow->setCentralWidget(centralwidget); QMetaObject::connectSlotsByName(MainWindow); } int main(int argc, char *argv[]) { QApplication app(argc, argv); app.setOrganizationName("Trolltech"); app.setApplicationName("Application Example"); MainWindow mainWin; mainWin.show(); return app.exec(); }
main.pro
HEADERS = main.h SOURCES = main.cpp CONFIG += no_keywords
UPDATE: Adding a call to addAction(paneSplitAct)
at the end of Pane::createMenus()
in combination with the Qt::WidgetShortcut
context seems to give me what I want.
From what I understand in the documents, it is supposed to create a context menu in widgets. It seems I am not getting one (right click I suppose), but this is normal since I don't want this. eventEvent()
and leaveEvent()
are still required to properly set focus.