如何确定 QAction 的来源?
我想知道是否有任何简单/优雅的方法来确定 QAction 的来源? 我的意思是我有一个 QAction 添加到多个 QWidget(使用 QWidget::addAction ),当发出操作的触发信号时,我需要知道哪个 QWidget 是源,因为我需要查询该特定的小部件。
我没有看到任何方法可以用 QAction 本身来做到这一点,无论从哪里触发操作,父级总是相同的。
我必须解决这个问题的想法是保存一个指向活动小部件的全局指针,但是当小部件接收焦点以将其设置为活动小部件时,Qt 中似乎没有优雅的方式来捕获事件。
另一种方法是为每个小部件使用不同的 QAction,但在我的情况下,最好使用一个,因为用户可以自定义操作的快捷方式,而且我认为使用相同的快捷方式和文本进行多个操作并不是一个好的设计等等,它们都执行相同的操作,但在不同的小部件上运行。
PS:大多数操作都可以通过快捷方式(启用小部件上下文,因此小部件无论如何都需要焦点)来触发,也可以通过该小部件上的自定义上下文菜单来触发。
如果有助于理解我的问题,我可以提供一个简单的代码示例:
// init the action etc.
void Widget::init()
{
QAction *action = new QAction("Do Something");
action->setShortcut("Ctrl+X");
action->setShortcutContext(Qt::WidgetShortcut);
connect(action, SIGNAL(triggered()), SLOT(action_triggered()));
// assume the widgets 1 to 3 already exist (type QWidget* off course)
widget1->addAction(action);
widget2->addAction(action);
widget3->addAction(action);
}
// SLOT action_triggered()
void Widget::action_triggered()
{
QWidget *source;
// TODO: which widget is the source: widget1, widget2 or widget3?
}
I want to know if there is any easy/elegant way to determine the source of an QAction?
What I mean I have one QAction which is added to multiple QWidgets (with QWidget::addAction) and when the triggered signal of the action gets emitted I need to know which QWidget is the source because I need to query that specific widget.
I don't see any way to do this with the QAction itself, the parent is always the same no matter from where the actions gets triggered.
The idea I had to solve this was to save a global pointer to the active widget, but again there seems to be no elegant way in Qt to capture the event when a widget receives the focus to set it as the active one.
Another way would be to use different QActions for each widget, but in my case it's better to use one because the user can customize the shortcuts of the actions and I don't think it's good design to have multiple actions with the same shortcuts and text etc. which all do the same but operate on different widgets.
P.S.: Most of the actions can be triggered either by shortcut (with widget context enabled so the widget need focus anyways) and also from a custom context menu on that widget.
I can provide a simple code example if that helps to understand my problem:
// init the action etc.
void Widget::init()
{
QAction *action = new QAction("Do Something");
action->setShortcut("Ctrl+X");
action->setShortcutContext(Qt::WidgetShortcut);
connect(action, SIGNAL(triggered()), SLOT(action_triggered()));
// assume the widgets 1 to 3 already exist (type QWidget* off course)
widget1->addAction(action);
widget2->addAction(action);
widget3->addAction(action);
}
// SLOT action_triggered()
void Widget::action_triggered()
{
QWidget *source;
// TODO: which widget is the source: widget1, widget2 or widget3?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以使用
QAction
linkedWidgets 方法来确定添加了操作的小部件列表。然后您可以检查此列表中当前具有焦点的小部件。I think you can use
QAction
associatedWidgets method to determine the list of widgets, to which action was added. Then you can examine this list for widget, that currently has focus.