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. QPushButton checkable only by code
Forum Updated to NodeBB v4.3 + New Features

QPushButton checkable only by code

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.8k 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    I have this required scenario:

    • QPushButton that reacts to clicks
    • at run-time, based on some criteria I will set its checked state to true or false

    I mean, when the user clicks on the button it should just fire the clicked signal without change it's checked state. But at the same time I need to change its checked state programmatically whenever is needed.

    Any idea about?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mark81
      wrote on last edited by
      #2

      Solved subclassing QPushButton and by-passing the isCheckable constraint.

      Pablo J. RoginaP 1 Reply Last reply
      3
      • M Mark81

        Solved subclassing QPushButton and by-passing the isCheckable constraint.

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Mark81 if your issue is solved, please don't forget to mark the post as such! Thanks.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mark81
          wrote on last edited by
          #4

          Here a very quick and dirty solution:

          #ifndef CHECKBUTTON_H
          #define CHECKBUTTON_H
          
          #include <QObject>
          #include <QPushButton>
          
          class CheckButton : public QPushButton
          {
              Q_OBJECT
          
          public:
              explicit CheckButton(QWidget *parent = nullptr) : QPushButton(parent) {}
              bool isChecked() const { return _isChecked; }
              void setChecked(bool checked)
              {
                  if (_isChecked == checked) return;
                  _isChecked = checked;
                  if (checked) setStyleSheet("background-color: rgb(255, 85, 0);");
                  else setStyleSheet("");
                  emit checkedChanged(checked);
              }
          
          signals:
              void checkedChanged(bool checked);
          
          private:
              bool _isChecked;
          };
          
          #endif // CHECKBUTTON_H
          
          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