I have QToolButton
with a couple QAction
s inside it.
The problem is that I've set an icon for this toolbar button and I don't want it to change, when I'm selecting some QAction
(it changes set item to the text from selected QAction
) from popup menu.
Is there any qt-way to get what I need?
header file
#include <QToolButton>
class FieldButton : public QToolButton
{
Q_OBJECT
public:
explicit FieldButton(QWidget *parent = 0);
};
cpp file
#include "fieldbutton.h"
FieldButton::FieldButton(QWidget *parent) :
QToolButton(parent)
{
setPopupMode(QToolButton::MenuButtonPopup);
QObject::connect(this, SIGNAL(triggered(QAction*)),
this, SLOT(setDefaultAction(QAction*)));
}
This is how I use it:
FieldButton *fieldButton = new FieldButton();
QMenu *allFields = new QMenu();
// ... filling QMenu with all needed fields of QAction type like:
QAction *field = new QAction(tr("%1").arg(*h),0);
field->setCheckable(true);
allFields->addAction(field);
// ...
fieldButton->setMenu(allFields);
fieldButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
fieldButton->setIcon(QIcon(":/field.png"));
fieldButton->setText("My text");
fieldButton->setCheckable(true);
toolbar->addWidget(fieldButton);
So, I dug a little in the QToolButton
source code here and it looks like this behavior is hardcoded in the sense that the QToolButton
class listens for the action triggered
signal and updates the button default action accordingly (QToolButton::setDefaultAction)
You can probably connect to the same signal and reset the QToolButton icon at your will.
BTW this looks a rather sensible behavior given that your actions are checkable and wrapped within a QToolButton.