Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. setting text to disabled widget action enables the widget
Forum Updated to NodeBB v4.3 + New Features

setting text to disabled widget action enables the widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 222 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved