hello,
I subclass a QGraphicsItem in order to draw a line more faster than QGraphicsPath Item.
My new item worked fine. But if I add a QGraphicsItem into scene before adding my customize Item,
QGraphicsView only update part of my Item. It looks very strange. Can anyone tell me the reason?
Here is my customized item.
/***********************************************************************************/
class GraphicsFreeLine : public QGraphicsItem
{
public:
GraphicsFreeLine();
QRectF boundingRect() const;
QPainterPath shape() const;
QPainterPath* CurrentPath() { return m_pPath;}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
private:
QPainterPath* m_pPath;
QRectF m_Rect;
};
GraphicsFreeLine::GraphicsFreeLine()
{
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptsHoverEvents(true);
m_pPath = new QPainterPath();
setCacheMode(QGraphicsItem::NoCache);
}
QRectF GraphicsFreeLine::boundingRect() const
{
QRectF rect = m_pPath->boundingRect();
rect.adjust(-LineWidth,-LineWidth,2* LineWidth,2*LineWidth);
return rect;
}
QPainterPath GraphicsFreeLine::shape() const
{
return *m_pPath;
}
void GraphicsFreeLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
painter->setClipRect(option->exposedRect);
QPen pen(Qt::red,LineWidth,Qt::SolidLine,Qt::RoundCap,Qt::BevelJoin);
painter->strokePath(*m_pPath,pen);
}
Also, I subclassed QGraphicsView to treat mouse event.
So I can draw a bezier path.
/***********************************************************************************/
class GraphicsViewEx : public QGraphicsView
{
Q_OBJECT
public:
GraphicsViewEx();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
QPointF m_curPt,m_prevPt;
GraphicsFreeLine* pFLine;
};
QPointF midPoint(QPointF p1,QPointF p2)
{
QPointF p = QPointF((p2.x()-p1.x())/2.0,(p2.y()-p1.y())/2.0);
return QPointF(p1.x() + p.x(),p1.y()+ p.y());
}
GraphicsViewEx::GraphicsViewEx() : QGraphicsView()
{
}
void GraphicsViewEx::mousePressEvent(QMouseEvent *event)
{
QPointF p = this->mapToScene(event->pos());
m_curPt = m_prevPt = p;
pFLine = new GraphicsFreeLine();
this->scene()->addItem(pFLine);
}
void GraphicsViewEx::mouseMoveEvent(QMouseEvent *event)
{
if (event->modifiers() & Qt::ShiftModifier) {
return;
}else{
if(event->buttons() == Qt::LeftButton){
QPointF p = this->mapToScene(event->pos());
QPointF cp = midPoint(m_curPt,p);
// create new path
QPainterPath newPath(m_prevPt);
newPath.quadTo(m_curPt,cp);
pFLine->CurrentPath()->addPath(newPath);
QRectF rect = newPath.boundingRect();
rect.adjust(pFLine>LineWidth,pFLine>LineWidth,2*pFLine->LineWidth,2*pFLine->LineWidth);
// only update part of the path to make draw faster
pFLine->update(rect);
m_prevPt = cp;
m_curPt = p;
}
}
QGraphicsView::mouseMoveEvent(event);
}
void GraphicsViewEx::mouseReleaseEvent(QMouseEvent *event)
{
}
Other code:
/***********************************************************************************/
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene(0,0,1024,768);
GraphicsViewEx view;
view.setScene(&scene);
view.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
view.show();
#if 1 // if I add these code , line can not be updated correctly !!
QGraphicsPathItem* pItem = new QGraphicsPathItem();
QPainterPath p;
p.moveTo(900,100);
p.quadTo(QPointF(200,200),QPointF(200,300));
pItem->setPath(p);
scene.addItem(pItem);
pItem->setZValue(128);
#endif
return app.exec();
}
These code works fine for me . Line can be drawn smoothly.
The problem is ,When I add some item in main, My new Lines can not be drawn correctly.
QGraphicsView’s update works very strange . Please help.
Thanks.
Johnwang
↧