I solved it:
- FIRST solution:
{
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;
}
addEllipse(ultimo_x-10,ultimo_y-10,20,20);
}
void SubQGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* 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;
}
addEllipse(ultimo_x-10,ultimo_y-10,20,20);
}
To copy to clipboard, switch view to plain text mode
This code draw a ellipse when you click the mouse. This solution is very basic, but it works fine!
- SECOND solution:
For my interest, I subclassed QGraphicsItem and I paint in this class. Then I added it to the scene. It works perfect because I can draw a lot of things (ellipse, lines, etc). And is automatically scaled!
If some has the same problem as me, there is the full code:
svgView.cpp:
#include "svgview.h"
#include <QFile>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QGraphicsRectItem>
#include <QGraphicsSvgItem>
#include <QPaintEvent>
#include <qmath.h>
#include <iostream>
using namespace std;
#ifndef QT_NO_OPENGL
#include <QGLWidget>
#endif
, 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
tilePixmap.fill(Qt::white);
tilePainter.fillRect(0, 0, 32, 32, color);
tilePainter.fillRect(32, 32, 32, 32, color);
tilePainter.end();
factor = 1.0;
setBackgroundBrush(tilePixmap);
}
{
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();
fpPaint = new FingerPrintPainter(mScene->sceneRect());
mScene->addItem(fpPaint);
}
void SvgView::setRenderer(RendererType type)
{
m_renderer = type;
if (m_renderer == OpenGL) {
#ifndef QT_NO_OPENGL
#endif
} else {
}
}
void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing)
{
#ifndef QT_NO_OPENGL
setRenderHint
(QPainter::HighQualityAntialiasing, highQualityAntialiasing
);
#else
Q_UNUSED(highQualityAntialiasing);
#endif
}
{
if(event->delta()>0){
scale(1.25, 1.25);
factor += 0.25;
}else{
scale(0.75, 0.75);
factor -= 0.25;
}
event->accept();
}
#include "svgview.h"
#include <QFile>
#include <QWheelEvent>
#include <QMouseEvent>
#include <QGraphicsRectItem>
#include <QGraphicsSvgItem>
#include <QPaintEvent>
#include <qmath.h>
#include <iostream>
using namespace std;
#ifndef QT_NO_OPENGL
#include <QGLWidget>
#endif
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();
fpPaint = new FingerPrintPainter(mScene->sceneRect());
mScene->addItem(fpPaint);
}
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::wheelEvent(QWheelEvent *event)
{
if(event->delta()>0){
scale(1.25, 1.25);
factor += 0.25;
}else{
scale(0.75, 0.75);
factor -= 0.25;
}
event->accept();
}
To copy to clipboard, switch view to plain text mode
svgView.h:
#ifndef SVGVIEW_H
#define SVGVIEW_H
#include <QGraphicsView>
#include <QPainter>
#include "SubQGraphicsScene.h"
#include "FingerPrintPainter.h"
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
{
Q_OBJECT
public:
enum RendererType { Native, OpenGL, Image };
void openFile
(const QFile &file
);
void setRenderer(RendererType type = Native);
public slots:
void setHighQualityAntialiasing(bool highQualityAntialiasing);
protected:
private:
RendererType m_renderer;
qreal factor;
SubQGraphicsScene *mScene;
FingerPrintPainter *fpPaint;
};
#endif // SVGVIEW_H
#ifndef SVGVIEW_H
#define SVGVIEW_H
#include <QGraphicsView>
#include <QPainter>
#include "SubQGraphicsScene.h"
#include "FingerPrintPainter.h"
QT_BEGIN_NAMESPACE
class QWheelEvent;
class QPaintEvent;
class QFile;
QT_END_NAMESPACE
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);
public slots:
void setHighQualityAntialiasing(bool highQualityAntialiasing);
protected:
void wheelEvent(QWheelEvent *event);
private:
RendererType m_renderer;
QGraphicsItem *m_svgItem;
QGraphicsRectItem *m_backgroundItem;
QGraphicsRectItem *m_outlineItem;
qreal factor;
SubQGraphicsScene *mScene;
QPainter painter;
QImage m_image;
FingerPrintPainter *fpPaint;
};
#endif // SVGVIEW_H
To copy to clipboard, switch view to plain text mode
subQGraphicsScene.cpp:
#include "SubQGraphicsScene.h"
#include <QWheelEvent>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
#include <iostream>
using namespace std;
SubQGraphicsScene
::SubQGraphicsScene(QObject *parent
){
// TODO Auto-generated constructor stub
}
SubQGraphicsScene::~SubQGraphicsScene() {
// TODO Auto-generated destructor stub
}
{
ultimo_x = event->scenePos().x();
ultimo_y = event->scenePos().y();
if (event->button() == Qt::LeftButton){
cout << "Botón derecho en :" << ultimo_x << ", " << ultimo_y << endl;
}else{
cout << "Botón izquierdo en :" << ultimo_x << ", " << ultimo_y << endl;
}
addEllipse(ultimo_x-10,ultimo_y-10,20,20);
}
#include "SubQGraphicsScene.h"
#include <QWheelEvent>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
#include <iostream>
using namespace std;
SubQGraphicsScene::SubQGraphicsScene(QObject *parent)
: QGraphicsScene(parent)
{
// TODO Auto-generated constructor stub
}
SubQGraphicsScene::~SubQGraphicsScene() {
// TODO Auto-generated destructor stub
}
void SubQGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
ultimo_x = event->scenePos().x();
ultimo_y = event->scenePos().y();
if (event->button() == Qt::LeftButton){
cout << "Botón derecho en :" << ultimo_x << ", " << ultimo_y << endl;
}else{
cout << "Botón izquierdo en :" << ultimo_x << ", " << ultimo_y << endl;
}
addEllipse(ultimo_x-10,ultimo_y-10,20,20);
}
To copy to clipboard, switch view to plain text mode
SubQGraphicsScene.h:
#ifndef SUBQGRAPHICSSCENE_H_
#define SUBQGRAPHICSSCENE_H_
#include <QGraphicsScene>
#include <QPainter>
#include <QtGui>
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
{
Q_OBJECT
public:
SubQGraphicsScene
(QObject *parent
= 0);
virtual ~SubQGraphicsScene();
private:
int ultimo_x;
int ultimo_y;
};
#endif /* 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;
};
#endif /* SUBQGRAPHICSSCENE_H_ */
To copy to clipboard, switch view to plain text mode
SubQGraphicsItem.cpp:
#include "SubQGraphicsItem.h"
#include <QPainter>
#include <time.h>
#include <iostream>
using namespace std;
SubQGraphicsItem
::SubQGraphicsItem(QRectF r
) { // TODO Auto-generated constructor stub
rect = r;
}
SubQGraphicsItem::~SubQGraphicsItem() {
// TODO Auto-generated destructor stub
}
{
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray);
srand ( time(NULL) );
int mRandom;
int mRandom2;
for (int i=0; i<100; i++){
mRandom = rand() % 3312 + 1;
mRandom2 = rand() % 4416 + 1;
painter->drawEllipse(mRandom, mRandom2, 20, 20);
}
}
QRectF SubQGraphicsItem
::boundingRect() const {
return rect;
}
#include "SubQGraphicsItem.h"
#include <QPainter>
#include <time.h>
#include <iostream>
using namespace std;
SubQGraphicsItem::SubQGraphicsItem(QRectF r) {
// TODO Auto-generated constructor stub
rect = r;
}
SubQGraphicsItem::~SubQGraphicsItem() {
// TODO Auto-generated destructor stub
}
void SubQGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray);
srand ( time(NULL) );
int mRandom;
int mRandom2;
for (int i=0; i<100; i++){
mRandom = rand() % 3312 + 1;
mRandom2 = rand() % 4416 + 1;
painter->drawEllipse(mRandom, mRandom2, 20, 20);
}
}
QRectF SubQGraphicsItem::boundingRect() const
{
return rect;
}
To copy to clipboard, switch view to plain text mode
SubQGraphicsItem.h:
#ifndef SubQGraphicsItem_H_
#define SubQGraphicsItem_H_
#include <QGraphicsItem>
{
public:
virtual ~SubQGraphicsItem();
private:
};
#endif /* SubQGraphicsItem_H_ */
#ifndef SubQGraphicsItem_H_
#define SubQGraphicsItem_H_
#include <QGraphicsItem>
class SubQGraphicsItem : public QGraphicsItem
{
public:
SubQGraphicsItem(QRectF r);
virtual ~SubQGraphicsItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
QRectF rect;
};
#endif /* SubQGraphicsItem_H_ */
To copy to clipboard, switch view to plain text mode
This code draws 30 random ellipses.
I hope it helps someone!!
Thanks!
Bookmarks