QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
Hello everybody,
i have a large Qt (4.8.4 / linux, opensuse) code which displays some data. I need to extend the main display window by adding axis scale. This window has a zoom (through mouse wheel), so the scale must adapt depending on the zoom. I read qwt (6.1) is appropriate for this task. I am performing some simple tests with qwt on a simplified code, but i am not able to display this scale using QwtPlotScaleItem, although I am successful at using qwt to display some curves !.
Can you tell me what I am doing wrong with the scale ?
Here is a section of the code ...
Code:
//Set scene, pixmap and get an image of the data
scene->addItem(pixmapItem);
view->setScene(scene);
setCentralWidget(view);
imageSrc = new ImageSource();
pixmapItem->setPixmap(*imageSrc->nextImage());
imagerectangle
= new QImage(27500,
250,
QImage::Format_ARGB32);
imagerectangle->fill(qRgba(0, 0, 0, 0));
// Set a Qpainter
rectangle
= new QPainter(imagerectangle
);
//Pen properties
QRectF rect
(5000,
100,
3000,
50);
lepen.setColor(Qt::red);
lepen.setWidth(10);
// TEST QWT: OK work -> a curve is displayed *****************************
xMap.setScaleInterval(0, 10000);
yMap.setScaleInterval(0, 250);
// Calculate values
double xval[10000];
double yval[10000];
for(int i=0; i<10000;i++)
{
xval[i] = i;
yval[i] = qSin(xval[i]) * qCos(2.0 * xval[i]) * 100 +101;
}
d_curves.
setPen(QColor(Qt
::yellow));
d_curves.
setRenderHint(QwtPlotItem::RenderAntialiased);
d_curves.setRawSamples(xval, yval, 10000);
const QRect &r
= contentsRect
();
xMap.setPaintInterval(r.left(), r.right());
yMap.setPaintInterval(r.top(), r.bottom());
d_curves.draw(rectangle, xMap, yMap, r);
d_curves.show();
// So far this is good ! ************************************************
// add a scale to the plot. DOES NOT WORK nothing is displayed ????????????
echelle->setBorderDistance(0);
echelle
->scaleDraw
()->setTickLength
(QwtScaleDiv::MajorTick,
100);
echelle
->scaleDraw
()->setTickLength
(QwtScaleDiv::MinorTick,
50);
rectangle
->setRenderHint
(QPainter::Antialiasing,
echelle
->testRenderHint
(QwtPlotItem::RenderAntialiased) );
echelle->setVisible(TRUE);
echelle->draw(rectangle,xMap,yMap,r);
echelle->show();
// what is wrong here ??????????????????????
// add to scene
scene
->addPixmap
(QPixmap::fromImage(*imagerectangle
));
Thank you very much for your help !
Val
Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
Well this code has not much to do with Qwt beside that it uses a plot curve to draw to a QGraphicsScene.
IMO this only makes sense, when you need to display something else together with a plot - otherwise I would remove the QGV stuff and use QwtPlot as view. But if there is really no way to get rid of the QGV code I would create a hidden QwtPlot widget creating the pixmap using QwtPlotRenderer.
Uwe
Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
Thank you very much for your feedback ...
yes, keeping QGV is required for other features of the complete code.
I try the following without success ...
Code:
//Same as before
view->setScene(scene);
setCentralWidget(view);
// Create a QWTPLOT
testhidden->setHidden(TRUE); //Hide the plot
testhidden->setTitle("QwtPlotWidget");
testhidden
->setAxisTitle
(QwtPlot::yLeft,
"Y");
testhidden
->setAxisTitle
(QwtPlot::xBottom,
"X");
// Calculate some fake curve
double xxval[10000];
double yyval[10000];
for(int i=0; i<10000;i++)
{
xxval[i] = i;
yyval[i] = qSin(xxval[i]) * qCos(2.0 * xxval[i]) * 100 +101;
}
//
// attach
m_curve->setRawSamples(xxval, yyval,250);
m_curve->attach(testhidden);
testhidden->show(); // Not sure if needed
//QT pixmap and rendering
scene->addItem(pixmapItem);
QwtPlotRenderer* plotrend = new QwtPlotRenderer;
plotrend->renderTo(testhidden,tmp);
//show the pixmap
pixmapItem->setPixmap(tmp);
// It compiles but fails to show the plot 8blank window) ...
Is it what you meant ? or did I miss something ?
Val
Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
Sure because tmp has no size - but in general yes.
Uwe
Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
I apologize for this mistake. I set up Qpixmap tmp(width,height).
The hidden QwtPlot is always right.
The QGV outcome is rather surprising. It shows a white rectangle framed by black borders. In the border I have got in gray color the axis names, the axis and plot title, but no curve, no axis labels.
I explored the color setup of the QwtPlot. If I setup QwtPlot to transparent -> QGV shows all black but for the axis name,axis and plot title. If I setup the background color of QwtPlot to red -> QGV shows a red rectangle with the black frame. No curve. So the background is displayed but not the curve.
I tries m_curve->show(); and m_curve->setVisible(TRUE); but to no avail.
Obviously, I am missing something with respect to the curve. I am carrying on to explore and to understand what is going on : this a good way to learn Qwt! I'll let you know if I find out a solution but at the same time I am open to any suggestion ...
Val
Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
You need to fill your pixmap initially with some background - otherwise you might see some random trash from the graphics memory.
Uwe
Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem
I filled the QPixmap with some background color but that did not make it.
Reading through other examples, I have tried to "replot" the QwtPlot just before to "renderTo" the QwtPlotRenderer and it worked -> the curve and the axis label are shown correctly.
So my small example code is working and I can go ahead with the real code now.
Thank you very much Uwe. I have greatly appreciated your support.
Val