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] Change behaviour of QPushButton?

[Solved] Change behaviour of QPushButton?

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

    Hi,
    I have a QPushButton in the main window of my application.
    What I want is that, when the user clicks it, it should not be restored back to its raised position. I want that the pushbutton should remain pressed visually. How do I do that?
    & I want to again restore the state of the button to raised in a function dependin upon some variables.
    How do I do this?
    Also I am using a SLOT which is triggered whenever the clicked() SIGNAL is emitted by the Push Button. So what I want is that when the button is in the pressed down state I dont want the SIGNAL clicked() to be emitted. Or atleast is there some way by which I can compare value of a property so as to bypass the code in the SLOT.
    Thank You.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bootchk
      wrote on last edited by
      #2

      bq. If you need toggle behavior (see setCheckable()) or a button that auto-repeats the activation signal when being pushed down like the arrows in a scroll bar (see setAutoRepeat()), a command button is probably not what you want. When in doubt, use a tool button.

      I think what you want is called "toggle behavior". In other words, you want a QToolButton instead of a QPushButton, which is a "command" button, meaning specific things about what it means to the user and how it behaves visually (goes down but comes right back up.) The documentation for QPushButton seems easy to understand, but it is full of subtle meaning, for example, what "toggle" means.

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        "toggleness" or whatever you call it is nothing unusual for QPushButton too. Just set the "checkable" property and connect to toggled(bool) instead of clicked().

        An example:
        @
        //connect stuff
        ui->button->setCheckable( true ); //or corresponding checkbox in the designer
        connect(ui->button, &QPushButton::toggled, this, &MainWindow::someSlot);

        //and then...
        void MainWindow::someSlot(bool checked)
        {
        if(checked) //button is "down"
        {
        //do stuff...
        ui->button->setChecked(false); // "raise" the button
        }
        else //button is "up"
        {
        //do other stuff or ignore...
        }
        }
        @

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

          Yes, one can "roll your own" behavior by using properties of the superclass QAbstractButton. But then you should probably worry about user friendliness, or user interface guidelines.

          Back up in the design process, and ask whether the button is "starting an action" (QPushButton, which is a command) or "changing a state" (QToolButton.) Then possibly the original question becomes moot, since the QToolButton might behave as desired (unless you are really designing a novel user interface technique.)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            There are more differences between QPushButton and QToolButton that would make you choose either the one or the other. The checkable nature of them is, IMHO, not one of them.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bootchk
              wrote on last edited by
              #6

              Yes, I'm wrong, I didn't read far enough about QToolButton. Both can be setCheckable and QToolButton doesn't default to checkable, it also can be used for "commands or options", where an option has a state.

              And the difference between a command and a state is fuzzy. A VCR button (play, pause, fastforward) is commanding a process (the player) to start, but also putting that process in a state (playing).

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                And to complicate the matter even further, the button may also be used to communicate that state to the user... :-)

                1 Reply Last reply
                0
                • CAD_codingC Offline
                  CAD_codingC Offline
                  CAD_coding
                  wrote on last edited by
                  #8

                  Hi @Krzysztof Kawa,
                  Thank You man!!!
                  That solution worked :-)
                  Previously I was trying everything with setChecked() & the various SIGNAL's of QPushButton but could not get the desired behaviour.
                  In your code I saw setCheckable() & then I tried again & it worked.
                  I think it was foolish of me not to find that previously.
                  Thank You Andre & bootchk as well :-)

                  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