Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Strange behaviour of QPushButton
Forum Updated to NodeBB v4.3 + New Features

Strange behaviour of QPushButton

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpython
8 Posts 2 Posters 966 Views 2 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.
  • Bored NerdB Offline
    Bored NerdB Offline
    Bored Nerd
    wrote on last edited by
    #1

    Hi guys, I'm trying to create a simple game on Python with PyQt5.
    f06735a4-418e-4d93-adef-99d915801128-image.png
    which the user has to guess correctly the flag , if he presses the right button (name of the flag displayed in the label ) the program will update the label with another flag and update all buttons text which one of them contains the right answer .
    If he presses the wrong button, nothing happens .
    Basically for the GUI I did it on QtDesigner and I converted it to py through pyuic5 on cmd.
    The problem is that when I run the program, initially it works for the first flag but not for others :
    for example now the program displays the flag Kenya so I press the first button (Kenya).
    Now the program updates the label (so another flag) and the buttons's text. Let's suppose that the next flag is Germany, if I press a random button (so a wrong answer) , the program updates the label and the buttons's text, it's as if I did right even though the answer is totally wrong.
    There's another problem and that is : it basically skips some countries without any valid reason, it's hard to explain so I invite you to go to my github and run the script (first you have to unzip the flagcode.zip and put all the files inside a folder called "flagcode", it must be in the same directory of runme.py)
    Sometimes it displays an empty label , this is not a problem because there are some files which contain some foreign characters , I can easily fix it but that's not the main problem.
    The main problem like I said before is that these buttons behave in a strange way , I already did a debug test but I didn't manage to solve anything.
    I would appreciate if you guys can explain to me the mistake I did, thank you very much!
    this is the project : Guess-the-Flag

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

      Hi and welcome to devnet,

      From the looks of it, you will end up with all buttons at some point connect and thus they will all trigger the same slot.

      So either:

      • disconnect all button before setting them up again
      • associate the number to the button, connect them all once and in the slot check whether it's the correct button that was pressed

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

      Bored NerdB 1 Reply Last reply
      1
      • Bored NerdB Offline
        Bored NerdB Offline
        Bored Nerd
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          From the looks of it, you will end up with all buttons at some point connect and thus they will all trigger the same slot.

          So either:

          • disconnect all button before setting them up again
          • associate the number to the button, connect them all once and in the slot check whether it's the correct button that was pressed
          Bored NerdB Offline
          Bored NerdB Offline
          Bored Nerd
          wrote on last edited by
          #4

          @SGaist Hi ,I choose the first way (because for me it's simple and sincerely I don't know how to do the second way ) but the program crashes after 2 flags

          self.pushButton_1.disconnect()
          self.pushButton_2.disconnect()
          self.pushButton_3.disconnect()
          self.pushButton_4.disconnect()
          

          I put them inside the function randomflag(self) before the call function self.retranslateUi()

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

            What does the crash tell you ?

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

            Bored NerdB 1 Reply Last reply
            0
            • Bored NerdB Offline
              Bored NerdB Offline
              Bored Nerd
              wrote on last edited by
              #6

              @SGaist the classic and generic error :
              79e90787-d59c-4161-b35a-c5b30e5b05e4-image.png

              1 Reply Last reply
              0
              • SGaistS SGaist

                What does the crash tell you ?

                Bored NerdB Offline
                Bored NerdB Offline
                Bored Nerd
                wrote on last edited by
                #7

                @SGaist By the way, I found the bug, thanks for the help!

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

                  What was the issue ?

                  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