问题
I have a Qwt plot defined in a class method:
plot = new QwtPlot();
const int margin = 5;
plot->setContentsMargins( margin, margin, margin, 0 );
plot->setTitle( "Support polygon" );
plot->setCanvasBackground( Qt::white );
plot->setAxisScale( QwtPlot::yLeft, -0.8,0.8 );
plot->setAxisScale( QwtPlot::xBottom, -0.8,0.8 );
QBoxLayout *layout = new QBoxLayout(QBoxLayout::LeftToRight);
layout->addWidget(plot);
setLayout(layout);
curve_ = new QwtPlotCurve();
curve_->attach( plot );
xData = new double[4];
yData = new double[4];
QTimer *replotTimer_ = new QTimer(this);
connect(replotTimer_, SIGNAL(timeout()), this, SLOT(updateMe_()));
replotTimer_->start(100);
the data is updated when the timer calls the callback function updateMe_() and the plot should be updated
void Support_polygon::updateMe_()
{
curve_->setRawSamples(xData,yData,4);
plot->replot();
}
xData and yData are also being modified in a thread, whose callback function is:
void Support_polygon::callback_()
{
msg_mutex.lock();
for (size_t ii=0; ii<msg.contacts.size(); ii++)
{
xData[ii] = 1.4f*float(std::rand())/float(RAND_MAX)-0.7;
yData[ii] = 1.4f*float(std::rand())/float(RAND_MAX)-0.7;
}
msg_mutex.unlock();
}
(right now I'm only putting random numbers, but when this works the data will be passed by a ROS message, that's why is in a different thread)
The problem is that the plot is never updated. As if replot() is never called. I tested and all callback functions are being called.
Surprisingly, the plot gets updated if I resize the window... if I keep re-sizing, the plot gets updated while I do it.
The replot call is being done on the main thread by the timer timeout signal. I don't understand what is going on.
回答1:
Even though in the documentation it says that setAutoReplot is not recommended and that performance wise is better to use replot, I tested setting setAutoReplot to true
plot->setAutoReplot( true );
removed the replot call in updateMe_(), and everything works.
But to me this seems like a bug.
Note: I'm using Qwt 6.1.2.
note: after many more issues with qwt I switched to qtcustomplot. Only one header and one cpp file. My two cents in case anyone is looking for a substitute.
来源:https://stackoverflow.com/questions/34948704/qwt-plot-is-not-reploting