Results 1 to 4 of 4

Thread: draw ticks pointing inwards while labels stay outside the plot area

  1. #1
    Join Date
    Jun 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default draw ticks pointing inwards while labels stay outside the plot area

    Hi there,

    I would like to have (or rather my boss wants it) a QwtPlot where the ticks of the scales point inwards while the labels stay outside like normal. So for the left yAxis it would be (left to right): label backbone ticks.

    If I use
    Qt Code:
    1. plot.axisScaleDraw(QwtPlot::yLeft)->setAlignment(QwtScaleDraw::RightScale);
    To copy to clipboard, switch view to plain text mode 
    the labels will end up inside the plot area (ie. backbone ticks labels).

    I have been thinking about using
    Qt Code:
    1. yLeft->attach(&plot);
    2. yLeft->setPosition(-1);
    To copy to clipboard, switch view to plain text mode 

    and having basically two scales, but I dont understand how to "attach" it to the left border of the plotarea.

    Thanks a lot
    Simon

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: draw ticks pointing inwards while labels stay outside the plot area

    I've never done what you're asking, but i guess that it would be simpler to reimplement QwtScaleDraw::drawTick() and/or QwtScaleDraw::drawLabel() to do what you want.

  3. The following user says thank you to Spitfire for this useful post:

    SimonSchmeisser (14th February 2012)

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

    Default Re: draw ticks pointing inwards while labels stay outside the plot area

    I would do my own type of plot item for the ticks. Simply copy the code from QwtPlotGrid and modify the drawLines methods to draw ticks instead of grid lines. Then disable drawing of the ticks for the scales and set the margin between the backbone and the canvas to 0 ( see QwtScaleDraw ).

    But before doing it I would explain your boss that this is against the architecture of QwtPlot ( scales and canvas are different widgets ) and ask if he really insists on hacks like this one.

    Uwe

  5. #4
    Join Date
    Jun 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw ticks pointing inwards while labels stay outside the plot area

    I implemented it by subclassing QwtScaleDraw and modifying drawLabel and drawBackbone. Works quite well. The inspiration came from qtiplot, which uses the same approach. There is some gap between the non-major ticks and the grid, but I guess thats just not avoidable due to Scale and Plot being different widgets.

    Here is the relevant code:
    Qt Code:
    1. void ScaleDraw::drawTick(QPainter *painter, double value, int len) const {
    2. if (m_tickDirection == outwards)
    3. return QwtScaleDraw::drawTick(painter, value, len);
    4.  
    5. if ( len <= 0 )
    6. return;
    7.  
    8. int pw2 = qMin((int)painter->pen().width(), len) / 2;
    9.  
    10. QwtScaleMap scaleMap = map();
    11. const QwtMetricsMap metricsMap = QwtPainter::metricsMap();
    12. QPoint pos = this->pos();
    13.  
    14. if ( !metricsMap.isIdentity() )
    15. {
    16. /*
    17.   The perfect position of the ticks is important.
    18.   To avoid rounding errors we have to use
    19.   device coordinates.
    20.   */
    21. QwtPainter::resetMetricsMap();
    22.  
    23. pos = metricsMap.layoutToDevice(pos);
    24.  
    25. if ( orientation() == Qt::Vertical )
    26. {
    27. scaleMap.setPaintInterval(
    28. metricsMap.layoutToDeviceY((int)scaleMap.p1()),
    29. metricsMap.layoutToDeviceY((int)scaleMap.p2())
    30. );
    31. len = metricsMap.layoutToDeviceX(len);
    32. }
    33. else
    34. {
    35. scaleMap.setPaintInterval(
    36. metricsMap.layoutToDeviceX((int)scaleMap.p1()),
    37. metricsMap.layoutToDeviceX((int)scaleMap.p2())
    38. );
    39. len = metricsMap.layoutToDeviceY(len);
    40. }
    41. }
    42.  
    43. const int tval = scaleMap.transform(value);
    44.  
    45. switch(alignment())
    46. {
    47. case LeftScale:
    48. {
    49. QwtPainter::drawLine(painter, pos.x() - pw2 - majTickLength(), tval,
    50. pos.x() + len - majTickLength(), tval);
    51.  
    52. break;
    53. }
    54.  
    55. case RightScale:
    56. {
    57. QwtPainter::drawLine(painter, pos.x() + pw2 + majTickLength(), tval,
    58. pos.x() - len + majTickLength(), tval);
    59. break;
    60. }
    61.  
    62. case BottomScale:
    63. {
    64. QwtPainter::drawLine(painter, tval, pos.y() + pw2 + majTickLength(),
    65. tval, pos.y() - len + majTickLength());
    66. break;
    67. }
    68.  
    69. case TopScale:
    70. {
    71. QwtPainter::drawLine(painter, tval, pos.y() - pw2 - majTickLength(),
    72. tval, pos.y() - majTickLength() + len);
    73. break;
    74. }
    75. }
    76. QwtPainter::setMetricsMap(metricsMap); // restore metrics map
    77. }
    78.  
    79. /*!
    80.   Draws the baseline of the scale
    81.   \param painter Painter
    82.  
    83.   \sa drawTick(), drawLabel()
    84. */
    85. void ScaleDraw::drawBackbone(QPainter *painter) const
    86. {
    87. if (m_tickDirection == outwards)
    88. return QwtScaleDraw::drawBackbone(painter);
    89.  
    90. const int bw2 = painter->pen().width() / 2;
    91.  
    92. const QPoint &pos = this->pos();
    93. const int len = length() - 1;
    94.  
    95. switch(alignment())
    96. {
    97. case LeftScale: {
    98. double leftShift = majTickLength();
    99. QwtPainter::drawLine(painter, pos.x() - bw2 - leftShift,
    100. pos.y(), pos.x() - bw2 - leftShift, pos.y() + len );
    101. break;
    102. }
    103. case RightScale: {
    104. double rightShift = majTickLength();
    105. QwtPainter::drawLine(painter, pos.x() + bw2 + rightShift,
    106. pos.y(), pos.x() + bw2 + rightShift, pos.y() + len);
    107. break;
    108. }
    109. case TopScale: {
    110. double upShift = majTickLength();
    111. QwtPainter::drawLine(painter, pos.x(), pos.y() - bw2 - upShift,
    112. pos.x() + len, pos.y() - bw2 - upShift);
    113. break;
    114. }
    115. case BottomScale: {
    116. double downShift = majTickLength();
    117. QwtPainter::drawLine(painter, pos.x(), pos.y() + bw2 + downShift,
    118. pos.x() + len, pos.y() + bw2 + downShift);
    119. break;
    120. }
    121. }
    122. }
    To copy to clipboard, switch view to plain text mode 

    This is still based on Qwt 5.x, I'll do the upgrade soon.

    So thanks for you help!

Similar Threads

  1. Replies: 2
    Last Post: 8th July 2011, 00:06
  2. Replies: 12
    Last Post: 18th May 2011, 20:40
  3. Replies: 1
    Last Post: 30th July 2010, 07:23
  4. Replies: 1
    Last Post: 16th March 2010, 15:23
  5. Replies: 4
    Last Post: 9th January 2009, 11:57

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.