Results 1 to 18 of 18

Thread: Axis yLeft cuts of numbers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Axis yLeft cuts of numbers

    Hi Uwe,

    OK, a little bigger example, with a custom QwtAxisScaleDraw for the yLeft axis. As you can see from the screenshot, the left side of most of the boxes is clippped.

    What next?

    Regards,
    David

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QMainWindow>
    3. #include <QPainter>
    4. #include <QPen>
    5. #include <qwt_plot.h>
    6. #include <qwt_scale_draw.h>
    7. #include <qwt_text.h>
    8. #include <qwt_painter.h>
    9. #include <qwt_math.h>
    10.  
    11. #if QT_VERSION < 0x040000
    12. #include <qwmatrix.h>
    13. #define QwtMatrix QWMatrix
    14. #else
    15. #include <qmatrix.h>
    16. #define QwtMatrix QMatrix
    17. #endif
    18.  
    19. class MyScaleDraw
    20. : public QwtScaleDraw
    21. {
    22. public:
    23. MyScaleDraw() {}
    24.  
    25. protected:
    26. virtual void drawLabel( QPainter * painter, double value ) const
    27. {
    28. QwtText lbl = tickLabel(painter->font(), value);
    29. if ( lbl.isEmpty() )
    30. return;
    31.  
    32. lbl.setBackgroundPen( QPen( Qt::black ) );
    33. QPoint pos = labelPosition(value);
    34.  
    35. QSize labelSize = lbl.textSize(painter->font());
    36. if ( labelSize.height() % 2 )
    37. labelSize.setHeight(labelSize.height() + 1);
    38.  
    39. const QwtMetricsMap metricsMap = QwtPainter::metricsMap();
    40. QwtPainter::resetMetricsMap();
    41.  
    42. labelSize = metricsMap.layoutToDevice(labelSize);
    43. pos = metricsMap.layoutToDevice(pos);
    44.  
    45. const QwtMatrix m = labelMatrix( pos, labelSize);
    46.  
    47. painter->save();
    48. #if QT_VERSION < 0x040000
    49. painter->setWorldMatrix(m, true);
    50. #else
    51. painter->setMatrix(m, true);
    52. #endif
    53.  
    54. lbl.draw (painter, QRect(QPoint(0, 0), labelSize) );
    55.  
    56. QwtPainter::setMetricsMap(metricsMap); // restore metrics map
    57.  
    58. painter->restore();
    59. }
    60.  
    61. private:
    62. };
    63.  
    64. int main(int argc, char *argv[])
    65. {
    66. QApplication a(argc, argv);
    67.  
    68. QwtPlot * pPlot = new QwtPlot;
    69. pPlot->setAxisScale( QwtPlot::yLeft, 0.0, 1.0e8, 5.0e6 );
    70. pPlot->setAxisScaleDraw( QwtPlot::yLeft, new MyScaleDraw() );
    71.  
    72. w.setCentralWidget( pPlot );
    73. w.resize( 500, 500 );
    74. w.show();
    75. return a.exec();
    76. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

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
  •  
Qt is a trademark of The Qt Company.