Hello wysota,
Thank you very much for the help.
Now I made a small example to show what I want to do. To do this I followed "Diagram Scene Example" of QGraphicsView.
I overwrite the "QGraphicsScene" class as "gView" to do my image processing related tasks and drawing related tasks. I will do all image processing tasks in this class and I will use "MainWindow" to transfer signals. (like Setting line color, text size ect in "Diagram Scene Example").
I loaded image into the graphics view in "MainWindow" constructor (scene->openSceneImage("").
After I displayed image on the screen then my work will start. I will select an area of the image with the mouse (say the face on the logo). Now I want to change the color of these selected pixels of the image and then I should display the image on the screen with the changed pixels.
In this attached example in "gView" class, in button pressed event I took the start and last points in mouse pressed and released events. Now I should draw a rectangle on the image from start point to last point (with the mouse dragging) and I should change all the pixels to green. I did this on "mouseReleaseEvent" in "gView" class and displayed the image on screen with changed (green) pixels.
(I took start and last points as static because I am unable to map co-ordinates from Scene to GrapicsView).
My application need to do lot of mouse clicks on the screen (image). Can i add image (like pix=QPixmap::fromImage(image);addPixmap( pix )
to Scene every time when I click mouse like as I did in mouse release event. If this is wrong how can I display image every time with the effected pixels.
I choose QGraphicsView because some times I need to show the drowned shapes (like lines, rectangles, circles etc.,) over the image (To show the user, the area which he selected).
I tried with "QPainter" class but it is not working everywhere except in "paintEvent" (I want to run my application in all platforms ). I think it is a bit difficult to show shapes with image on the screen for every .
(My task is like quick masking tool in Photoshop).
Can you please help me whether my approach is right or wrong for my task.
Thanks in Advance
ankireddy
code:-
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QHBoxLayout>
#include "gscene.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
signals:
private slots:
void on_actionOpen_triggered();
private:
Ui::MainWindow *ui;
gScene *scene;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QHBoxLayout>
#include "gscene.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void openImage(QString path);
private slots:
void on_actionOpen_triggered();
private:
Ui::MainWindow *ui;
gScene *scene;
QGraphicsView *view;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
MainWindow.CPP
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new gScene(this);
scene
->setSceneRect
(QRectF(0,
0,
900,
900));
layout->addWidget(view);
scene->setgView(view);
widget->setLayout(layout);
connect(this,
SIGNAL(openImage
(QString)),scene,
SLOT(openSceneImage
(QString)));
//connect(scene, SIGNAL(itemInserted(DiagramItem*)),this, SLOT(itemInserted(DiagramItem*)));
setCentralWidget(widget);
setWindowTitle(tr("gViewTest"));
setUnifiedTitleAndToolBarOnMac(true);
scene->openSceneImage("");
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new gScene(this);
scene->setSceneRect(QRectF(0, 0, 900, 900));
QHBoxLayout *layout = new QHBoxLayout;
view = new QGraphicsView(scene);
layout->addWidget(view);
scene->setgView(view);
QWidget *widget = new QWidget;
widget->setLayout(layout);
connect(this,SIGNAL(openImage(QString)),scene,SLOT(openSceneImage(QString)));
//connect(scene, SIGNAL(itemInserted(DiagramItem*)),this, SLOT(itemInserted(DiagramItem*)));
setCentralWidget(widget);
setWindowTitle(tr("gViewTest"));
setUnifiedTitleAndToolBarOnMac(true);
scene->openSceneImage("");
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
gView.h
#ifndef GSCENE_H
#define GSCENE_H
#include <QGraphicsScene>
#include<QMessageBox>
#include<QGraphicsView>
#include<QGraphicsLineItem>
#include<QPixmap>
#include<QImage>
//#include <Magick++.h>
#include<QPointF>
#include<QGraphicsSceneMouseEvent>
//using namespace Magick;
{
public:
//gScene();
{
gView=view;
}
public slots:
protected:
// void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
private:
//Image magickImage;
// Blob blob;
};
#endif // GSCENE_H
#ifndef GSCENE_H
#define GSCENE_H
#include <QGraphicsScene>
#include<QMessageBox>
#include<QGraphicsView>
#include<QGraphicsLineItem>
#include<QPixmap>
#include<QImage>
//#include <Magick++.h>
#include<QPointF>
#include<QGraphicsSceneMouseEvent>
//using namespace Magick;
class gScene : public QGraphicsScene
{
public:
//gScene();
gScene( QObject *parent);
void setgView(QGraphicsView *view)
{
gView=view;
}
public slots:
void openSceneImage(QString path);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
// void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
private:
QGraphicsView *gView;
QByteArray imgData;
QPixmap pixmap;
//Image magickImage;
// Blob blob;
QImage image;
QPixmap pix;
QPointF first,last;
};
#endif // GSCENE_H
To copy to clipboard, switch view to plain text mode
gView.cpp
#include "gscene.h"
{
}
{
//first=mouseEvent->pos();
first.setX(10);
first.setY(10);
}
{
last.setX(100);
last.setY(100);
int x1=first.x();
int y1=first.y();
int x2=last.x();
int y2=last.y();
QRgb color=qRgb(0,255,0);
for(int y=y1;y<=y2;y++)
{
for(int x=x1;x<=x2;x++)
{
image.setPixel(x,y,color);
}
}
addPixmap( pix );
}
void gScene
::openSceneImage(QString path
) {
image.load("logo.png");
addPixmap( pix );
/* magickImage.read("C:\\Test\\logo.png");
//magickImage.scale(Geometry(800, 600));
magickImage.magick("RGBA");
magickImage.write(&blob);
imgData = ((char*)(blob.data()));
// STEP 3 - Display
pixmap.loadFromData(imgData,"RGBA");
addPixmap( pixmap );*/
}
#include "gscene.h"
gScene::gScene( QObject *parent = 0): QGraphicsScene(parent)
{
}
void gScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
//first=mouseEvent->pos();
first.setX(10);
first.setY(10);
}
void gScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
last.setX(100);
last.setY(100);
int x1=first.x();
int y1=first.y();
int x2=last.x();
int y2=last.y();
QRgb color=qRgb(0,255,0);
for(int y=y1;y<=y2;y++)
{
for(int x=x1;x<=x2;x++)
{
image.setPixel(x,y,color);
}
}
pix=QPixmap::fromImage(image);
addPixmap( pix );
}
void gScene::openSceneImage(QString path)
{
image.load("logo.png");
pix=QPixmap::fromImage(image);
addPixmap( pix );
/* magickImage.read("C:\\Test\\logo.png");
//magickImage.scale(Geometry(800, 600));
magickImage.magick("RGBA");
magickImage.write(&blob);
imgData = ((char*)(blob.data()));
// STEP 3 - Display
pixmap.loadFromData(imgData,"RGBA");
addPixmap( pixmap );*/
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks