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. Changing Checkbox Label Based on Check State

Changing Checkbox Label Based on Check State

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 734 Views
  • 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.
  • P Offline
    P Offline
    Phamy1289
    wrote on last edited by
    #1

    I want to have the checkbox label change based on the state it is in. I'm using the connect but it doesn't seem to be working. What am I doing wrong?

    pRequirementCheckbox1 = new QCheckBox("Unselected");
    pRequirementCheckbox1->setTristate(true);
    connect(pRequirementCheckbox1, SIGNAL(stateChanged(int)), this, SLOT(onSelectRequirements(pRequirementCheckbox1)));

    void ScheduleAdvice::onSelectRequirements(QCheckBox *checkbox)
    {
    if(checkbox->checkState() == Qt::Checked)
    {
    checkbox->setText("Selected");
    }
    else if(checkbox->checkState() == Qt::PartiallyChecked)
    {
    checkbox->setText("Must Meet");
    }
    else
    {
    checkbox->setText("Unselected");
    }
    }

    65e80dc0-0f0c-4777-87a6-a6f39c852e69-image.png

    Pl45m4P 1 Reply Last reply
    0
    • P Phamy1289

      I want to have the checkbox label change based on the state it is in. I'm using the connect but it doesn't seem to be working. What am I doing wrong?

      pRequirementCheckbox1 = new QCheckBox("Unselected");
      pRequirementCheckbox1->setTristate(true);
      connect(pRequirementCheckbox1, SIGNAL(stateChanged(int)), this, SLOT(onSelectRequirements(pRequirementCheckbox1)));

      void ScheduleAdvice::onSelectRequirements(QCheckBox *checkbox)
      {
      if(checkbox->checkState() == Qt::Checked)
      {
      checkbox->setText("Selected");
      }
      else if(checkbox->checkState() == Qt::PartiallyChecked)
      {
      checkbox->setText("Must Meet");
      }
      else
      {
      checkbox->setText("Unselected");
      }
      }

      65e80dc0-0f0c-4777-87a6-a6f39c852e69-image.png

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Phamy1289 said in Changing Checkbox Label Based on Check State:

      connect(pRequirementCheckbox1, SIGNAL(stateChanged(int)), this, SLOT(onSelectRequirements(pRequirementCheckbox1)));

      I don't think this will ever work...

      Try:

      connect(pRequirementCheckbox1, SIGNAL(stateChanged(int)), this, SLOT(onSelectRequirements(int)));
      
      void ScheduleAdvice::onSelectRequirements(int state)
      {
        // Access your checkBox and check for state...
      }
      

      If you dont have your checkBox in your ui then you could use lambdas:

      connect(pRequirementCheckbox1, &QCheckBox::stateChanged, this, [&] () {
      
      if(pRequirementCheckbox1->checkState() == Qt::Checked)
      {
      pRequirementCheckbox1->setText("Selected");
      }
      else if(pRequirementCheckbox1->checkState() == Qt::PartiallyChecked)
      {
      pRequirementCheckbox1->setText("Must Meet");
      }
      else
      {
      pRequirementCheckbox1->setText("Unselected");
      }
      }
      });
      

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Phamy1289
        wrote on last edited by
        #3

        What if I have multiple buttons created dynamically that I want to do the same?

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

          Have a look at QButtonGroup Class

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Phamy1289
            wrote on last edited by
            #5

            Not buttons, sorry. I mean checkboxes. I need to dynamically create multiple checkboxes and have them all do the same.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              Have you ever read the doc ?

              The QButtonGroup class provides a container to organize groups of button widgets.
              QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.
              An exclusive button group switches off all checkable (toggle) buttons except the one that has been clicked. By default, a button group is exclusive. The buttons in a button group are usually checkable QPushButtons, QCheckBoxes (normally for non-exclusive button groups), or QRadioButtons. If you create an exclusive button group, you should ensure that one of the buttons in the group is initially checked; otherwise, the group will initially be in a state where no buttons are checked.
              A button can be added to the group with addButton() and removed with removeButton(). If the group is exclusive, the currently checked button is available with checkedButton(). If a button is clicked, the buttonClicked() signal is emitted; for a checkable button in an exclusive group this means that the button has been checked. The list of buttons in the group is returned by buttons().
              In addition, QButtonGroup can map between integers and buttons. You can assign an integer id to a button with setId(), and retrieve it with id(). The id of the currently checked button is available with checkedId(), and there is an overloaded signal buttonClicked() which emits the id of the button. The id -1 is reserved by QButtonGroup to mean "no such button". The purpose of the mapping mechanism is to simplify the representation of enum values in a user interface.
              See also QGroupBox, QPushButton, QCheckBox, and QRadioButton.

              P 1 Reply Last reply
              1
              • M mpergand

                Have you ever read the doc ?

                The QButtonGroup class provides a container to organize groups of button widgets.
                QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.
                An exclusive button group switches off all checkable (toggle) buttons except the one that has been clicked. By default, a button group is exclusive. The buttons in a button group are usually checkable QPushButtons, QCheckBoxes (normally for non-exclusive button groups), or QRadioButtons. If you create an exclusive button group, you should ensure that one of the buttons in the group is initially checked; otherwise, the group will initially be in a state where no buttons are checked.
                A button can be added to the group with addButton() and removed with removeButton(). If the group is exclusive, the currently checked button is available with checkedButton(). If a button is clicked, the buttonClicked() signal is emitted; for a checkable button in an exclusive group this means that the button has been checked. The list of buttons in the group is returned by buttons().
                In addition, QButtonGroup can map between integers and buttons. You can assign an integer id to a button with setId(), and retrieve it with id(). The id of the currently checked button is available with checkedId(), and there is an overloaded signal buttonClicked() which emits the id of the button. The id -1 is reserved by QButtonGroup to mean "no such button". The purpose of the mapping mechanism is to simplify the representation of enum values in a user interface.
                See also QGroupBox, QPushButton, QCheckBox, and QRadioButton.

                P Offline
                P Offline
                Phamy1289
                wrote on last edited by
                #7

                @mpergand My apologies. I read it and I'm worried if it will be able to interact with the checkboxes as they are in tristate. Would the toggle() signal work for it?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Phamy1289
                  wrote on last edited by
                  #8

                  I figured it out. I forget to set the sender message in my custom slot. Used @Pl45m4 first set of code. Thanks

                  1 Reply Last reply
                  0

                  • Login

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