Important: Please read the Qt Code of Conduct -

setting text to disabled widget action enables the widget

  • i have a disabled widget action
    the widget is a tool button
    when i set text to button's action, the button and action become enabled
    why is this?

    class WidgetAction : public QWidgetAction
    	explicit WidgetAction(QObject* parent = nullptr);
    	QToolButton *getButton() const;
    	QWidget* createWidget(QWidget* parent) override;
    WidgetAction::WidgetAction(QObject* parent /* = nullptr */)
    	: QWidgetAction(parent)
    QToolButton *WidgetAction::getButton() const
    	auto lstWidgets = createdWidgets();
    	return !lstWidgets.isEmpty() ? static_cast<QToolButton *>( : nullptr;
    QWidget* WidgetAction::createWidget(QWidget* parent)
    	auto pAction = new QAction(parent);
    	connect(pAction, &QAction::triggered, [=]() { ... });
    	auto pButton = new QToolButton(parent);
    	return pButton;
    // USAGE
    m_pWidget = new WidgetAction(this);
    insertAction(pFirstAction, m_pWidget);

    later, when i set the text (actually i need to set the tooltip only, but the issue still remains for tooltip too):

    auto pButton = m_pWidget->getButton();
    if (auto pAction = pButton ? pButton->defaultAction() : nullptr)

    after this the disabled button becomes enabled.
    p.s. the widget action is added to toolbar.

  • Lifetime Qt Champion


    From the looks of it, each time you call getButton, you in fact create a new button.

Log in to reply