Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
    {
    public:
    	explicit WidgetAction(QObject* parent = nullptr);
    	QToolButton *getButton() const;
    protected:
    	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 *>(lstWidgets.at(0)) : nullptr;
    }
    
    QWidget* WidgetAction::createWidget(QWidget* parent)
    {
    	auto pAction = new QAction(parent);
    	connect(pAction, &QAction::triggered, [=]() { ... });
    
    	auto pButton = new QToolButton(parent);
    	pButton->setDefaultAction(pAction);
    	pButton->setAutoRaise(true);
    	pButton->setEnabled(someCondition);
    	return pButton;
    }
    
    // USAGE
    
    m_pWidget = new WidgetAction(this);
    insertAction(pFirstAction, m_pWidget);
    m_pWidget->setEnabled(false);
    

    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)
    {
        pAction->setText(...);
    }
    

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


  • Lifetime Qt Champion

    Hi,

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


Log in to reply