Question

I want to create a event when a button has been pressed program should allow to draw free hand lines using mouse pointer in a picture. Currently I am in the stage where I can show album of pictures in a tab window. Can anybody help with that by providing some guideline or clues???

Was it helpful?

Solution

Here is an example of how to paint with mouse movements:

http://qt-project.org/doc/qt-4.8/widgets-scribble.html

OTHER TIPS

Your question is quite broad, I'm afraid. I'd advise doing some research into what you want to achieve, maybe looking at how other people have solved the same problem. You'll get the most out of SO by using it more as a resource to answer specific questions.

That said, here are a couple of things you could look at:

QGraphicsScene: A surface which can contain large numbers of 2D graphics objects (i.e. the lines which you'll be allowing the user to draw)

QGraphicsSceneMouseEvent: The event you need to catch and handle in order to get input from the mouse to your scene. You can handle this event either by creating a subclass of QGraphicsScene and reimplenting QGraphicsScene::mouseMoveEvent, or by installing an eventFilter on your scene.

If you are displaying the picture using a simple QWidget (I mean, not something complicated like QGraphicsScene), just reimplement the QWidget::mouseMoveEvent(QMouseEvent *e). When the user has pressed a mouse button (e->buttons() != 0) you could do a painting within the picture.

Let's assume you have your picture in a member QImage img and track the previous cursor position in QPoint cursorLast. Let's also assume that you display the picture using something like a QLabel, where you can just set the image in a property. Then you could do the painting like this:

void MyWidget::mouseMoveEvent(QMouseEvent *e) {
    if(e->buttons()) {
        if(!cursorLast.isNull()) {
            QPainter p(&img); // and set pen
            p.drawLine(cursorLast, e->pos());
            p.end();
            setImage(img); // update image in your view
        }
        cursorLast = e->pos();
    }
}

Don't forget to reset the cursorLast member when the mouse gets released:

void MyWidget::mouseReleaseEvent(QMouseEvent *e) {
    cursorLast = QPoint(); // reset
}

Note that this code isn't tested and I may have used slightly wrong names in the methods, but it will be easy to find the real ones.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top