Qt Forum

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

    [Solved] How to Toggle qpushbutton name

    General and Desktop
    2
    10
    2170
    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.
    • H
      herculis last edited by

      hello guys i want to toggle qpushbutton name means when i hit the push button once, push button should have name as "add" and when i hit it again it should have sub and when i hit it gain it should show again add.

      How can i do it
      plz help me guys
      Thanks

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

        Hi,

        Use a slot connected to the clicked() signal, or if you are using a checkable button the toggled(bool) signal.

        In the slot, verify the current text and change it accordingly or if your button is checkable, update the name based on whether the button is checked or not.

        [edit: corrected wrong wording]

        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
        • H
          herculis last edited by

          @addButton = new QPushButton(tr("add"));
          connect(addButton, SIGNAL(clicked()),this, SLOT(slotButtonaddPressed()));

          void abcd:slotButtonaddPressed()
          {
          hideButton->setText(tr("sub"));
          }
          @

          I have done this now when i clicked 3rd time it shows "sub" only i want add when i press 3rd time how can i do that
          thanks

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

            You haven't implemented the "verify the button name" part

            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
            • H
              herculis last edited by

              verify means
              how can i do it
              Help

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

                Verify means: test what the current QPushButton text is and then set the other one.

                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
                • H
                  herculis last edited by

                  with which function i can check the name of pushbutton i know i have to use it in If statement
                  Can u give on example related to my one.
                  Thanks

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

                    Use the "text":http://qt-project.org/doc/qt-5/qabstractbutton.html#text-prop property

                    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
                    • H
                      herculis last edited by

                      @if(addButton->text=="add")
                      {
                      addButton->setText(tr("sub"));
                      }
                      else
                      {
                      addButton->setText(tr("add"));
                      }@
                      i wrote like this but it not working it not building the program

                      1 Reply Last reply Reply Quote 0
                      • H
                        herculis last edited by

                        Thank you very much SGaist it worked
                        Thanks alot

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post