Results 1 to 7 of 7

Thread: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem

  1. #1
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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 ...

    Qt Code:
    1. //Set scene, pixmap and get an image of the data
    2. scene = new QGraphicsScene(this);
    3. pixmapItem = new QGraphicsPixmapItem();
    4. scene->addItem(pixmapItem);
    5.  
    6. view = new QGraphicsView();
    7. view->setScene(scene);
    8.  
    9. setCentralWidget(view);
    10.  
    11. imageSrc = new ImageSource();
    12. pixmapItem->setPixmap(*imageSrc->nextImage());
    13.  
    14. QImage* imagerectangle;
    15. imagerectangle = new QImage(27500,250,QImage::Format_ARGB32);
    16. imagerectangle->fill(qRgba(0, 0, 0, 0));
    17.  
    18. // Set a Qpainter
    19. QPainter* rectangle;
    20. rectangle = new QPainter(imagerectangle);
    21.  
    22. //Pen properties
    23. QPen lepen;
    24. QRectF rect(5000,100,3000,50);
    25. lepen.setColor(Qt::red);
    26. lepen.setWidth(10);
    27.  
    28.  
    29. // TEST QWT: OK work -> a curve is displayed *****************************
    30. QwtPlotCurve d_curves;
    31.  
    32. xMap.setScaleInterval(0, 10000);
    33. yMap.setScaleInterval(0, 250);
    34.  
    35. // Calculate values
    36. double xval[10000];
    37. double yval[10000];
    38. for(int i=0; i<10000;i++)
    39. {
    40. xval[i] = i;
    41. yval[i] = qSin(xval[i]) * qCos(2.0 * xval[i]) * 100 +101;
    42. }
    43.  
    44. d_curves.setPen(QColor(Qt::yellow));
    45. d_curves.setStyle(QwtPlotCurve::Lines);
    46. d_curves.setRenderHint(QwtPlotItem::RenderAntialiased);
    47. d_curves.setCurveAttribute(QwtPlotCurve::Fitted);
    48.  
    49. d_curves.setRawSamples(xval, yval, 10000);
    50.  
    51. const QRect &r = contentsRect();
    52.  
    53. xMap.setPaintInterval(r.left(), r.right());
    54. yMap.setPaintInterval(r.top(), r.bottom());
    55.  
    56. d_curves.draw(rectangle, xMap, yMap, r);
    57. d_curves.show();
    58. // So far this is good ! ************************************************
    59.  
    60.  
    61. // add a scale to the plot. DOES NOT WORK nothing is displayed ????????????
    62. QwtPlotScaleItem* echelle;
    63. echelle = new QwtPlotScaleItem();
    64. echelle->setAlignment(QwtScaleDraw::TopScale);
    65. echelle->setBorderDistance(0);
    66. echelle->scaleDraw()->setTickLength(QwtScaleDiv::MajorTick,100);
    67. echelle->scaleDraw()->setTickLength(QwtScaleDiv::MinorTick,50);
    68. echelle->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels,true);
    69. rectangle->setRenderHint(QPainter::Antialiasing,
    70. echelle->testRenderHint(QwtPlotItem::RenderAntialiased) );
    71. echelle->setVisible(TRUE);
    72. echelle->draw(rectangle,xMap,yMap,r);
    73. echelle->show();
    74. // what is wrong here ??????????????????????
    75.  
    76.  
    77. // add to scene
    78. scene->addPixmap(QPixmap::fromImage(*imagerectangle));
    To copy to clipboard, switch view to plain text mode 

    Thank you very much for your help !

    Val

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

  3. #3
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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 ...

    Qt Code:
    1. //Same as before
    2. scene = new QGraphicsScene(this);
    3. view = new QGraphicsView();
    4. view->setScene(scene);
    5. setCentralWidget(view);
    6.  
    7.  
    8. // Create a QWTPLOT
    9. QwtPlotCurve* m_curve= new QwtPlotCurve;
    10.  
    11. QwtPlot* testhidden = new QwtPlot;
    12. testhidden->setHidden(TRUE); //Hide the plot
    13. testhidden->setTitle("QwtPlotWidget");
    14. testhidden->setAxisTitle(QwtPlot::yLeft,"Y");
    15. testhidden->setAxisTitle(QwtPlot::xBottom,"X");
    16.  
    17. // Calculate some fake curve
    18. double xxval[10000];
    19. double yyval[10000];
    20. for(int i=0; i<10000;i++)
    21. {
    22. xxval[i] = i;
    23. yyval[i] = qSin(xxval[i]) * qCos(2.0 * xxval[i]) * 100 +101;
    24. }
    25.  
    26. //
    27. // attach
    28. m_curve->setRawSamples(xxval, yyval,250);
    29. m_curve->attach(testhidden);
    30.  
    31. testhidden->show(); // Not sure if needed
    32.  
    33.  
    34. //QT pixmap and rendering
    35. pixmapItem = new QGraphicsPixmapItem();
    36. scene->addItem(pixmapItem);
    37.  
    38. QPixmap tmp;
    39. QwtPlotRenderer* plotrend = new QwtPlotRenderer;
    40. plotrend->renderTo(testhidden,tmp);
    41.  
    42. //show the pixmap
    43. pixmapItem->setPixmap(tmp);
    44. // It compiles but fails to show the plot 8blank window) ...
    To copy to clipboard, switch view to plain text mode 


    Is it what you meant ? or did I miss something ?

    Val

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT/QWT: add axis scale to Qpainter using QwtPlotScaleItem

    Sure because tmp has no size - but in general yes.

    Uwe

  5. #5
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

  7. #7
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile 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

Similar Threads

  1. Replies: 1
    Last Post: 9th September 2013, 08:50
  2. Replies: 2
    Last Post: 26th December 2012, 02:03
  3. qwt axis scale
    By Markus_AC in forum Qwt
    Replies: 0
    Last Post: 15th December 2011, 10:45
  4. Replies: 4
    Last Post: 16th January 2011, 11:32
  5. Axis with a probability scale
    By Lister in forum Qwt
    Replies: 1
    Last Post: 5th May 2009, 07:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.