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. QPixmap change color every second
Forum Updated to NodeBB v4.3 + New Features

QPixmap change color every second

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 4.9k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #7

    Hi,

    QPixmap pixmap(100, 100);
    pixmap.fill(Qt::red);
    

    There you have a 100 by 100 red pixmap.

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

    J 1 Reply Last reply
    1
    • J jrachman

      @mrjj Ok, would you be able to provide me with a coding example or maybe edit one of the rectangles in my project?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #8

      Hi
      do you mean you want to have a widget that flashes in red,green and yellow over and over?
      like a led/lamp in some device ?

      J 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        do you mean you want to have a widget that flashes in red,green and yellow over and over?
        like a led/lamp in some device ?

        J Offline
        J Offline
        jrachman
        wrote on last edited by
        #9

        @mrjj Ya like have the widget change colors every second.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          QPixmap pixmap(100, 100);
          pixmap.fill(Qt::red);
          

          There you have a 100 by 100 red pixmap.

          J Offline
          J Offline
          jrachman
          wrote on last edited by
          #10

          @SGaist How would I change the color every second?

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

            Using a QTimer and in the slot you update whatever widget you want to update. Since you're using a QPixmap, I guess you set it on a QLabel.

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

            J 2 Replies Last reply
            1
            • SGaistS SGaist

              Using a QTimer and in the slot you update whatever widget you want to update. Since you're using a QPixmap, I guess you set it on a QLabel.

              J Offline
              J Offline
              jrachman
              wrote on last edited by
              #12

              @SGaist So make an update function?

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

                That method already exists for the widgets. Again, if you are using a QLabel to show the pixmap, then you can just set the new pixmap on the label.

                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
                • SGaistS SGaist

                  Using a QTimer and in the slot you update whatever widget you want to update. Since you're using a QPixmap, I guess you set it on a QLabel.

                  J Offline
                  J Offline
                  jrachman
                  wrote on last edited by jrachman
                  #14

                  @SGaist Ok and what if I want the colors to change after a specific condition. For example, for int x, x<100, and x++, change the color to green if x%2==0 (else change color to red) then wait 5 seconds. @mrjj

                  mrjjM J.HilkJ 2 Replies Last reply
                  0
                  • J jrachman

                    @SGaist Ok and what if I want the colors to change after a specific condition. For example, for int x, x<100, and x++, change the color to green if x%2==0 (else change color to red) then wait 5 seconds. @mrjj

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    @jrachman
                    Hi
                    You can do all that in QTimers slot.
                    Say it calls your function every second.
                    You can then do something else after x seconds or keep track of
                    how many seconds passed since last time.

                    Do you need more than one of these "leds" in such case it would be easier to handle with a custom widget
                    as the housekeeping info would else have to live in mainwindow and could be messy for
                    more than a few flashing leds.

                    J 1 Reply Last reply
                    1
                    • J jrachman

                      @SGaist Ok and what if I want the colors to change after a specific condition. For example, for int x, x<100, and x++, change the color to green if x%2==0 (else change color to red) then wait 5 seconds. @mrjj

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

                      Hi @jrachman

                      if I may suggest a different approach.

                      For me it seems, you should subclass QWidget, and overwrite the QWidget::paintEvent function.

                      Than you can use QPainter and draw directly on the Widget

                      something like:

                      void MyWidget::paintEvent(QPaintEvent *event)
                      {
                          QWdiget::paintEvent(event);
                          QPainter painter(this);
                          painter.setRenderHint(QPainter::Antialiasing);
                          QRect rect = QRect(290, 20, 70, 40);
                          painter.fillRect(rect, Qt::red);
                      }
                      

                      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.

                      J 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @jrachman
                        Hi
                        You can do all that in QTimers slot.
                        Say it calls your function every second.
                        You can then do something else after x seconds or keep track of
                        how many seconds passed since last time.

                        Do you need more than one of these "leds" in such case it would be easier to handle with a custom widget
                        as the housekeeping info would else have to live in mainwindow and could be messy for
                        more than a few flashing leds.

                        J Offline
                        J Offline
                        jrachman
                        wrote on last edited by
                        #17

                        @mrjj Hmmm... ya I am not too familiar with the QTimers slot stuff and how to make that work.

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          Hi @jrachman

                          if I may suggest a different approach.

                          For me it seems, you should subclass QWidget, and overwrite the QWidget::paintEvent function.

                          Than you can use QPainter and draw directly on the Widget

                          something like:

                          void MyWidget::paintEvent(QPaintEvent *event)
                          {
                              QWdiget::paintEvent(event);
                              QPainter painter(this);
                              painter.setRenderHint(QPainter::Antialiasing);
                              QRect rect = QRect(290, 20, 70, 40);
                              painter.fillRect(rect, Qt::red);
                          }
                          
                          J Offline
                          J Offline
                          jrachman
                          wrote on last edited by
                          #18

                          @J.Hilk Ok I see. So would you be able to show me how you would implement this in with this example?: for int x, x<100, and x++, change the color to green if x%2==0 (else change color to red) then wait 5 seconds.

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

                            You should then start by familiarising yourself with the Signals & Slots which is one of Qt's core feature.

                            As for your use case, you could also take advantage of QObject::startTimer which would avoid using signals and slots.

                            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