Can I add multistate to a single button

Im looking to reduce the interface by adding multiple press state to a single button.

Rather than have individual buttons for Auto,  On,  Off. I would like to have a sinle button "State" when pressed 3 times would cycle throught Auto, On and Off. On the 4th press it would be back to Auto. I guess i could do it with a dropdown selection list also.

Any thoughts?


  • edited September 2021

    You could simply do it in a slot connected to the button's clicked signal.

    Just increment a variable each click, the fourth click resetting the value.

    Try this out, see if it does what you want:


    (function () {


        // a dialog

        var wDlg = new DzDialog();

        wDlg.caption = "tri-state button";

        wDlg.width = 200;

        wDlg.height = 200;


        //a button

        var wTriButton = new DzPushButton(wDlg);


        // set the initial state of the button

        var nState = 0// <--- THIS hold the button state, read it from wherever

        wTriButton.text = "Off";


        // make it pretty

        var wLayout = new DzGridLayout(wDlg);



        // connect a toggle counter



        // records the state of the button for each click

        function doToggle() {


            nState += 1;

            if (nState > 2) {

                nState = 0;



            //update the button text




        // updates the button text based on the toggle state

        function updateButton() {


            var txt = "";

            switch (nState) {

                case 0:

                    txt = "Off";


                case 1:

                    txt = "On";


                case 2:

                    txt = "Auto";




            // set the button's text

            wTriButton.text = txt;






    edited text("Off") to "Off" so it "just works"

    Post edited by ghost_of_delete_key on
  • Qt's UI guidelines say that to select exactly one from a set of mutually exclusive options you users would expect to see radio buttons inside a group because all the choices are apparent, or a combo box if space is a premium. I think a command button might be slightly confusing because a command button is supposed to initiate an action, and it won't be obvious to the user what the button does, what the states are, etc..

    In your case, I think a combobox is most appropriate.

  • TugpsxTugpsx Posts: 496

    @ghost_of_delete_key Thanks this is what i was looking for but as TheMysteryIsThePoint said it may be best to present the users with the combobox rather than having to explain that the button has 3 states. It was a good Idea at one point. I will find future use for the assisted code again Thanks.

  • Darwin called this...




Sign In or Register to comment.