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. Qtool Images
QtWS25 Last Chance

Qtool Images

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 541 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.
  • S Offline
    S Offline
    sherief
    wrote on last edited by sherief
    #1

    On the Qt designer, how do you create a button that switches the icon image when clicked?

    Thank you for the assistance!

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

      Hi and welcome to devnet,

      Do you mean a QPushButton with an icon ?

      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
      • S Offline
        S Offline
        sherief
        wrote on last edited by
        #3

        Yes, either QPushButton or QToolButton (both with icons), whichever one would make the most sense

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

          And by click do you mean it's a checkable button ?

          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
          • S Offline
            S Offline
            sherief
            wrote on last edited by
            #5

            This is the current code related to the button:

                self.pushButton = QtWidgets.QPushButton(self.centralwidget)
                self.pushButton.setGeometry(QtCore.QRect(160, 180, 271, 121))
                self.pushButton.setStyleSheet("")
                self.pushButton.setText("")
                icon = QtGui.QIcon()
                
                icon.addPixmap(QtGui.QPixmap("images/Clicking.png"))
                icon.addPixmap(QtGui.QPixmap("images/Off.png"), QtGui.QIcon.Active)
                icon.addPixmap(QtGui.QPixmap("images/On.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
            
            
                self.pushButton.setIcon(icon)
                self.pushButton.setIconSize(QtCore.QSize(300, 300))
                self.pushButton.setCheckable(True)
                self.pushButton.setAutoRepeatDelay(400)
                self.pushButton.setDefault(False)
                self.pushButton.setObjectName("pushButton")
            

            I want the button to be off.png by default, then when I'm clicking on it the icon becomes clicking.png, and when I release the click it becomes On.png
            Currently, it is off.png by default, but clicking on it does not change the icon to clicking.png, and releasing does successfully change the icon to on.png

            Strangely enough though, when I'm clicking on the window (instead of the actual button), the icon switches to clicking.png and goes back to off.png once I release it.
            How can I fix the middle three lines so that the icon becomes clicking.png when I'm holding the click on the button, and then become on.png once I release it? Thanks

            Note: I don't think the code would run as is if you don't have any of the images saved on your computer.

            jsulmJ J.HilkJ 2 Replies Last reply
            0
            • S sherief

              This is the current code related to the button:

                  self.pushButton = QtWidgets.QPushButton(self.centralwidget)
                  self.pushButton.setGeometry(QtCore.QRect(160, 180, 271, 121))
                  self.pushButton.setStyleSheet("")
                  self.pushButton.setText("")
                  icon = QtGui.QIcon()
                  
                  icon.addPixmap(QtGui.QPixmap("images/Clicking.png"))
                  icon.addPixmap(QtGui.QPixmap("images/Off.png"), QtGui.QIcon.Active)
                  icon.addPixmap(QtGui.QPixmap("images/On.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
              
              
                  self.pushButton.setIcon(icon)
                  self.pushButton.setIconSize(QtCore.QSize(300, 300))
                  self.pushButton.setCheckable(True)
                  self.pushButton.setAutoRepeatDelay(400)
                  self.pushButton.setDefault(False)
                  self.pushButton.setObjectName("pushButton")
              

              I want the button to be off.png by default, then when I'm clicking on it the icon becomes clicking.png, and when I release the click it becomes On.png
              Currently, it is off.png by default, but clicking on it does not change the icon to clicking.png, and releasing does successfully change the icon to on.png

              Strangely enough though, when I'm clicking on the window (instead of the actual button), the icon switches to clicking.png and goes back to off.png once I release it.
              How can I fix the middle three lines so that the icon becomes clicking.png when I'm holding the click on the button, and then become on.png once I release it? Thanks

              Note: I don't think the code would run as is if you don't have any of the images saved on your computer.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @sherief Use https://doc.qt.io/qt-5/qabstractbutton.html#pressed and https://doc.qt.io/qt-5/qabstractbutton.html#released signals to change the icon.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S sherief

                This is the current code related to the button:

                    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
                    self.pushButton.setGeometry(QtCore.QRect(160, 180, 271, 121))
                    self.pushButton.setStyleSheet("")
                    self.pushButton.setText("")
                    icon = QtGui.QIcon()
                    
                    icon.addPixmap(QtGui.QPixmap("images/Clicking.png"))
                    icon.addPixmap(QtGui.QPixmap("images/Off.png"), QtGui.QIcon.Active)
                    icon.addPixmap(QtGui.QPixmap("images/On.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
                
                
                    self.pushButton.setIcon(icon)
                    self.pushButton.setIconSize(QtCore.QSize(300, 300))
                    self.pushButton.setCheckable(True)
                    self.pushButton.setAutoRepeatDelay(400)
                    self.pushButton.setDefault(False)
                    self.pushButton.setObjectName("pushButton")
                

                I want the button to be off.png by default, then when I'm clicking on it the icon becomes clicking.png, and when I release the click it becomes On.png
                Currently, it is off.png by default, but clicking on it does not change the icon to clicking.png, and releasing does successfully change the icon to on.png

                Strangely enough though, when I'm clicking on the window (instead of the actual button), the icon switches to clicking.png and goes back to off.png once I release it.
                How can I fix the middle three lines so that the icon becomes clicking.png when I'm holding the click on the button, and then become on.png once I release it? Thanks

                Note: I don't think the code would run as is if you don't have any of the images saved on your computer.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @sherief

                this seems like a good point to use stylesheets, I would think.
                Not sure if the setup is the same for Python, but I would think so:

                self.pushButton.setStyleSheet("QPushButton:{border-image:url(:/images/Off.png);}QPushButton:pressed{border-image:url(:/images/Clicking.png);}QPushButton:checked{(border-image:url(:/images/On.png);}");
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1

                • Login

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