Results 1 to 4 of 4

Thread: Problem inherit from QGraphicItem

  1. #1
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem inherit from QGraphicItem

    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 

  2. #2
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem inherit from QGraphicItem

    use QGraphicsScene addItem method, this method will add your item to scene and make it visible on your QGraphicsView.

    Qt Code:
    1. // set the view rect
    2.  
    3.  
    4. // Set the scene Rect
    5. // scene->setSceneRect(0, 0, 400, 400);
    6.  
    7. view->setScene(scene);
    8.  
    9. LineItem *L1 = new LineItem();
    10. L1->setLine(0, 0, 100, 100);
    11.  
    12. scene->addItem(L1);
    To copy to clipboard, switch view to plain text mode 

    If this will not solve your problem then paste your code where you add LineItem to scene.

  3. #3
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem inherit from QGraphicItem

    i have added it to view,but it did't show on the view
    Qt Code:
    1. #ifndef FILLVIEW_H
    2. #define FILLVIEW_H
    3.  
    4. #include <QGraphicsView>
    5. #include <QGraphicsEllipseItem>
    6. #include <QGraphicsLineItem>
    7. #include "GraphicsItem/LineItem.h"
    8. enum ShapeName{
    9. Line, //线(并非利用贝塞尔曲线绘制而成)
    10. Rectangle, //矩形
    11. Circle, //æ ‡å‡†åœ†
    12. Triangle, //三角形
    13. Ellipse, //æ¤*圆
    14. BezierCurve, //贝塞尔曲线
    15. Control //控制
    16. };
    17.  
    18.  
    19. class FillView : public QGraphicsView
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. FillView(QWidget *parent);
    25. ~FillView();
    26.  
    27.  
    28.  
    29. QPainter* ItemPainter;
    30.  
    31. void mouseMoveEvent ( QMouseEvent * event ) ;
    32. void mouseReleaseEvent ( QMouseEvent * event );
    33. void mousePressEvent ( QMouseEvent * event ) ;
    34. void wheelEvent ( QWheelEvent * event ) ;
    35.  
    36.  
    37. QString getDrawingType()
    38. {
    39. return drawingType;
    40. };
    41. void ShowBackGroundGrid(bool show)
    42. {
    43. BackGroundGridVisible=show ;
    44. }
    45.  
    46. bool GetBackGroundVisble()
    47. {
    48. return BackGroundGridVisible;
    49. }
    50.  
    51.  
    52. public slots:
    53. void setCurrentShape(ShapeName s);
    54. signals:
    55. void getXY(float x,float y);
    56. private:
    57. void scaledBy(double factor);
    58. QString drawingType;
    59. QPointF beginPoint;
    60. QPointF endPoint;
    61. bool BackGroundGridVisible;
    62. LineItem *ll;
    63. ShapeName currentShapeName;
    64.  
    65.  
    66. public:
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74. };
    75.  
    76.  
    77.  
    78.  
    79. #endif // FILLVIEW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "fillview.h"
    2. #include <QDebug>
    3. #include <QTime>
    4. #include <QMouseEvent>
    5. #include <QGraphicsEllipseItem>
    6.  
    7. FillView::FillView(QWidget *parent)
    8. : QGraphicsView(parent)
    9. {
    10. this->setDragMode(QGraphicsView::DragMode::RubberBandDrag);
    11. setMouseTracking(true);
    12.  
    13. setRenderHints(QPainter::Antialiasing
    14. | QPainter::TextAntialiasing);
    15. setContextMenuPolicy(Qt::ActionsContextMenu);
    16. line=new QGraphicsLineItem(beginPoint.x(),beginPoint.y(),beginPoint.x(),beginPoint.y() );
    17.  
    18.  
    19. // scene()->addItem(&item);
    20. }
    21.  
    22.  
    23. FillView::~FillView()
    24. {
    25.  
    26. }
    27.  
    28. void FillView::setCurrentShape(ShapeName s)
    29. {
    30. currentShapeName=s;
    31. if (s!=ShapeName::Control)
    32. {
    33. this->setCursor(Qt::CrossCursor);
    34. }else
    35. {
    36. this->setCursor(Qt::CursorShape::ArrowCursor);
    37. }
    38.  
    39.  
    40. }
    41.  
    42. void FillView::mousePressEvent( QMouseEvent * event )
    43. {
    44. beginPoint=mapToScene(event->pos());
    45. // BoundaryPoint(beginPoint.toPoint());
    46. switch(currentShapeName)
    47. {
    48.  
    49. case ShapeName::Line:
    50. {
    51. ll=new LineItem(beginPoint.x(),beginPoint.y(),beginPoint.x(),beginPoint.y() );
    52. ll->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    53. scene()->addItem(line);
    54. break;
    55. }
    56. case ShapeName::Circle:
    57. {
    58. circle=new QGraphicsEllipseItem(beginPoint.x(),beginPoint.y(),0,0);
    59. circle->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    60.  
    61. scene()->addItem(circle);
    62. break;
    63. }
    64. }
    65.  
    66.  
    67. }
    68. void FillView::mouseReleaseEvent(QMouseEvent * event)
    69. {
    70.  
    71. endPoint=mapToScene( event->pos());
    72. // BoundaryPoint(endPoint.toPoint());
    73. switch(currentShapeName)
    74. {
    75.  
    76. case ShapeName::Line:
    77. {
    78. ll->setLine(beginPoint.x(),beginPoint.y(),endPoint.x(),endPoint.y());
    79. break;
    80. }
    81. case ShapeName::Circle:
    82. {
    83. double x = this->endPoint.x() - this->beginPoint.x();
    84. double y = this->endPoint.y() - this->beginPoint.y();
    85.  
    86. double radius = sqrt(x * x + y * y);
    87. circle->setRect(this->beginPoint.x() -radius,
    88. this->beginPoint.y() - radius,radius*2,radius*2);
    89. break;
    90. }
    91. }
    92.  
    93.  
    94.  
    95.  
    96. }
    97. void FillView::mouseMoveEvent ( QMouseEvent * mevent )
    98. {
    99.  
    100. if (mevent->buttons()==Qt::LeftButton )
    101. {
    102. endPoint=mapToScene(mevent->pos());
    103. // BoundaryPoint(endPoint.toPoint());
    104.  
    105. switch(currentShapeName)
    106. {
    107.  
    108. case ShapeName::Line:
    109. {
    110. ll->setLine(beginPoint.x(),beginPoint.y(),endPoint.x(),endPoint.y());
    111. break;
    112. }
    113. case ShapeName::Circle:
    114. {
    115. double x = this->endPoint.x() - this->beginPoint.x();
    116. double y = this->endPoint.y() - this->beginPoint.y();
    117.  
    118. double radius = sqrt(x * x + y * y);
    119. circle->setRect(this->beginPoint.x() -radius,
    120. this->beginPoint.y() - radius,radius*2,radius*2);
    121. break;
    122. }
    123. }
    124. }
    125.  
    126.  
    127. //qDebug() <<"X:"<<mevent->pos().x()<<"Y:"<<mevent->pos().y()<<QTime::currentTime().toString() ;
    128. QPointF pos=mapToScene(QPoint(mevent->pos().x(),mevent->pos().y()));
    129. if (this->scene()->sceneRect().contains(pos))
    130. {
    131. emit getXY(pos.x(),pos.y());
    132. // setCursor(Qt::CursorShape::ArrowCursor);
    133. }
    134. else
    135. {
    136. QPointF posn=pos;
    137. if (posn.x()>sceneRect().width())
    138. {
    139. posn.setX(sceneRect().width());
    140. }
    141. if (posn.y()>sceneRect().height())
    142. {
    143. posn.setY(sceneRect().height());
    144. }
    145.  
    146. if (posn.x()<0)
    147. {
    148. posn.setX(0);
    149. }
    150. if (posn.y()<0)
    151. {
    152. posn.setY(0);
    153. }
    154. emit getXY(posn.x(),posn.y());
    155. //setCursor(Qt::CursorShape::ForbiddenCursor);
    156. }
    157.  
    158.  
    159. if (mevent->buttons()==Qt::LeftButton)
    160. {
    161. endPoint=mapToScene(mevent->pos());
    162. QRectF rect(beginPoint,endPoint);
    163.  
    164. }
    165.  
    166. }
    167.  
    168. void FillView::wheelEvent( QWheelEvent * e )
    169. {
    170. if (e->modifiers().testFlag(Qt::ControlModifier)){ // zoom only when CTRL key pressed
    171.  
    172. int numSteps = e->delta() / 15 / 8;
    173.  
    174. if (numSteps == 0) {
    175. e->ignore();
    176. return;
    177. }
    178. qreal sc = pow(1.25, numSteps); // I use scale factor 1.25
    179. this->scale(sc,sc);
    180.  
    181.  
    182. e->accept();
    183. }
    184. }
    185.  
    186. void FillView::scaledBy(double factor)
    187. {
    188.  
    189. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 33 minutes:


    thank you ,i goti it
    Last edited by xiongxiongchuan; 13th August 2013 at 06:00.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem inherit from QGraphicItem

    ur scene() ins still empty ..
    use like this

    Qt Code:
    1. QRectF sceneRect( preferred Geometry)
    2.  
    3. QGraphicsScene *scene = new QGraphicsScene(sceneRect);
    4.  
    5. addScene(scene);
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. QGraphicItem is not recognized
    By stella1016 in forum Qt Programming
    Replies: 3
    Last Post: 1st July 2011, 16:33
  2. QGraphicScene and different topmost QGraphicItem
    By Xaar in forum Qt Programming
    Replies: 3
    Last Post: 5th December 2007, 16:58
  3. identify QGraphicItem
    By avis_phoenix in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2007, 06:16
  4. Location of QGraphicItem!!
    By rachana in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2007, 21:20
  5. need help with QGraphicItem
    By rachana in forum Qt Programming
    Replies: 13
    Last Post: 30th January 2007, 06:27

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.