如何在 qt 中添加一个条目到工具栏上下文菜单?
How to add an entry to toolbar context menu in qt?
默认情况下,工具栏的上下文菜单中会填入工具栏的名称。我想通过一个额外的条目来扩展这个上下文菜单。
我找到了一个扩展 QTextEdit 元素的上下文菜单的示例。
http://www.qtcentre.org/threads/35166-extend-the-standard-context-menu-of-qtextedit
但是,它使用了 QTextEdit 的 createStandardContextMenu class。但是 QToolBar 似乎没有 属性:
http://doc.qt.io/qt-4.8/qtoolbar.html
编辑
显然,默认的上下文菜单是来自 QMainWindow 的菜单。
http://doc.qt.io/qt-4.8/qmainwindow.html#createPopupMenu
不幸的是,我还不知道如何向其中添加条目。
编辑
我正在使用这个来源:
http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html
您需要从 QToolBar
派生出您自己的 class 并覆盖它的虚函数 contextMenuEvent
:
qmytoolbar.h
#ifndef QMYTOOLBAR_H
#define QMYTOOLBAR_H
#include <QToolBar>
class QMyToolBar : public QToolBar
{
Q_OBJECT
public:
explicit QMyToolBar(QWidget *parent = 0)
: QToolBar(parent){}
protected:
void contextMenuEvent(QContextMenuEvent *event);
};
#endif // QMYTOOLBAR_H
qmytoolbar.cpp
#include "qmytoolbar.h"
#include <QMenu>
#include <QContextMenuEvent>
void QMyToolBar::contextMenuEvent(QContextMenuEvent *event)
{
// QToolBar::contextMenuEvent(event);
QMenu *menu = new QMenu(this);
menu->addAction(tr("My Menu Item"));
menu->exec(event->globalPos());
delete menu;
}
如果您想保留我创建的标准菜单 window 并向其中添加您的项目,请保持指向您的 QMainWindow' in your QMyToolBar and modify 'QMyToolBar::contextMenuEvent
:
void QMyToolBar::contextMenuEvent(QContextMenuEvent *event)
{
// QToolBar::contextMenuEvent(event);
QMenu *menu =
//new QMenu(this);
m_pMainWindow->createPopupMenu();
menu->addAction(tr("My Menu Item"));
menu->exec(event->globalPos());
delete menu;
}
如果您想为主 window 中的所有 QToolBar
提供相同的上下文菜单,则无需派生 QToolBar
,您只需覆盖 createPopupMenu()
在您的主 window 中将您的自定义操作添加到返回的菜单中,如下所示:
QMenu* MainWindow::createPopupMenu(){
//call the overridden method to get the default menu containing checkable entries
//for the toolbars and dock widgets present in the main window
QMenu* menu= QMainWindow::createPopupMenu();
//you can add whatever you want to the menu before returning it
menu->addSeparator();
menu->addAction(tr("Custom Action"), this, SLOT(CustomActionSlot()));
return menu;
}
默认情况下,工具栏的上下文菜单中会填入工具栏的名称。我想通过一个额外的条目来扩展这个上下文菜单。
我找到了一个扩展 QTextEdit 元素的上下文菜单的示例。
http://www.qtcentre.org/threads/35166-extend-the-standard-context-menu-of-qtextedit
但是,它使用了 QTextEdit 的 createStandardContextMenu class。但是 QToolBar 似乎没有 属性:
http://doc.qt.io/qt-4.8/qtoolbar.html
编辑
显然,默认的上下文菜单是来自 QMainWindow 的菜单。
http://doc.qt.io/qt-4.8/qmainwindow.html#createPopupMenu
不幸的是,我还不知道如何向其中添加条目。
编辑
我正在使用这个来源:
http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html
您需要从 QToolBar
派生出您自己的 class 并覆盖它的虚函数 contextMenuEvent
:
qmytoolbar.h
#ifndef QMYTOOLBAR_H
#define QMYTOOLBAR_H
#include <QToolBar>
class QMyToolBar : public QToolBar
{
Q_OBJECT
public:
explicit QMyToolBar(QWidget *parent = 0)
: QToolBar(parent){}
protected:
void contextMenuEvent(QContextMenuEvent *event);
};
#endif // QMYTOOLBAR_H
qmytoolbar.cpp
#include "qmytoolbar.h"
#include <QMenu>
#include <QContextMenuEvent>
void QMyToolBar::contextMenuEvent(QContextMenuEvent *event)
{
// QToolBar::contextMenuEvent(event);
QMenu *menu = new QMenu(this);
menu->addAction(tr("My Menu Item"));
menu->exec(event->globalPos());
delete menu;
}
如果您想保留我创建的标准菜单 window 并向其中添加您的项目,请保持指向您的 QMainWindow' in your QMyToolBar and modify 'QMyToolBar::contextMenuEvent
:
void QMyToolBar::contextMenuEvent(QContextMenuEvent *event)
{
// QToolBar::contextMenuEvent(event);
QMenu *menu =
//new QMenu(this);
m_pMainWindow->createPopupMenu();
menu->addAction(tr("My Menu Item"));
menu->exec(event->globalPos());
delete menu;
}
如果您想为主 window 中的所有 QToolBar
提供相同的上下文菜单,则无需派生 QToolBar
,您只需覆盖 createPopupMenu()
在您的主 window 中将您的自定义操作添加到返回的菜单中,如下所示:
QMenu* MainWindow::createPopupMenu(){
//call the overridden method to get the default menu containing checkable entries
//for the toolbars and dock widgets present in the main window
QMenu* menu= QMainWindow::createPopupMenu();
//you can add whatever you want to the menu before returning it
menu->addSeparator();
menu->addAction(tr("Custom Action"), this, SLOT(CustomActionSlot()));
return menu;
}