Hello Crish,
Thank you very much for the reply.
The rubberband example may not fulfill my requirement. I developed an example and attached here. Please have look at my example.
I may use rectangle brush or circle brush of any size.
Load image and drag mouse slowly. You will get smooth drawing. Now move mouse fast and see the difference.
In my code "LineDrawing" will give all pixels in a line from cur to cp points.
The "RectangleAddNext" will give all pixels of a rectangle for a given point. The "dpoints" is used to avoid repetitions of the points.
I should get all pixels while mouse move only.
Here I am applying "red" for drawing and "Yellow" on image (this is an example only. I need to do some operations on pixels). I can't do this in "mouseRelease". If I do like that the user can't see any thing till he releases mouse.
Qt don't have any call to get event from event queue. Even if write a while in "mousePressEvent", the moment I release the mouse the OS will sent "mouse release" event to event queue. So in while loop if I get event from event queue then I can break from while. This what I did in another language.
can you please show me a way to do this task.
Thanks in advance
anki.n
Code
wimageview.h
#ifndef WIMAGEVIEW_H
#define WIMAGEVIEW_H
#include <QWidget>
#include<QMessageBox>
#include<QImage>
#include<QPixmap>
#include<QPaintEvent>
#include<QPainter>
#include<QMouseEvent>
#include<QPointF>
#include<QEvent>
#include<QList>
#include<QMap>
{
Q_OBJECT
public:
explicit WImageView
(QWidget *parent
= 0);
QSize minimumSizeHint
() const;
signals:
public slots:
private:
bool readImage;
QRgb penColor;
const uchar *cimgData;
QList<QPoint> points;
QMap<long,int> dpoints;
void LineDrawing(int x,int y,int x2,int y2,int cursize);
void RectangleAddNext(const int x,const int y,int size);
};
#endif // WIMAGEVIEW_H
#ifndef WIMAGEVIEW_H
#define WIMAGEVIEW_H
#include <QWidget>
#include<QMessageBox>
#include<QImage>
#include<QPixmap>
#include<QPaintEvent>
#include<QPainter>
#include<QMouseEvent>
#include<QPointF>
#include<QEvent>
#include<QList>
#include<QMap>
class WImageView : public QWidget
{
Q_OBJECT
public:
explicit WImageView(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
QSize minimumSizeHint() const;
QSize sizeHint() const;
void mousePressEvent(QMouseEvent *mouseEvent);
void mouseReleaseEvent(QMouseEvent *mouseEvent);
void mouseMoveEvent(QMouseEvent *mouseEvent);
signals:
public slots:
void OpenImage(QString path);
private:
QImage image;
QPixmap pxmap;
bool readImage;
QPointF start,end;
QRgb penColor;
const QByteArray *imgData;
const uchar *cimgData;
QPoint cp,cur,prev;
QList<QPoint> points;
QMap<long,int> dpoints;
void linePoint(QPoint pt);
void LineDrawing(int x,int y,int x2,int y2,int cursize);
void RectangleAddNext(const int x,const int y,int size);
};
#endif // WIMAGEVIEW_H
To copy to clipboard, switch view to plain text mode
wimageview.cpp
#include "wimageview.h"
#include<QColor>
#include<QApplication>
WImageView
::WImageView(QWidget *parent
) :{
readImage=false;
}
void WImageView
::OpenImage(QString path
) {
cur.setX(-1);
image.load(path);
readImage=true;
update();
}
{
p.drawPixmap(0,0,pxmap.width(),pxmap.height(),pxmap);
}
QSize WImageView
::minimumSizeHint() const {
}
QSize WImageView
::sizeHint() const {
}
void WImageView
::mousePressEvent(QMouseEvent *mouseEvent
) {
start.setX(mouseEvent->x());
start.setY(mouseEvent->y());
cp=cur=prev=mouseEvent->pos();
}
void WImageView
::mouseReleaseEvent(QMouseEvent *mouseEvent
) {
start=end;
update();
}
void WImageView
::mouseMoveEvent(QMouseEvent *mouseEvent
) {
QRgb color=qRgb(255,0,0);
penColor=color;
cp=mouseEvent->pos();
prev=cp;
if(cur.x()==-1)
cur=cp;
LineDrawing(cur.x(),cur.y(),prev.x(),prev.y(),20);
cur=prev;
}
void WImageView
::linePoint(QPoint pt
) {
p.setPen(penColor);
p.drawPoint(pt);
QRgb color=qRgb(255,255,0);
image.setPixel(pt,color);
update();
}
void WImageView::LineDrawing(int x0,int y0,int x1, int y1,int CursorFactor)
{
int x,y;
int dx = x1 - x0;
int dy = y1 - y0;
x=x0; y=y0;
RectangleAddNext(x,y,CursorFactor);
int dxx=(dx>0)?dx:(-dx);
int dyy=(dy>0)?dy:(-dy);
if (dxx > dyy)
{
float m = (float) dy / (float) dx;
float b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1)
{
x0 += dx;
x=x0;
y=(int)(m*x0 + b);
RectangleAddNext(x,y,CursorFactor);
}
} else if (dy != 0)
{
float m = (float) dx / (float) dy;
float b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1)
{
y0 += dy;
x=(int)(m*y0 + b);
y=y0;
RectangleAddNext(x,y,CursorFactor);
}
}
}
void WImageView::RectangleAddNext(const int x,const int y,int size)
{
int limit=size/2;
int x1,y1;
for (int i=-limit; i<=limit; i++)
{
x1=x+i;
y1=y+limit;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
}
x1=x+i;
y1=y-limit;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
}
x1=x-limit;
y1=y-i;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
}
x1=x+limit;
y1=y-i;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
}
}
}
#include "wimageview.h"
#include<QColor>
#include<QApplication>
WImageView::WImageView(QWidget *parent) :
QWidget(parent)
{
readImage=false;
}
void WImageView::OpenImage(QString path)
{
cur.setX(-1);
image.load(path);
pxmap=QPixmap::fromImage(image);
readImage=true;
update();
}
void WImageView::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.drawPixmap(0,0,pxmap.width(),pxmap.height(),pxmap);
}
QSize WImageView::minimumSizeHint() const
{
return QSize(400, 400);
}
QSize WImageView::sizeHint() const
{
return QSize(600, 600);
}
void WImageView::mousePressEvent(QMouseEvent *mouseEvent)
{
start.setX(mouseEvent->x());
start.setY(mouseEvent->y());
cp=cur=prev=mouseEvent->pos();
}
void WImageView::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
start=end;
pxmap=QPixmap::fromImage(image);
update();
}
void WImageView::mouseMoveEvent(QMouseEvent *mouseEvent)
{
QRgb color=qRgb(255,0,0);
penColor=color;
cp=mouseEvent->pos();
prev=cp;
if(cur.x()==-1)
cur=cp;
LineDrawing(cur.x(),cur.y(),prev.x(),prev.y(),20);
cur=prev;
}
void WImageView::linePoint(QPoint pt)
{
QPainter p(&pxmap);
p.setPen(penColor);
p.drawPoint(pt);
QRgb color=qRgb(255,255,0);
image.setPixel(pt,color);
update();
}
void WImageView::LineDrawing(int x0,int y0,int x1, int y1,int CursorFactor)
{
int x,y;
int dx = x1 - x0;
int dy = y1 - y0;
x=x0; y=y0;
RectangleAddNext(x,y,CursorFactor);
int dxx=(dx>0)?dx:(-dx);
int dyy=(dy>0)?dy:(-dy);
if (dxx > dyy)
{
float m = (float) dy / (float) dx;
float b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1)
{
x0 += dx;
x=x0;
y=(int)(m*x0 + b);
RectangleAddNext(x,y,CursorFactor);
}
} else if (dy != 0)
{
float m = (float) dx / (float) dy;
float b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1)
{
y0 += dy;
x=(int)(m*y0 + b);
y=y0;
RectangleAddNext(x,y,CursorFactor);
}
}
}
void WImageView::RectangleAddNext(const int x,const int y,int size)
{
int limit=size/2;
int x1,y1;
for (int i=-limit; i<=limit; i++)
{
x1=x+i;
y1=y+limit;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
linePoint(QPoint(x1,y1));
}
x1=x+i;
y1=y-limit;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
linePoint(QPoint(x1,y1));
}
x1=x-limit;
y1=y-i;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
linePoint(QPoint(x1,y1));
}
x1=x+limit;
y1=y-i;
if(dpoints.find(x1*10000+y1)==dpoints.end())
{
dpoints.insert(x1*10000+y1,10);
linePoint(QPoint(x1,y1));
}
}
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "wimageview.h"
#include<QFileDialog>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
signals:
private slots:
void on_actionOpen_triggered();
private:
Ui::MainWindow *ui;
WImageView *gView;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "wimageview.h"
#include<QFileDialog>
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;
WImageView *gView;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
gView=new WImageView();
ui->WHLayout->addWidget(gView);
connect(this,
SIGNAL(openImage
(QString)),gView,
SLOT(OpenImage
(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
//QMessageBox::information(this,"MWindow",fileName);
emit openImage(fileName);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
gView=new WImageView();
ui->WHLayout->addWidget(gView);
connect(this,SIGNAL(openImage(QString)),gView,SLOT(OpenImage(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, "Choose Image");
//QMessageBox::information(this,"MWindow",fileName);
emit openImage(fileName);
}
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();
w.showMaximized();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//w.show();
w.showMaximized();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks