Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to determine the source of an QAction?
    primarykey
    data
    text
    <p>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 <a href="http://doc.qt.nokia.com/latest/qwidget.html#addAction" rel="nofollow">QWidget::addAction</a>) 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.</p> <p>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.</p> <p>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.</p> <p>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> <p>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.</p> <p>I can provide a simple code example if that helps to understand my problem:</p> <pre><code>// init the action etc. void Widget::init() { QAction *action = new QAction("Do Something"); action-&gt;setShortcut("Ctrl+X"); action-&gt;setShortcutContext(Qt::WidgetShortcut); connect(action, SIGNAL(triggered()), SLOT(action_triggered())); // assume the widgets 1 to 3 already exist (type QWidget* off course) widget1-&gt;addAction(action); widget2-&gt;addAction(action); widget3-&gt;addAction(action); } // SLOT action_triggered() void Widget::action_triggered() { QWidget *source; // TODO: which widget is the source: widget1, widget2 or widget3? } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload