Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to change texts and image by button multiple times
Forum Updated to NodeBB v4.3 + New Features

How to change texts and image by button multiple times

Scheduled Pinned Locked Moved Solved Game Development
10 Posts 3 Posters 1.4k 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.
  • milhaoM Offline
    milhaoM Offline
    milhao
    wrote on last edited by milhao
    #1

    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?

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      You connect to the button’s clicked signal. From there you call setPixmap() and setText() 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.

      Software Engineer
      The Qt Company, Oslo

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

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        milhaoM 2 Replies Last reply
        2
        • Axel SpoerlA Axel Spoerl

          You connect to the button’s clicked signal. From there you call setPixmap() and setText() 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.

          milhaoM Offline
          milhaoM Offline
          milhao
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • SGaistS SGaist

            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.

            milhaoM Offline
            milhaoM Offline
            milhao
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • SGaistS SGaist

              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.

              milhaoM Offline
              milhaoM Offline
              milhao
              wrote on last edited by
              #6

              @SGaist where the state machine and keyboard is outside the class or inside the class

              SGaistS 1 Reply Last reply
              0
              • Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by Axel Spoerl
                #7

                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;
                    }
                }
                

                Software Engineer
                The Qt Company, Oslo

                1 Reply Last reply
                1
                • milhaoM milhao

                  @SGaist where the state machine and keyboard is outside the class or inside the class

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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 ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  milhaoM 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @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 ?

                    milhaoM Offline
                    milhaoM Offline
                    milhao
                    wrote on last edited by milhao
                    #9

                    @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

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

                      Sure you can use the same button for the sequence.

                      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

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved