Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved setting text to disabled widget action enables the widget

    General and Desktop
    2
    2
    78
    Loading More Posts
    • 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
      user4592357 last edited by user4592357

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • First post
          Last post