Results 1 to 10 of 10

Thread: qwtplotgrid and qwtscalediv problem

  1. #1
    Join Date
    Apr 2013
    Posts
    24
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default qwtplotgrid and qwtscalediv problem

    Hi,

    I plan to write a function for the plot user to customize the style of grids. That is to say, the user can explicitly assign the number of the minor and major grid line. For example, a scale is 150(only considering X-axis) : major ticks is 0, 75, 150, medium ticks is 15, 30, 45, 60, 90, 105, 120, 135, minor ticks is 5, 10, 20, 25, 35, 40... 130, 140, 145 user only is needed to give the number of ticker(3 major ticks, 8 medium ticks and 20 minor tickes)

    I met two problem:

    1. for the style

    grid line of major ticks should be painted in style of Qt::black, 1, Qt::SolidLine, and the medium ticks should be Qt::gray, 1, Qt:ashLine.

    I use the qwtplotgrid member function setMajorPen()/setMinorPen() to apply the above style to the grid line, however it shows a unexpected plot grid line. Neither major lines nor minor lines are in correct postions. pls help to find the attchments.

    what i get
    mygrid.jpg

    what i want
    grid.jpg

    2. for the number(or steps, stepsize)

    I use QwtScaleEngine(QwtLinearScaleEngine/QwtLogScaleEngine), but it cannot work out the my ideal tickes as I metioned below.


    ------------------code------------------
    Qt Code:
    1. plots[i] = new QwtPlot(mainFrame);
    2.  
    3. plots[i]->enableAxis(QwtPlot::xBottom, false);
    4. plots[i]->enableAxis(QwtPlot::yRight, false);
    5. plots[i]->enableAxis(QwtPlot::xTop, true);
    6. plots[i]->enableAxis(QwtPlot::yLeft,true);
    7.  
    8. // grid
    9. grids[i] = new QwtPlotGrid();
    10. grids[i]->enableX(true);
    11. grids[i]->enableXMin(true);
    12.  
    13. // for major grid line
    14. grids[i]->setMajorPen(Qt::black, 1, Qt::SolidLine);
    15. // for minor grid line
    16. grids[i]->setMinorPen(Qt::gray, 1, Qt::DashLine);
    17.  
    18. div = lineSE->divideScale(0, 150, 2, 5, 15);
    19.  
    20. plots[i]->setAxisScaleDiv(QwtPlot::xTop, div);
    21.  
    22. grids[i]->attach(plots[i]);
    To copy to clipboard, switch view to plain text mode 


    Thank you in advance.



    Tang Tao

  2. #2
    Join Date
    Apr 2013
    Posts
    24
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qwtplotgrid and qwtscalediv problem

    In my case, scale is 150, I want:

    major ticks: (0, 75, 150) --> QPen(Qt::black, 1, Qt::SolidLine)
    medium tickes: (15, 30, 45, 60, 90, 105, 120, 135) --> QPen(Qt::gray, 1, Qt:ashLine)
    minor tickes: (5, 10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95, 100, 110, 115, 125, 130) --> no painted grid lines

    I tried:

    Qt Code:
    1. QList<double> majorTicks;
    2. majorTicks << 0 << 75 << 150;
    3.  
    4. QList<double> mediumTicks;
    5. mediumTicks << 15 << 30 << 45 << 60 << 90 << 105 << 120 << 135;
    6.  
    7. QList<double> minorTicks;
    8. minorTicks << 5 << 10 << 20 << 25 << 35 << 40 << 50 << 55 << 65
    9. << 70 << 80 << 85 << 95 << 100 << 110 << 115 << 125 << 130;
    10.  
    11. div.setTicks(QwtScaleDiv::MajorTick, majorTicks);
    12. div.setTicks(QwtScaleDiv::MediumTick, mediumTicks);
    13. div.setTicks(QwtScaleDiv::MinorTick, minorTicks);
    14.  
    15. plots[i]->setAxisScale(QwtPlot::xTop, 0, 150);
    16. //plots[i]->setAxisScaleDiv(QwtPlot::xTop, div);
    17. grids[i]->setXDiv(div);
    To copy to clipboard, switch view to plain text mode 

    still failed. Need helps!


    Tang Tao

  3. #3
    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: qwtplotgrid and qwtscalediv problem

    The idea of using setAxisScaleDiv is the right one, but now the answer depends on what Qwt version you are using. When using Qwt < 6.1 you always have to use the following constructor:

    Qt Code:
    1. QwtScaleDiv::QwtScaleDiv( const QwtInterval &, QList<double> ticks[NTickTypes] );
    To copy to clipboard, switch view to plain text mode 
    This is the only way to have an object where isValid() is true.

    In Qwt 6.1 this nasty valid flag ( better don't ask what it was good for ) has been moved to where it belongs and you can use QwtScaleDiv like everyone expects that it can be used.

    Uwe

  4. #4
    Join Date
    Apr 2013
    Posts
    24
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qwtplotgrid and qwtscalediv problem

    Hi, Uwe

    I really appreciate your reply. I am using the latest version of qwt (v6.1).
    However, I am still confused about you answer. Are you glad to explain more clear for me?

    And why I set the right div-object(in post #2) and it doesn't take effect?


    Tang Tao

  5. #5
    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: qwtplotgrid and qwtscalediv problem

    Fixing the code from your second posting:

    Qt Code:
    1. QwtScaleDiv div( 0, 150 );
    2. div.setTicks(QwtScaleDiv::MajorTick, majorTicks);
    3. div.setTicks(QwtScaleDiv::MediumTick, mediumTicks);
    4. div.setTicks(QwtScaleDiv::MinorTick, minorTicks);
    5. plots[i]->setAxisScaleDiv(QwtPlot::xTop, div);
    To copy to clipboard, switch view to plain text mode 

    When you want to have the grid lines independent from the scale ticks you have to set the QwtPlotItem::ScaleInterest flag of the grid item to false. Then the settings done for the grid won't be overwritten by the next replot.

    Uwe

  6. #6
    Join Date
    Apr 2013
    Posts
    24
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qwtplotgrid and qwtscalediv problem

    hi, Uwe

    Sorry for late reply.

    I think I want the grid to be decided by the QwtPlot::xTop scale. As your told, my plot has the right ticks at the xTop-scale now.

    But the action of grid line is not what I expect.

    grid1.jpg

    You can see that, in the picture:

    at x = 15, 45, 105, 135, the vertical grid lines is the minor grid lines(gray, dashline).

    at x = 30, 60, 90, 120, the vertical grid lines is the major grid lines(black, soildline).

    what I want is:

    at x = 15, 30, 45, 60, 90, 105, 135, the vertical grid lines is the minor grid lines(gray, dashline)
    at x = 0, 75, 150, the vertical grid lines is the major grid lines(black, soildline)


    does that mean I should overload QwtPlotGrid::draw()?


    Thank you!


    Tang Tao

  7. #7
    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: qwtplotgrid and qwtscalediv problem

    Quote Originally Posted by tangtao_xp View Post
    at x = 15, 30, 45, 60, 90, 105, 135, the vertical grid lines is the minor grid lines(gray, dashline)
    at x = 0, 75, 150, the vertical grid lines is the major grid lines(black, soildline)
    When using setAxisScaleDiv() the ticks will be exactly, where you have defined them. Of course another call of setAxisScale() or setAxisAutoScale() for the same axis will overwrite your definition.

    Quote Originally Posted by tangtao_xp View Post
    does that mean I should overload QwtPlotGrid::draw()?
    No all what needs to done is to define range and ticks in a QwtScaleDiv object and assign it to the scale. The grid will be synchronized to it in the next replot.

    Uwe

  8. #8
    Join Date
    Apr 2013
    Posts
    24
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qwtplotgrid and qwtscalediv problem

    hi, Uwe

    When using setAxisScaleDiv() the ticks will be exactly, where you have defined them. Of course another call of setAxisScale() or setAxisAutoScale() for the same axis will overwrite your definition.
    I checked my code again and I do not found another call of div defining function(so my definition won't be overwrited). I will give all code below.
    And the grid lines are never synchronized to the scale. And plot picture is the same with post#6

    my code:

    Qt Code:
    1. void MainWindow::setWidget()
    2. {
    3. for (int i = 0; i < 1; i++)
    4. {
    5. plots[i] = new QwtPlot(mainFrame);
    6.  
    7. plots[i]->plotLayout()->setAlignCanvasToScales(true);
    8.  
    9. for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    10. plots[i]->axisWidget(axis)->setMargin( 0 );
    11.  
    12. QwtPlotCanvas *canvas = new QwtPlotCanvas();
    13. canvas->setFrameStyle(QFrame::Plain | QFrame::NoFrame);
    14. plots[i]->setCanvas(canvas);
    15.  
    16. plots[i]->enableAxis(QwtPlot::xBottom, false);
    17. plots[i]->enableAxis(QwtPlot::yRight, false);
    18. plots[i]->enableAxis(QwtPlot::xTop, true);
    19. plots[i]->enableAxis(QwtPlot::yLeft,true);
    20.  
    21. grids[i] = new QwtPlotGrid();
    22. grids[i]->enableX(true);
    23. grids[i]->enableXMin(true);
    24.  
    25. grids[i]->setMajorPen(Qt::black, 1, Qt::SolidLine);
    26. grids[i]->setMinorPen(Qt::gray, 1, Qt::DashLine);
    27.  
    28. QwtScaleDiv div(0, 150);
    29.  
    30. QList<double> majorTicks;
    31. majorTicks << 0 << 75 << 150;
    32.  
    33. QList<double> mediumTicks;
    34. mediumTicks << 15 << 30 << 45 << 60 << 90 << 105 << 120 << 135;
    35.  
    36. div.setTicks(QwtScaleDiv::MajorTick, majorTicks);
    37. div.setTicks(QwtScaleDiv::MediumTick, mediumTicks);
    38.  
    39. plots[i]->setAxisScaleDiv(QwtPlot::xTop, div);
    40.  
    41. grids[i]->attach(plots[i]);
    42.  
    43. // picker
    44. pickers[i] = new QwtPlotPicker(plots[i]->canvas());
    45. pickers[i]->setTrackerMode(QwtPlotPicker::AlwaysOn);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 



    No all what needs to done is to define range and ticks in a QwtScaleDiv object and assign it to the scale. The grid will be synchronized to it in the next replot.
    Yes, I agree with you. In my code, I just want to simplify the problem for better understanding, for that english is not my mother tongue.

    As far as I know, in qwt, we can take advantage of setAxisAutoScale() and QwtScaleEnginee class to auto-calculate the tricks. (I wish discuss with you after the first problem done)


    Thank you for your patient help.



    Tang Tao

  9. #9
    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: qwtplotgrid and qwtscalediv problem

    I modified the bode example this way:

    In plot.cpp I added the following lines:

    Qt Code:
    1. enableAxis( QwtPlot::xTop );
    2. grid->setXAxis( QwtPlot::xTop ); // did you miss this one ???
    To copy to clipboard, switch view to plain text mode 
    In mainwindow.cpp I replaced the implementation of MainWindow::print():

    Qt Code:
    1. void MainWindow::print()
    2. {
    3. QList<double> majorTicks;
    4. majorTicks << 0 << 75 << 150;
    5.  
    6. QList<double> mediumTicks;
    7. mediumTicks << 15 << 30 << 45 << 60 << 90 << 105 << 120 << 135;
    8.  
    9. QList<double> minorTicks;
    10. minorTicks << 5 << 10 << 20 << 25 << 35 << 40 << 50 << 55 << 65
    11. << 70 << 80 << 85 << 95 << 100 << 110 << 115 << 125 << 130;
    12.  
    13. QwtScaleDiv div( 0, 150 );
    14. div.setTicks(QwtScaleDiv::MajorTick, majorTicks);
    15. div.setTicks(QwtScaleDiv::MediumTick, mediumTicks);
    16. div.setTicks(QwtScaleDiv::MinorTick, minorTicks);
    17. d_plot->setAxisScaleDiv(QwtPlot::xTop, div);
    18. }
    To copy to clipboard, switch view to plain text mode 
    When pressing "Print" the scale looks like expected and the grid lines are aligned to the ticks.

    Uwe

  10. The following user says thank you to Uwe for this useful post:

    tangtao_xp (15th June 2013)

  11. #10
    Join Date
    Apr 2013
    Posts
    24
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qwtplotgrid and qwtscalediv problem

    Quote Originally Posted by Uwe View Post
    I modified the bode example this way:

    In plot.cpp I added the following lines:

    Qt Code:
    1. enableAxis( QwtPlot::xTop );
    2. grid->setXAxis( QwtPlot::xTop ); // did you miss this one ???
    To copy to clipboard, switch view to plain text mode 
    In mainwindow.cpp I replaced the implementation of MainWindow:rint():
    It works! Thank you, Uwe!

    I think the reason why code does not take effect is that the grid synchronizes with the x-bottom axis by default.
    And I should change this default action of grid by setXAxis(QwtPlot::xTop) .

    Now, I move to how to auto-calculate the ticks both in linearscale and logscale. If I had a trouble with that, I will post out.

    Thank you again!



    Tang Tao

Similar Threads

  1. Replies: 3
    Last Post: 11th March 2013, 08:31
  2. Replies: 6
    Last Post: 28th March 2012, 01:11
  3. Replies: 2
    Last Post: 3rd March 2012, 13:51
  4. An error in QwtScaleDiv::invert
    By alex_sh in forum Qwt
    Replies: 3
    Last Post: 12th January 2012, 14:25
  5. painting in QwtPlotGrid
    By ready in forum Qwt
    Replies: 2
    Last Post: 18th July 2011, 07:26

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.