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. How to check the state of a checkbox when a button is clicked.
QtWS25 Last Chance

How to check the state of a checkbox when a button is clicked.

Scheduled Pinned Locked Moved General and Desktop
checkboxmessagebox
9 Posts 3 Posters 13.1k 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.
  • A Offline
    A Offline
    alchemydragon
    wrote on last edited by SGaist
    #1

    I am a newbie at QT. I have some experience with c++ for command line apps and python development. I want to grow my gui development skills. Now to the question. I have some basic code I am playing with to learn. On the ui I have a checkbox named ckbox. In the menu I have a menu item that I want to display two different messages depending on if the checkbox is clicked or not. Here is the code segment that I have right now for the menu item:

     MenuItem {
        text: qsTr("&Message")
        onTriggered: if (ckbox.isChecked === True) {
                                  messageDialog.show(qsTr("If a witch floats,"))
                              } else {
                                  messageDialog.show(qsTr("therefore she is made of wood."))
                             }
    }
    

    The error I am getting is "ReferenceError: ckbox is not defined"
    Thanks in advance for your help! Sorry if my code snippet didn't show up right. Still learning to use the editor tags.

    [edit: Fixed coding tags: thee back-ticks SGaist]

    M 1 Reply Last reply
    0
    • A alchemydragon

      I am a newbie at QT. I have some experience with c++ for command line apps and python development. I want to grow my gui development skills. Now to the question. I have some basic code I am playing with to learn. On the ui I have a checkbox named ckbox. In the menu I have a menu item that I want to display two different messages depending on if the checkbox is clicked or not. Here is the code segment that I have right now for the menu item:

       MenuItem {
          text: qsTr("&Message")
          onTriggered: if (ckbox.isChecked === True) {
                                    messageDialog.show(qsTr("If a witch floats,"))
                                } else {
                                    messageDialog.show(qsTr("therefore she is made of wood."))
                               }
      }
      

      The error I am getting is "ReferenceError: ckbox is not defined"
      Thanks in advance for your help! Sorry if my code snippet didn't show up right. Still learning to use the editor tags.

      [edit: Fixed coding tags: thee back-ticks SGaist]

      M Offline
      M Offline
      Maxim DC
      wrote on last edited by Maxim DC
      #2

      @alchemydragon Try

      onTriggered: if (ckbox.checked == true) 
                   {messageDialog.show(qsTr("If ... floats,"))}
                   else
                   {messageDialog.show(qsTr("therefore ... wood."))}
      

      or try

      onTriggered: if (ckbox.checkedState == Qt.Checked) 
                   {messageDialog.show(qsTr("If ... floats,"))}
                   else
                   {messageDialog.show(qsTr("therefore ... wood."))}
      

      and maybe you have to set id: ckbox in your CheckBox element
      [EDIT] What do you mean "On the ui"

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alchemydragon
        wrote on last edited by
        #3

        I still get the error that ckbox is not defined. The ui is the user interface, the MainForum,ui,qml file. Thnaks for your suggestions.

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

          Use if (ui->ckbox->checked())

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Brad M
            wrote on last edited by
            #5

            Sorry, typo. Should be:

            if (ui->ckbox->isChecked())

            A 1 Reply Last reply
            0
            • B Brad M

              Sorry, typo. Should be:

              if (ui->ckbox->isChecked())

              A Offline
              A Offline
              alchemydragon
              wrote on last edited by
              #6

              @Brad-M i get an "unexpected token '>' with your suggestion. I tried changing the -> to a dot or just the dash and both times I got "ui is undefined" error.

              B 1 Reply Last reply
              0
              • A alchemydragon

                @Brad-M i get an "unexpected token '>' with your suggestion. I tried changing the -> to a dot or just the dash and both times I got "ui is undefined" error.

                B Offline
                B Offline
                Brad M
                wrote on last edited by
                #7

                @alchemydragon did you create the checkbox in the designer or in code?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Brad M
                  wrote on last edited by
                  #8

                  @alchemydragon you need to check the name of your checkbox variable. If you created it with the designer it will be listed as the objectName in the property display panel. If you created it in code can you show me the code where it is declared? Re-reading your first post I see that ckbox is not defined so that is probably the wrong identifier.

                  A 1 Reply Last reply
                  0
                  • B Brad M

                    @alchemydragon you need to check the name of your checkbox variable. If you created it with the designer it will be listed as the objectName in the property display panel. If you created it in code can you show me the code where it is declared? Re-reading your first post I see that ckbox is not defined so that is probably the wrong identifier.

                    A Offline
                    A Offline
                    alchemydragon
                    wrote on last edited by
                    #9

                    @Brad-M I made it in Designer. I double checked the id box. It has ckbox. Sorry I am just now getting back to you. It has been busy at work this week.

                    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