PDA

View Full Version : how can i mark points on top of a Image?



montamer
1st April 2010, 20:26
I am trying to mark points on top of a image,
- I select the topleft and bottomright points on the image
- generate grid points on top of the image (something like this )
http://img169.imageshack.us/img169/9664/screenshotfigure11.jpg
- the grid should be cleared later

currently i created a ImageItem class derived from QpixmapItem

#ifndef IMAGEITEM_H
#define IMAGEITEM_H

#include <QGraphicsPixmapItem>
#include <QtDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPoint>
#include <QPainter>
#include <QObject>

class ImageItem : public QObject ,public QGraphicsPixmapItem
{
Q_OBJECT
public:
ImageItem();
ImageItem(QGraphicsItem *, QGraphicsScene*);
public slots:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
signals:
void mouseClicked(QPointF);

};

#endif // IMAGEITEM_H


#include "imageitem.h"

ImageItem::ImageItem()
{
}

ImageItem::ImageItem(QGraphicsItem *parent, QGraphicsScene* scene) : QGraphicsPixmapItem(parent,scene){

}

void ImageItem::mousePressEvent(QGraphicsSceneMouseEven t *event){
emit mouseClicked(event->pos());
}


i can get the topleft and bottomright points from the mousePressEvent
Now my problem is how can i draw points (which can be removed later) on top of QPixmapItem??:confused:

any ideas how to implement this? or if u have a sample code please share :cool:

john_god
1st April 2010, 23:48
That link looks like a form or dialog with a image. What about a label to display a image and QPainter to draw the points on top ?

AlbertoN
2nd April 2010, 13:53
any ideas how to implement this? or if u have a sample code please share :cool:

This is my code to emulate what happen when a user click a mouse button on a desktop and then moves the mouse. A dashed retangle is drawn on desktop to let user see "selection area". All this is done on a screenshoot image. There are
mainly 3 methods:

mousePressEvent =>get mouse coords, start coords
mouseMoveEvent =>get end coords and draw a rectangle on image,
mouseReleaseEvent =>restore original image

I hope you can use my code to do whatever you need.
Bye
Alberto



.h:
int startx,starty,endx,endy,addable,printed;
QPixmap originalPixmap;//image where i draw
QPixmap copyPixmap;//temporary image
QPixmap savePixmap;//original image
QPen pen;


.cpp:
void Widget::mousePressEvent(QMouseEvent *event)
{
startx=event->globalPos().x()-offsetx+ui->scrollArea->horizontalScrollBar()->value();
starty=event->globalPos().y()-offsety+ui->scrollArea->verticalScrollBar()->value();
addable=1;
QObject::disconnect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(ch eckText(QString)));
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(addable==1){
if(printed==1){
originalPixmap=copyPixmap.copy(QRect());
ui->label->setPixmap(originalPixmap);
}
QPainter painter(&originalPixmap);

painter.setPen(QPen(Qt::gray, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawRect(startx,
starty,
event->globalPos().x()-startx-offsetx+ui->scrollArea->horizontalScrollBar()->value(),
event->globalPos().y()-starty-offsety+ui->scrollArea->verticalScrollBar()->value() );
painter.end();
ui->label->setPixmap(originalPixmap);
printed=1;
}
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
printed=0;
originalPixmap=copyPixmap.copy(QRect());
ui->label->setPixmap(originalPixmap);
if(addable==1){
endx=event->globalPos().x()-offsetx+ui->scrollArea->horizontalScrollBar()->value();
endy=event->globalPos().y()-offsety+ui->scrollArea->verticalScrollBar()->value();
addable=2;
connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(ch eckText(QString)));
}
}