在这里搜索和像 qtcentre 这样的其他地方我已经看到这个问题出现了,但似乎无法让它工作。我有一个带有QSplitter的MainWindow小部件,其中包含两个Pane小部件(从QFrame子类化)。每个窗格都有一个菜单栏,其中包含相同的关联QActions/Shortcuts。
我已经尝试了ShortcutContexts与setShortcutContext()的所有组合。
WindowShortcut和ApplicationShortcut上下文给出了预期的“不明确的快捷方式重载”。
而WidgetShortcut和WidgetWithChildrenShortcut都什么都不做。
如果我手动激活菜单,它们当然可以正常工作。我还尝试使用重载的enterEvent()强制关注父小部件。
有任何想法吗?
谢谢。
主文件
#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;
};
主文件
#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();
}
主程序
HEADERS = main.h
SOURCES = main.cpp
CONFIG += no_keywords
更新:在最后加上使用上下文
的addAction(paneSplitAct)
调用似乎给了我想要的东西。 Pane::createMenus()
Qt::WidgetShortcut
根据我对文档的了解,这应该在小部件中创建一个上下文菜单。我似乎没有得到一个(我假设鼠标右键单击)但没关系,因为我不想要一个。仍然需要eventEvent()
and覆盖才能正确设置焦点。leaveEvent()