Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView updates not correct
Qt 6.11 is out! See what's new in the release blog

QGraphicsView updates not correct

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnwang
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Please enclose your code with coding tags (one @ at the beginning and one at the end) Otherwise the code is very difficult to read and will get less attention

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved