Dawid Sip wrote:
Hi,
I'm working on an option in my graphics editing application for drawing Polygon around an object in an image. It should work comparably to Path in Gimp except for that I'd like the line between the last added vertex and the one to be placed to be animated (stretched between the pouse pointer and the last added vertex. I cant really find any example in which i could look up howto do this so I'm hoping someone could point me in the right direction. I forgot to mention.

Hi Dawid,

I assume you want to do this on a QGraphicsView, since this is what you were using in the previous post. I've attached a small example that shows how this can be done.

Basically you maintain two shapes. The first is what you have built so far, in my case a plain QPainterPath. The second is a line. The last point on the QPainterPath corresponds to the first point in the line and when you move the mouse you update the last point in the line, thus animating it along the cursor.

If you want to do this on a plain widget it will be almost identical, except you need to implement a paintEvent and draw the actual path and line.

I'm using an small rectangle made in .svg for vertices. This is because i need the polygon to be scalable when i zoom in or out..

QPainter and QGraphicsView are vectorial and will zoom for you. Unless you want an especially fancy looking rectangle, just setting a zoom factor on the graphics view or painter and drawing the rect will do.

best regards,
Gunnar
package com.trolltech.tests;

import com.trolltech.qt.gui.*;
import com.trolltech.qt.core.*;


public class GraphicsShapeThingy extends QGraphicsView {

    private QPainterPath path;
    private QGraphicsPathItem pathItem;
    private QGraphicsLineItem lineItem;

    public GraphicsShapeThingy() {

        QGraphicsScene scene = new QGraphicsScene();
        setScene(scene);

        // some initial setup...
        path = new QPainterPath();
        path.moveTo(100, 100);
        path.lineTo(200, 100);
        path.lineTo(300, 150);

        pathItem = scene.addPath(path);
        lineItem = scene.addLine(300, 150, 0, 0, new QPen(QColor.black, 0, Qt.PenStyle.DotLine));
    }



    protected void mouseMoveEvent(QMouseEvent e) {
        // Update the end point of the line
        QPointF pos = mapToScene(e.pos());
        QLineF line = lineItem.line();
        lineItem.setLine(line.x1(), line.y1(), pos.x(), pos.y());
    }

    protected void mouseReleaseEvent(QMouseEvent e) {
        // push the end point of the line to the path.
        QLineF line = lineItem.line();
        path.lineTo(line.x2(), line.y2());
        pathItem.setPath(path);

        // make the end point of the path the start of the line
        lineItem.setLine(line.x2(), line.y2(), line.x2(), line.y2());
    }

    public static void main(String args[]) {
        QApplication.initialize(args);

        QGraphicsView view = new GraphicsShapeThingy();

        view.setRenderHints(QPainter.RenderHint.Antialiasing);
        view.resize(1024, 768);
        view.show();

        QApplication.exec();
    }
}
_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to