i inherit form QGraphicsitem.but it dosen't show on my QGraphicsView,
Qt Code:
  1. #ifndef LINEITEM_H
  2. #define LINEITEM_H
  3.  
  4. #include <QGraphicsItem.>
  5. #include <QPen>
  6. #include <QPainter>
  7.  
  8. class LineItem :public QGraphicsItem
  9. {
  10.  
  11.  
  12. public:
  13. LineItem( QGraphicsItem * parent = 0);
  14. LineItem( const QLineF & line, QGraphicsItem * parent = 0 );
  15. LineItem( qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem * parent = 0 );
  16. ~LineItem();
  17. //
  18. QLineF line ( ) const;
  19. QPen getPen()const;
  20. void setLine( const QLineF & line );
  21. void setLine( qreal x1, qreal y1, qreal x2, qreal y2 );
  22. void setPen( const QPen & pen );
  23.  
  24. QRectF boundingRect() const;
  25. void QGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
  26. QPainterPath shape () const ;
  27.  
  28.  
  29. private:
  30. QPointF startPoint;
  31. QPointF centerPoint;
  32. QPointF endPoint;
  33.  
  34. QPen pen;
  35.  
  36. };
  37.  
  38. #endif // LINEITEM_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "LineItem.h"
  2.  
  3. LineItem::LineItem( QGraphicsItem * parent)
  4. : QGraphicsItem( parent)
  5. {
  6.  
  7. }
  8.  
  9. LineItem::LineItem( const QLineF & line, QGraphicsItem * parent ):QGraphicsItem(parent)
  10. {
  11. startPoint=line.p1();
  12. endPoint=line.p2();
  13. centerPoint=QPointF((startPoint.x()+endPoint.y())/2,(startPoint.y()+endPoint.y())/2);
  14. pen=QPen(Qt::black);
  15. }
  16.  
  17. LineItem::LineItem( qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem * parent /* = 0 */ ):QGraphicsItem(parent)
  18. {
  19. startPoint=QPointF(x1,y1);
  20. endPoint=QPointF(x2,y2);
  21. centerPoint=QPointF((x1+x2)/2,(y1+y2)/2);
  22. }
  23.  
  24.  
  25. LineItem::~LineItem()
  26. {
  27.  
  28. }
  29.  
  30. QRectF LineItem::boundingRect() const
  31. {
  32. return QRectF(startPoint,endPoint);
  33.  
  34. }
  35.  
  36. QPainterPath LineItem::shape() const
  37. {
  38. path.addRect(QRectF(startPoint,endPoint));
  39. return path;
  40. }
  41.  
  42. QPen LineItem::getPen() const
  43. {
  44. return pen;
  45. }
  46.  
  47. void LineItem::setPen( const QPen & pen )
  48. {
  49. this->pen=pen;
  50. }
  51.  
  52. void LineItem::setLine( const QLineF & line )
  53. {
  54. startPoint=line.p1();
  55. endPoint=line.p2();
  56. centerPoint=QPointF((startPoint.x()+endPoint.y())/2,(startPoint.y()+endPoint.y())/2);
  57. }
  58.  
  59. void LineItem::setLine( qreal x1, qreal y1, qreal x2, qreal y2 )
  60. {
  61. startPoint=QPointF(x1,y1);
  62. endPoint=QPointF(x2,y2);
  63. }
  64.  
  65. void LineItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /* = 0 */ )
  66. {
  67. /*painter->setPen(QPen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin));
  68. painter->drawRect(startPoint.x()-2,startPoint.y(),4,4);*/
  69. //painter->drawPoint(startPoint);
  70. //painter->drawPoint(endPoint);
  71. //painter->drawPoint(centerPoint);
  72.  
  73. painter->setPen(Qt::blue);
  74. painter->setFont(QFont("Arial", 30));
  75. painter->drawText(QRectF(startPoint,endPoint), Qt::AlignCenter, "Qt");
  76.  
  77. }
To copy to clipboard, switch view to plain text mode