QPixmap change color every second
-
In my application, I would like to take my QPixmap and change its color from green to yellow to red then back to green and continuous until the application is closed. Here is a Dropbox link to the code of the project: https://www.dropbox.com/s/s6mig7rfc2jiw6k/dummy.zip?dl=0. It contains 3 QPixmaps that need to be changing colors. Do not worry about the other text and stuff; all I want is the colors to be changing in real time. If there is a better method of real time color changing than QPixmaps, feel free to show me that as well.
Thank you.
-
Hi
As far as i know, QImage is faster for image manipulation than QPixmap.
And you could offload the manipulation to a thread and only display in main.But looking at the code, it seems you just want to draw a color rect on the QPixmap and not really change the color of the pixels ? or ?
-
Hi mrjj,
Well I am not too familiar with this framework to answer you question but you seem right. I would like those 3 rectangles to change color whether it is drawing over the rectangle or changing the color of the pixels. Either way, they seem like they achieve my goal just in different ways.
-
Hi,
QPixmap pixmap(100, 100); pixmap.fill(Qt::red);
There you have a 100 by 100 red pixmap.
-
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.
-
@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. -
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); }
-
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.