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. [Solved] Toggle button by label only
Forum Updated to NodeBB v4.3 + New Features

[Solved] Toggle button by label only

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.1k Views 1 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.
  • C Offline
    C Offline
    Ccmc
    wrote on last edited by
    #1

    I have a checkable QPushButton which I use to toggle a function as indicated by its button text. However, in addition, the button raises and lowers with each toggle--I want the button to appear unpressed in both states.

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean that you want your button to act as if he was not checkable ?

      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
      0
      • C Offline
        C Offline
        Ccmc
        wrote on last edited by
        #3

        I don't think so. Let me rephrase: I want my button to be able to toggle between two actions, but the only indication of this should be its changing text.

        1 Reply Last reply
        1
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          connect the button to a slot where you toggle the text back and forth.
          You can set and check the state e.g. by using dynamic properties when you do not want to subclass QPushButton.

          But i would recomment subclassing QPushButton and manage the satet with a member variable.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Ccmc
            wrote on last edited by
            #5

            I'd still like the button to be checkable though, just without the sustained pressed effect. Is there perhaps a way to "release" the button without untoggling it?

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              may i ask why you don't want this?

              It's possible by:
              @
              void MyButton::mousePressEvent(QMouseEvent* event)
              {
              this->setChecked( ! this->isChecked() ); //toggle checked state
              //don't call base-class implementation to avoid push effect of button;
              //not calling the base class implementation may have other side effects you may have to take care of yourself, but i can't think of any right now.
              this->update();
              }
              @

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NicuPopescu
                wrote on last edited by
                #7

                Hi,

                try this by designer or code:

                1. set the button as flat

                2. set stylesheet background and ::focus the same color: for non focus you should provide in style sheet also a border

                  i.e.

                @#pushButtonTv
                {
                border:0px solid rgba(0,0,0,0);
                background-color:rgba(255,255,255,255);
                }

                #pushButtonTv:focus
                {
                background-color:rgba(255,255,255,255);
                }@

                1. set it as checkable and checked as the first state

                2. change text:

                @if(ui->pushButton->isChecked())
                ui->pushButton->setText("Checked");
                else
                ui->pushButton->setText("Unchecked");@

                hope it helps!

                Cheers!

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Ccmc
                  wrote on last edited by
                  #8

                  Raven-worx:

                  I tried your second suggestion first - it worked for appearances, but as you predicted, it was too buggy to bother fixing.

                  I had originally wanted to use a checkable button for the simplicity of being able to use pushButton->isChecked(), but based on your first suggestion I switched to using pushButton->text() == "Status 1", which wasn't so hard either.

                  Thank you!

                  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