QPixmap change color every second
-
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.
-
@mrjj Ok, would you be able to provide me with a coding example or maybe edit one of the rectangles in my project?
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 ? -
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 ? -
Hi,
QPixmap pixmap(100, 100); pixmap.fill(Qt::red);
There you have a 100 by 100 red pixmap.
-
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.
-
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.
-
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.
-
@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); }
-
@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.