我里面QToolButton
有几个QAction
。
问题是我已经为这个工具栏按钮设置了一个图标,我不希望它改变,当我从弹出菜单中
选择一些QAction
(它将设置项目从 selected 更改为文本)时。有什么方法可以得到我需要的东西吗?
头文件QAction
#include <QToolButton>
class FieldButton : public QToolButton
{
Q_OBJECT
public:
explicit FieldButton(QWidget *parent = 0);
};
.cpp 文件
#include "fieldbutton.h"
FieldButton::FieldButton(QWidget *parent) :
QToolButton(parent)
{
setPopupMode(QToolButton::MenuButtonPopup);
QObject::connect(this, SIGNAL(triggered(QAction*)),
this, SLOT(setDefaultAction(QAction*)));
}
这就是我使用它的方式:
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);