Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Toggling QPixmap image
Forum Updated to NodeBB v4.3 + New Features

Toggling QPixmap image

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 2 Posters 701 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.
  • cerrC Offline
    cerrC Offline
    cerr
    wrote on last edited by cerr
    #1

    Hi,

    I need to display a "flashing" QPixmap.
    I currently have a static pixmap that gets displayed like so: self.Lab1Icon.setPixmap(self.red) and I want it to toggle between two different pixmaps (let's say self.red & self.clr). How do I best go about this? Should I use the QTimer class' interval feature and update a global to toggle between two states?

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

      Hi,

      No, globales are not needed. Just have a list containing your various pixmap, a counter and in the slot you connect to your QTimer, increment the counter and set the new current image on your label. All of them shall be class members.

      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
      1
      • cerrC Offline
        cerrC Offline
        cerr
        wrote on last edited by
        #3

        I came up with something like the below, and while it does work functionally, it still makes use of a global to keep the state of the list between two function calls:

        import sys
        from PyQt4.QtGui import *
        from PyQt4.QtCore import QTimer
        
        # Create an PyQT4 application object.
        a = QApplication(sys.argv)
        
        # The QWidget widget is the base class of all user interface objects in PyQt4.
        w = QWidget()
        
        # Set window size.
        w.resize(320, 240)
        
        # Set window title
        w.setWindowTitle("Hello World!")
        
        redPM = QPixmap('red.gif')
        clrPM = QPixmap('clr.gif')
        listPM = [ redPM, clrPM ]
        lbl = QLabel('Hello World!', w)
        lbl.move(100, 80)
        lbl.setPixmap(redPM)
        lbl.resize(lbl.sizeHint())
        i = 0
        
        def toggle():
            global i
            if i == 1:
                lbl.setPixmap(listPM[0])
                i=0
            else:
                lbl.setPixmap(listPM[1])
                i=1
        
        tmr = QTimer()
        tmr.timeout.connect(toggle)
        tmr.setInterval(500)
        tmr.start()
        
        
        
        
        # Show window
        w.show()
        
        sys.exit(a.exec_())
        

        Mh now thinking about it, would it be more ressource efficient to display an animated gif as QMovie instead?

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

          That depends on how many of these you are going to have and their size.

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

          cerrC 1 Reply Last reply
          0
          • SGaistS SGaist

            That depends on how many of these you are going to have and their size.

            cerrC Offline
            cerrC Offline
            cerr
            wrote on last edited by cerr
            #5

            @SGaist I'm going to have only one for the time being. The size is 118x118.but I'm actually going to leave it as I have it above as it offers easy flexibility to adjust the frequency as required. As always, Thanks for your help!

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

              You're welcome !

              Since you have it working the way you want it, please mark the thread as solved using the "Topic Tool" button or the three doted menu beside the answer you deem correct so other forum users may know a solution has been found.

              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
              1

              • Login

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