Unsolved QPixmap change color every second
-
@mrjj Ya like have the widget change colors every second.
-
@SGaist How would I change the color every second?
-
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.
-
@SGaist So make an update function?
-
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); }
-
@mrjj Hmmm... ya I am not too familiar with the QTimers slot stuff and how to make that work.
-
@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.
-
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.