[英]How to add an entry to toolbar context menu in qt?
By default the context menu of a toolbar is filled with the names of the toolbars. 默认情况下,工具栏的上下文菜单中填充了工具栏的名称。 I would like to extend this context menu by an additional entry.
我想通过一个额外的条目扩展这个上下文菜单。
I found an example extending the context menu of a QTextEdit element. 我找到了一个扩展QTextEdit元素的上下文菜单的示例。
http://www.qtcentre.org/threads/35166-extend-the-standard-context-menu-of-qtextedit http://www.qtcentre.org/threads/35166-extend-the-standard-context-menu-of-qtextedit
However, it uses the createStandardContextMenu of the QTextEdit class. 但是,它使用QTextEdit类的createStandardContextMenu。 But QToolBar appears not to have that property:
但是QToolBar似乎没有那个属性:
http://doc.qt.io/qt-4.8/qtoolbar.html http://doc.qt.io/qt-4.8/qtoolbar.html
Edit 编辑
Apparently, the default context menu is the one from the QMainWindow. 显然,默认的上下文菜单是QMainWindow中的菜单。
http://doc.qt.io/qt-4.8/qmainwindow.html#createPopupMenu http://doc.qt.io/qt-4.8/qmainwindow.html#createPopupMenu
Unfortunately, I have no clue yet how to add an entry to it. 不幸的是,我不知道如何添加条目。
Edit 编辑
I am working with this source: 我正在使用这个来源:
http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html
No need to derive QToolBar
if you want to provide the same context menu for all the QToolBar
s in your main window, you just need to override createPopupMenu()
in your main window to add your Custom Action to the returned menu like this: 如果要为主窗口中的所有
QToolBar
提供相同的上下文菜单,则无需派生QToolBar
,只需在主窗口中覆盖createPopupMenu()
即可将自定义操作添加到返回的菜单中,如下所示:
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;
}
You need to derive your own class from QToolBar
and override its virtual function contextMenuEvent
: 您需要从
QToolBar
派生自己的类并覆盖其虚函数contextMenuEvent
:
qmytoolbar.h 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 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;
}
If you want to retain standard menu created my main window and add your items to it keep a pointer to your QMainWindow' in your QMyToolBar and modify 'QMyToolBar::contextMenuEvent
: 如果你想保留标准菜单创建我的主窗口并添加你的项目,
QMainWindow' in your QMyToolBar and modify 'QMyToolBar::contextMenuEvent
保留一个指向你的QMainWindow' in your QMyToolBar and modify 'QMyToolBar::contextMenuEvent
的指针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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.