Drawing line in QGraphicsScene with Qt

I have a problem with drawing lines. It works well when the mouse moves slowly, but when the mouse moves faster there are some gaps and I don't know why. This is the code:

if(QEvent::MouseButtonPress&&event->buttons()==Qt::LeftButton){
QPointF pt=mapToScene(event->pos());
        band->setGeometry(0,0,0,0);
         band->hide();
        band->update();
         this->scene()->addLine(pt.x(),pt.y(),pt.x(),pt.y(),QPen(color, size));
    qDebug()<<event->pos();
}

      

Here's a screenshot:

enter image description here

On the left, it descends more slowly, faster.

+3


source to share


1 answer


So this is a really interesting question. I am doing the same on my computer and getting the same problem. I am not reading your code deeply because it seems like you are subclassing QGraphicsView

, but I am subclassing QGraphicsScene

, but it doesn't matter. I am telling you the basic idea. I can offer you the following:

Draw it as it is, but when drawing the custom end, you will remove that and draw 1 very nice curve without those gaps. You must use mouseReleaseEvent

:

In mouseMoveEvent

:

    QPoint pos = mouseEvent->scenePos().toPoint();//just get point
    pol.append(pos);//append to polygon
//...draw lines or what you want

      

In the constructor:

QPolygon pol;

      

In mouseReleaseEvent

you create QPainterPath

, load a polygon onto it and draw a normal line with no spaces.



void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QPainterPath myPath;
    myPath.addPolygon(pol);
    addPath(myPath,QPen(Qt::red,2));
    pol.clear();
}

      

Result:

I was moving very fast and getting spaces (now my mouse button is pressed)

enter image description here

now i have released my button and i get a normal curve

enter image description here

+3


source







All Articles