Reputation: 63
I have created a little UI with a simple toolbar. I am initing the toolbar within the initToolbar
function. This function is within a Class inheriting from QMainWindow
.
void Main_Frame::initToolBar() {
rectangle.setText(rectangle_text);
circle.setText(circle_text);
triangle.setText(triangle_text);
triangle.setObjectName(triangle_id);
circle.setObjectName(circle_id);
rectangle.setObjectName(rectangle_id);
toolBar.addAction(&triangle);
toolBar.addAction(&circle);
toolBar.addAction(&rectangle);
connect(
&toolBar, &QToolBar::actionTriggered, this, [=]() {
process_toolbar_ac_evt(*sender());
});
}
I want any tool bar events to be processed through process_toolbar_ac_ect(QObject obj)
. Within that method, I want to decide what action (within the toolbar) has been triggered.I want to do this by the object
name. Therefore I have given any action an object name. But when I call sender().objectName()
I get an empty QString
.
My suggestion is, that sender returns a pointer to on of my actions that I put to the toolbar. If this is the case, why I get an empty QString
on the sender()
?
void Main_Frame::process_toolbar_ac_evt(QObject &evt) {
if (evt.objectName() == circle_id) {
// If circle action has clicked, to this ...
}
}
Upvotes: 0
Views: 258
Reputation: 1878
As you are connecting to one of QToolBar
's signals the sender()
will be your tool bar object.
Simply use the QAction
argument that is passed in the QToolBar::actionTriggered
signal. That's what it is for.
NB: Avoid QObject::sender()
wherever possible. It virtually breaks the desired decoupling achieved by signals and slots.
Upvotes: 1