sergio87
31st August 2011, 09:53
Hi!
I'm trying to make a program that draws a ellipse over an image when you click. I¡ll explain:
I open an image with QGraphicsScene (subclassed). In the subclass I override the mouseReleaseEvent to collect the mouse clicks. I want to draw an ellipse when click the mouse in these coordinates. The coordinates are from the image and not from the window.
This is the code, based on a example of QT:
SubQGraphicsScene.cpp
SubQGraphicsScene::SubQGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
// TODO Auto-generated constructor stub
}
SubQGraphicsScene::~SubQGraphicsScene() {
// TODO Auto-generated destructor stub
}
void SubQGraphicsScene::mouseReleaseEvent(QGraphicsScen eMouseEvent* event)
{
ultimo_x = event->scenePos().x();
ultimo_y = event->scenePos().y();
if (event->button() == Qt::LeftButton){
cout << "Right button :" << ultimo_x << ", " << ultimo_y << endl;
}else{
cout << "Left button :" << ultimo_x << ", " << ultimo_y << endl;
}
update();
this->update();
}
void SubQGraphicsScene::paintEvent(QPaintEvent *event){
cout << "PAINTER" << endl;
//QGraphicsScene::paintEvent(event);
//painter.begin(this);
painter.setPen(QColor("#FF0000"));
//painter.setBrush(QColor("#FFAAAA"));
//Draw reference points
painter.drawEllipse(ultimo_x,ultimo_y,20,20);
//Draw points
painter.end(); // close the painter
}
SubQGraphicsScene.h:
#ifndef SUBQGRAPHICSSCENE_H_
#define SUBQGRAPHICSSCENE_H_
#include <QGraphicsScene>
#include <QPainter>
#include <QtGui>
QT_BEGIN_NAMESPACE
class QPaintEvent;
QT_END_NAMESPACE
class SubQGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
SubQGraphicsScene(QObject *parent = 0);
virtual ~SubQGraphicsScene();
private:
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
QPainter painter;
int ultimo_x;
int ultimo_y;
protected:
void paintEvent(QPaintEvent *event);
};
#endif /* SUBQGRAPHICSSCENE_H_ */
SvgView.cpp:
SvgView::SvgView(QWidget *parent)
: QGraphicsView(parent)
, m_renderer(Native)
, m_svgItem(0)
, m_backgroundItem(0)
, m_outlineItem(0)
{
mScene = new SubQGraphicsScene(this);
setScene(mScene);
setTransformationAnchor(AnchorUnderMouse);
setDragMode(RubberBandDrag);
setViewportUpdateMode(FullViewportUpdate);
// Prepare background check-board pattern
QPixmap tilePixmap(64, 64);
tilePixmap.fill(Qt::white);
QPainter tilePainter(&tilePixmap);
QColor color(220, 220, 220);
tilePainter.fillRect(0, 0, 32, 32, color);
tilePainter.fillRect(32, 32, 32, 32, color);
tilePainter.end();
factor = 1.0;
setBackgroundBrush(tilePixmap);
}
void SvgView::drawBackground(QPainter *p, const QRectF &)
{
p->save();
p->resetTransform();
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
p->restore();
}
void SvgView::openFile(const QFile &file)
{
if (!file.exists())
return;
//QGraphicsScene *s = scene();
mScene->clear();
resetTransform();
QPixmap pix( "C:\\i3s\\FotosINPUT\\prueba.JPG" );
mScene->addPixmap(pix);
mScene->update();
}
void SvgView::setRenderer(RendererType type)
{
m_renderer = type;
if (m_renderer == OpenGL) {
#ifndef QT_NO_OPENGL
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
#endif
} else {
setViewport(new QWidget);
}
}
void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
{
#ifndef QT_NO_OPENGL
setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
#else
Q_UNUSED(highQualityAntialiasing);
#endif
}
void SvgView::setViewBackground(bool enable)
{
if (!m_backgroundItem)
return;
m_backgroundItem->setVisible(enable);
}
void SvgView::setViewOutline(bool enable)
{
if (!m_outlineItem)
return;
m_outlineItem->setVisible(enable);
}
void SvgView::paintEvent(QPaintEvent *event)
{
if (m_renderer == Image) {
if (m_image.size() != viewport()->size()) {
m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
}
QPainter imagePainter(&m_image);
QGraphicsView::render(&imagePainter);
imagePainter.end();
QPainter p(viewport());
p.drawImage(0, 0, m_image);
} else {
QGraphicsView::paintEvent(event);
painter.begin(this);
painter.setPen(QColor("#FF0000"));
painter.drawEllipse(100,100,20,20);
painter.end(); // close the painter
}
}
void SvgView::wheelEvent(QWheelEvent *event)
{
if(event->delta()>0){
scale(1.25, 1.25);
factor += 0.25; //Save the scale
}else{
scale(0.75, 0.75);
factor -= 0.25; //Save the scale
}
event->accept();
}
SvgView.h:
class SvgView : public QGraphicsView
{
Q_OBJECT
public:
enum RendererType { Native, OpenGL, Image };
SvgView(QWidget *parent = 0);
void openFile(const QFile &file);
void setRenderer(RendererType type = Native);
void drawBackground(QPainter *p, const QRectF &rect);
void mousePressEvent(QMouseEvent *event);
public slots:
void setHighQualityAntialiasing(bool highQualityAntialiasing);
void setViewBackground(bool enable);
void setViewOutline(bool enable);
protected:
void wheelEvent(QWheelEvent *event);
void paintEvent(QPaintEvent *event);
private:
RendererType m_renderer;
QGraphicsItem *m_svgItem;
QGraphicsRectItem *m_backgroundItem;
QGraphicsRectItem *m_outlineItem;
qreal factor;
SubQGraphicsScene *mScene;
QPainter painter;
QImage m_image;
};
It never draws an ellipse. SubQGraphicsScene::paintEvent never is called. SvgView::paintEvent is called, but not draw...
Can anyone help me, please!
Thanks in advance
I'm trying to make a program that draws a ellipse over an image when you click. I¡ll explain:
I open an image with QGraphicsScene (subclassed). In the subclass I override the mouseReleaseEvent to collect the mouse clicks. I want to draw an ellipse when click the mouse in these coordinates. The coordinates are from the image and not from the window.
This is the code, based on a example of QT:
SubQGraphicsScene.cpp
SubQGraphicsScene::SubQGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
// TODO Auto-generated constructor stub
}
SubQGraphicsScene::~SubQGraphicsScene() {
// TODO Auto-generated destructor stub
}
void SubQGraphicsScene::mouseReleaseEvent(QGraphicsScen eMouseEvent* event)
{
ultimo_x = event->scenePos().x();
ultimo_y = event->scenePos().y();
if (event->button() == Qt::LeftButton){
cout << "Right button :" << ultimo_x << ", " << ultimo_y << endl;
}else{
cout << "Left button :" << ultimo_x << ", " << ultimo_y << endl;
}
update();
this->update();
}
void SubQGraphicsScene::paintEvent(QPaintEvent *event){
cout << "PAINTER" << endl;
//QGraphicsScene::paintEvent(event);
//painter.begin(this);
painter.setPen(QColor("#FF0000"));
//painter.setBrush(QColor("#FFAAAA"));
//Draw reference points
painter.drawEllipse(ultimo_x,ultimo_y,20,20);
//Draw points
painter.end(); // close the painter
}
SubQGraphicsScene.h:
#ifndef SUBQGRAPHICSSCENE_H_
#define SUBQGRAPHICSSCENE_H_
#include <QGraphicsScene>
#include <QPainter>
#include <QtGui>
QT_BEGIN_NAMESPACE
class QPaintEvent;
QT_END_NAMESPACE
class SubQGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
SubQGraphicsScene(QObject *parent = 0);
virtual ~SubQGraphicsScene();
private:
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
QPainter painter;
int ultimo_x;
int ultimo_y;
protected:
void paintEvent(QPaintEvent *event);
};
#endif /* SUBQGRAPHICSSCENE_H_ */
SvgView.cpp:
SvgView::SvgView(QWidget *parent)
: QGraphicsView(parent)
, m_renderer(Native)
, m_svgItem(0)
, m_backgroundItem(0)
, m_outlineItem(0)
{
mScene = new SubQGraphicsScene(this);
setScene(mScene);
setTransformationAnchor(AnchorUnderMouse);
setDragMode(RubberBandDrag);
setViewportUpdateMode(FullViewportUpdate);
// Prepare background check-board pattern
QPixmap tilePixmap(64, 64);
tilePixmap.fill(Qt::white);
QPainter tilePainter(&tilePixmap);
QColor color(220, 220, 220);
tilePainter.fillRect(0, 0, 32, 32, color);
tilePainter.fillRect(32, 32, 32, 32, color);
tilePainter.end();
factor = 1.0;
setBackgroundBrush(tilePixmap);
}
void SvgView::drawBackground(QPainter *p, const QRectF &)
{
p->save();
p->resetTransform();
p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
p->restore();
}
void SvgView::openFile(const QFile &file)
{
if (!file.exists())
return;
//QGraphicsScene *s = scene();
mScene->clear();
resetTransform();
QPixmap pix( "C:\\i3s\\FotosINPUT\\prueba.JPG" );
mScene->addPixmap(pix);
mScene->update();
}
void SvgView::setRenderer(RendererType type)
{
m_renderer = type;
if (m_renderer == OpenGL) {
#ifndef QT_NO_OPENGL
setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
#endif
} else {
setViewport(new QWidget);
}
}
void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
{
#ifndef QT_NO_OPENGL
setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
#else
Q_UNUSED(highQualityAntialiasing);
#endif
}
void SvgView::setViewBackground(bool enable)
{
if (!m_backgroundItem)
return;
m_backgroundItem->setVisible(enable);
}
void SvgView::setViewOutline(bool enable)
{
if (!m_outlineItem)
return;
m_outlineItem->setVisible(enable);
}
void SvgView::paintEvent(QPaintEvent *event)
{
if (m_renderer == Image) {
if (m_image.size() != viewport()->size()) {
m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied);
}
QPainter imagePainter(&m_image);
QGraphicsView::render(&imagePainter);
imagePainter.end();
QPainter p(viewport());
p.drawImage(0, 0, m_image);
} else {
QGraphicsView::paintEvent(event);
painter.begin(this);
painter.setPen(QColor("#FF0000"));
painter.drawEllipse(100,100,20,20);
painter.end(); // close the painter
}
}
void SvgView::wheelEvent(QWheelEvent *event)
{
if(event->delta()>0){
scale(1.25, 1.25);
factor += 0.25; //Save the scale
}else{
scale(0.75, 0.75);
factor -= 0.25; //Save the scale
}
event->accept();
}
SvgView.h:
class SvgView : public QGraphicsView
{
Q_OBJECT
public:
enum RendererType { Native, OpenGL, Image };
SvgView(QWidget *parent = 0);
void openFile(const QFile &file);
void setRenderer(RendererType type = Native);
void drawBackground(QPainter *p, const QRectF &rect);
void mousePressEvent(QMouseEvent *event);
public slots:
void setHighQualityAntialiasing(bool highQualityAntialiasing);
void setViewBackground(bool enable);
void setViewOutline(bool enable);
protected:
void wheelEvent(QWheelEvent *event);
void paintEvent(QPaintEvent *event);
private:
RendererType m_renderer;
QGraphicsItem *m_svgItem;
QGraphicsRectItem *m_backgroundItem;
QGraphicsRectItem *m_outlineItem;
qreal factor;
SubQGraphicsScene *mScene;
QPainter painter;
QImage m_image;
};
It never draws an ellipse. SubQGraphicsScene::paintEvent never is called. SvgView::paintEvent is called, but not draw...
Can anyone help me, please!
Thanks in advance