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);