
Originally Posted by
d_stranz
You're probably not going to have an actual paintEvent() in your thread, but instead you will have some other method that creates a new QImage with the current data. For whatever class it is that implements this updating, be sure it is derived from QObject (so it can emit signals), and implement a signal with a signature something like:
signals:
void imageUpdated
( const QImage & image
);
signals:
void imageUpdated( const QImage & image );
To copy to clipboard, switch view to plain text mode
In the GUI thread, add a slot to whatever QWidget class will display the image, and connect that slot to the thread's signal:
public slots:
void onImageUpdated
( const QImage & image
);
public slots:
void onImageUpdated( const QImage & image );
To copy to clipboard, switch view to plain text mode
In that slot, copy the image, then call
QWidget::update() to schedule a paintEvent().
Is it possible to implement similar functionality using the slot and signal mechanism if I am painting onto a widget, not a QImage? I attempted to do so, but when I run the program, no data is ever displayed on the plot.
In plot constructor:
connect(this, SIGNAL(plotUpdated()), this, SLOT(onPlotUpdated()));
d_timer->singleShot(10000, this, SLOT(onTimerFinished()));
connect(this, SIGNAL(plotUpdated()), this, SLOT(onPlotUpdated()));
d_timer = new QTimer(this);
d_timer->singleShot(10000, this, SLOT(onTimerFinished()));
To copy to clipboard, switch view to plain text mode
onTimerFinished() slot, (added single shot timer and created this because I read that signals should be emitted inside an event):
void Plot::onTimerFinished()
{
replot();
Q_EMIT plotUpdated();
}
void Plot::onTimerFinished()
{
replot();
Q_EMIT plotUpdated();
}
To copy to clipboard, switch view to plain text mode
onPlotUpdated() slot:
void Plot::onPlotUpdated()
{
if (index < 10)
{
// update d_curve's data
d_curve->updateSamples(index);
replot();
index += 1;
frame += 1;
// if index is 10, set to 0 to continue plotting
if (index == 10) index = 0;
// stop the plotting
if (frame > 1000)
{
// set index to 11 to stop plotting
index = 11;
}
}
Q_EMIT plotUpdated();
}
void Plot::onPlotUpdated()
{
if (index < 10)
{
// update d_curve's data
d_curve->updateSamples(index);
replot();
index += 1;
frame += 1;
// if index is 10, set to 0 to continue plotting
if (index == 10) index = 0;
// stop the plotting
if (frame > 1000)
{
// set index to 11 to stop plotting
index = 11;
}
}
Q_EMIT plotUpdated();
}
To copy to clipboard, switch view to plain text mode
Bookmarks