自动填充“编辑”菜单在当前焦点小部件上下文菜单的菜单中 [英] Automatically populate "Edit" menu in menubar with current focus widget context menu

查看:197
本文介绍了自动填充“编辑”菜单在当前焦点小部件上下文菜单的菜单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找方法来实现Qt应用程序的编辑菜单。 编辑菜单包含诸如复制,剪切,粘贴等项目,需要转发到当前活动的小部件。



似乎找不到一个标准或优雅的方式来做到这一点。根据这个问题,这是不可能的:



如何实现编辑菜单,其中撤消,剪切,粘贴和复制?



我最近想到在显示编辑菜单时触发当前活动窗口小部件的上下文菜单事件,通过:

  //在MainWindow构造函数中创建菜单
...
edit_menu = menuBar > addMenu(tr(& Edit));
connect(edit_menu,SIGNAL(aboutToShow()),this,SLOT(showEditMenu()));
...

//自定义槽来处理编辑菜单
void MainWindow :: showEditMenu()
{
QWidget * w = QApplication: :focusWidget();

//在菜单栏中显示当前焦点窗口部件的上下文菜单
QPoint global_pos = edit_menu-> mapToGlobal(edit_menu-> rect()。bottomLeft()); $ b¥b QPoint pos = w-> mapFromGlobal(global_pos);

QApplication :: sendEvent(w,new QContextMenuEvent(QContextMenuEvent :: Keyboard,pos,global_pos));
}

这显示当前窗口小部件的上下文菜单,但有一些问题。例如,它使焦点远离菜单栏,或者如果您首先点击不同的菜单项,菜单栏具有焦点等。



一个部分解决方案从窗口小部件抓取上下文菜单,并将其项目动态复制到编辑菜单。有没有办法做到这一点?



是否有更好的方法在Qt中编辑菜单?

感谢您的帮助。

解决方案

,您可以始终从小部件的 actions 执行操作。要为小部件创建编辑操作,您可以这样做:

  void MainWindow :: addActions(QWidget * widget)
{

QAction * copyAction = new QAction(copy,widget);
if(connect(copyAction,SIGNAL(triggered()),widget,SLOT(copy()))
{
widget-> addAction(copyAction);
qDebug()<<success connection;
}
}

  foreach(QObject * obj,centralWidget() - > children())
{
QWidget * w = dynamic_cast< QWidget *> obj);
if(w)
addActions(w);
}

那么你总是可以更新编辑菜单的动作, >

这可能不优雅,但更好,比不可能。例子中的主要不好的假设是复制槽被命名为 copy


I've been looking for ways to implement the "Edit" menu of Qt application. The "Edit" menu contains items such as "Copy", "Cut", "Paste", etc. and which need to forward to the currently active widget.

I can't seem to find a standard or elegant way to do this. According to this question, it's not possible:

How to implement the "Edit" menu with "Undo", "Cut", "Paste" and "Copy"?

I recently had the idea to trigger a context menu event on the current active widget when the "Edit" menu is shown, via:

// create menus in MainWindow constructor
...
edit_menu = menuBar()->addMenu(tr("&Edit"));
connect(edit_menu, SIGNAL(aboutToShow()), this, SLOT(showEditMenu()));
...

// custom slot to handle the edit menu
void MainWindow::showEditMenu()
{
    QWidget* w = QApplication::focusWidget();

    // show the context menu of current focus widget in the menubar spot
    QPoint global_pos = edit_menu->mapToGlobal(edit_menu->rect().bottomLeft());
    QPoint pos = w->mapFromGlobal(global_pos);

    QApplication::sendEvent(w, new QContextMenuEvent(QContextMenuEvent::Keyboard, pos, global_pos));
}

This shows the a context menu for the current widget great, but has some problems. For example, it takes focus away from the menubar, or if you click a different menubar item first, the menubar has focus, etc.

One partial solution would be to grab the context menu from the widget and copy it's items into the edit menu dynamically. Is there a way to do this?

Is there a better way build the edit menu in Qt?

Thanks for your help.

解决方案

well, if you just need to create menu, you can always take actions from actions of a widget. To create edit actions for widgets, you can do something like this:

void MainWindow::addActions (QWidget* widget)
{

    QAction * copyAction = new QAction("copy",widget);
    if(connect(copyAction,SIGNAL(triggered()),widget,SLOT(copy())))
    {
        widget->addAction(copyAction);
        qDebug()<<"success connection";
    }
}

and

foreach (QObject * obj, centralWidget()->children())
{
    QWidget * w = dynamic_cast<QWidget*>(obj);
    if (w)
        addActions(w);
}

then you always can update actions of edit menu with focused widget's actions

This may be not elegant, but it better, than imporssible. The main bad assumption in example is that copy slot is named copy

这篇关于自动填充“编辑”菜单在当前焦点小部件上下文菜单的菜单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆