Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Disregard Checkbox changes when Cancel is clicked.

    General and Desktop
    3
    8
    1014
    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.
    • HashTagJF
      HashTagJF last edited by kshegunov

      Can someone help me with this? I want to disregard the changes in the checkbox when cancel button is clicked. I used the following codes but it didn't work.

      void MainWindow::on_Cancel_clicked()
      {
      
      if (ui->checkBox->isChecked()){
       ui->checkBox->setChecked(false);
      }
      
      else{
       ui->checkBox->setChecked(true);
      }
      }
      

      [Moved to General and Desktop ~kshegunov]

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        What exactly doesn't work ?

        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 0
        • HashTagJF
          HashTagJF last edited by

          The Cancel button. When I unchecked the checkbox then click cancel the checkbox should still be checked since there's no changes that has been made but when I re open it, it remains unchecked.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Do you mean when you start your application again ?

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

            HashTagJF 1 Reply Last reply Reply Quote 0
            • A
              ambershark last edited by ambershark

              That code is flawed. What if the user did not change the checkbox before hitting cancel? You are forcing a toggle basically when it's potential it wasn't clicked.

              You should track the original state in a variable until either ok or cancel is pressed. If cancel, return it to it's original state, if ok, honor the change.

              And finally, it does sound that you are trying to have a checkbox value persist after the app closes and reopens. If this is the case you need to save it's value somewhere. Check into QSettings for an easy way to handle serialization of options.

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              1 Reply Last reply Reply Quote 0
              • HashTagJF
                HashTagJF @SGaist last edited by

                @SGaist yes when I start my application again..

                1 Reply Last reply Reply Quote 0
                • HashTagJF
                  HashTagJF last edited by

                  my project is like this..
                  0_1480570478181_upload-317a5dfc-c5e7-4c4f-af95-3b989b4434a2

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    @ambershark still has a point. Changing something that hasn't been modified is just plain wrong.

                    The use QSetting to store and load the state of the widget.

                    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 0
                    • First post
                      Last post