Solved How to change texts and image by button multiple times
-
I'm trying to make a game and I need to pass its story and I wanted to use a button to change the image and text, how do I do that, several times?
-
You connect to the button’s
clicked
signal. From there you callsetPixmap()
andsetText()
on the object you want to change. If you want to toggle between two or more combinations of pixmaps/texts, you have to implement a counter. -
You connect to the button’s
clicked
signal. From there you callsetPixmap()
andsetText()
on the object you want to change. If you want to toggle between two or more combinations of pixmaps/texts, you have to implement a counter. -
Hi,
In addition to what @Axel-Spoerl wrote, since you mention a story it looks like you will have some linear progress so maybe consider a state machine for that and on each click you'll advance one state. With each state you'll operate the updates required.
-
@Axel-Spoerl ok but how do i make this counter. I tried to do it with if and a form similar to a variable that incremented for each click of the button the variable adds one more and this variable would go to the next situation, but it doesn't work. how would i actually make this counter
-
@SGaist I found this state machine interesting, this like that you suggest it sends to an example that would be similar to what I want?
-
@SGaist where the state machine and keyboard is outside the class or inside the class
-
The state machine is well documented, just examine the examples.
If you want to implement a counter, I wonder if you have heard about static variables within a method and the modulo operator. That’s the simple solution if your text/image pattern always follows the same direction.
Your slot would look like this:Void yourClass::onButtonClicked { const int numberStates = 3; // or whatever the number of your states is static int counter = numberStates -1; ++counter %= numberStates; // increment and start over after last state switch (counter) { case 0: // set first image and text break; case 1: // set second image and text break; case 2: // set third image and text break; } }
-
@milhao said in How to change texts and image by button multiple times:
@SGaist where the state machine and keyboard is outside the class or inside the class
That question is really unclear. Can you explain what the issue is ?
-
@SGaist sorry I couldn't express myself, I just wanted to know if this example would solve my problem with the use of the button for several sequential actions
-
Sure you can use the same button for the sequence.