PDA

View Full Version : How to get mouse release event from event queue



ankireddy
11th January 2012, 10:05
Hello,

I am implementing a painting program. For this I need to collect all coordinates along the mouse path and should color those pixels with specified color.
To collect coordinates I wrote code in mouseMoveEvent() as follows


void drawing::mouseMoveEvent(QMouseEvent *mouseEvent)
{
cp=mouseEvent->pos();


if(cur.x()==-1)
cur=cp;

LineDrawing(cur.x(),cur.y(),cp.x(),cp.y(),20); //Collects pixels of 20 Cursor size
cur=cp;
}

The "LineDrawing" will collect all coordinates (cursor size of 20 in rectangle shape) from cur point to cp point.

This is working only if I mouse mouse slowly and the drawing is smooth. If I mouse mouse fast then the drawing is not smooth. That is the drawing of the color is not coming with the mouse. This is because of "Linedrawing" function code. The length between cur point and cp is more (say 20 pix length) so LineDrawing is not returning quickly.

This drawing I did in VC++. There in "MouseDownEvent" I wrote the code like this

void drawing::onLbuttonDown(..)
{

cp=mouseEvent->pos();


if(cur.x()==-1)
cur=cp;

while(true)
{
GetCursorPos(&cp) //Get's the current cursor pooint
LineDrawing(cur.x(),cur.y(),cp.x(),cp.y(),20); //Collects pixels of 20 Cursor size
cur=cp;

GetMessage(&msg,NULL,0,0);
if (msg.message==WM_LBUTTONUP)
break;

}

This is giving smooth drawing because the length between cur and cp is very small (say 3 pix length) and LineDrawing returns quickly.

In Qt is there any function to get message from message queue directly (like GetMessage() in VC++). So I can do my code in Qt also.

If this approach is wrong can you please guide me how to implement paint (collect pixels along mouse path of cursor size).

Thanks in advance
anki.n

}

pkj
11th January 2012, 17:56
Reddy Sir Emi chestunaru:)...
There are more than a few mouse move events being generated. And very quickly. By the time you process them in mouseMoveEvent override, more than a couple mousemoveevents are generated and get merged into a single event. Hence you get those broken/unsmooth lines. Either you cut down your function to bare minimum. Even that will take it's due time to run. And you will miss some points nonetheless. Why don't you collect as many points you can and at the mouseRelease, draw a line between pairs of points in the collection. Or use a appropriate smoothing function/bezier curve to draw line with the available points.

ChrisW67
12th January 2012, 00:36
The "LineDrawing" will collect all coordinates (cursor size of 20 in rectangle shape) from cur point to cp point.
This is utterly unclear. The point delivered in the event has nothing to do with the size or shape of the cursor (I assume you mean mouse cursor). You have only one point delivered in each event, and you keep a copy of the last processed point (paradoxically called cur). It makes no sense to talk of "collect all coordinates from cur point to cp point" because there are only the two mouse positions and you already know them. Does "LineDrawing" actually draw a line between the two points specified as arguments or does it do something else? If LineDrawing is expecting to go into some local loop collecting more mouse points before drawing a line then you are doing it wrong.

The Scribble example quite happily keeps up with rapid mouse movements on my machine. I suggest you read the code.

ankireddy
12th January 2012, 04:09
Hello Chris,

Thank you very much for your help.

My program will do masking on image. I will load an image to the screen and will provide a brush to do masking on the image.
So if the user draws (selects) an area on the image then I should remember all those points (pixels) for further processing (storing those pixels in a map of pair <pixcoordinate,pixcolor). I should show the user selection with some color. That is while drawing with mouse I should color the pixel as well as should insert into the map.

My "LineDrawing" will collect all pixels from "cur" to "cp" point with brush size say 20. (suppose the brush is in rect shap, size is 20 and cur is (10,10) and cp is (10,20) so I should collect all pix in a rect 0,0,10,20).

With current implementation the drawing is not smooth, if I mouse the mouse fast. Because the length of Cur and cp is more so my "LineDrawing" should collect more pixels.

I did this in VC++ with my little knowledge as explained above. It is working smooth because the length of cur and cp is too less say <4 pixels even for fast mouse moves.

If I release mouse then the os should get release event. Can't I get mouse release event from the event queue.

If I am going in a wrong way can you pleas guide me how to implement this.

Thanks in advance
anki.n

ChrisW67
12th January 2012, 05:32
You seem to be expecting that you will receive mouse move and release events while you are busy inside the mouse down event handler. It isn't going to happen that way. Your program will receive the mouse button release event after you release the mouse button and the program returns to the Qt event loop. Doing any lengthy process inside the event handler is delaying the next opportunity that Qt has to deliver a mouse update... leading to larger steps.

Ultimately you are trying to implement a rubber band style selection of a rectangular region. Have you looked at QRubberBand? If you want to force it to increments of your brush size then you would do that in the mouse move event before setting the rubber band geometry.


#include <QtGui>
#include <QDebug>

class Widget: public QWidget {
Q_OBJECT
public:
Widget(QWidget *p = 0): QWidget(p), rubberBand(0)
{
resize(640, 480);
}
protected:
void mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}

void mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(geo);
}

void mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();
// do stuff with the selected pixels
qDebug() << "Grab pixels inside" << rubberBand->geometry();
}

private:
QPoint origin;
QRubberBand *rubberBand;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Widget w;
w.show();
return app.exec();
}
#include "main.moc"

ankireddy
12th January 2012, 09:41
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>



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

wimageview.cpp


#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(),pxma p);


}
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));
}




}

}

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


mainwindow.cpp


#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);
}






main.cpp


#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();
}

wysota
12th January 2012, 11:43
So what's wrong with this code?

ankireddy
12th January 2012, 12:21
Hi wysota,

Thank you for the reply.

When I move the mouse fast the drawing is not smooth (not painting with the speed of the mouse). The brush drawing (painting) is not fast as much as mouse movement. Painting with jerks i.e mouse is at one place but drawing is appearing after a bit of time. I want to show the painting with the speed of the mouse.

How can I catch "mouseRelease" event from the event queue?

You please excuse me if I am doing repeated replies.

Can you please help me how to do painting as fast as moue move.

If I am doing wrong can you please help me what to do?


Thanks in advance
anki.n

stampede
12th January 2012, 12:58
Try to collect the points during mouse events, for example in a QList. Then during paintEvent, present the current state of the object - use the QPainter's methods (drawLine, drawPoint, or others) to draw the collected points. It should be faster than accessing image pixels during each mouse move event - quote from the documentation of setPixel:

This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine() to access pixel data directly.

wysota
12th January 2012, 15:10
Here is my fairy complete implementation of an image masking app. It has some glitches related to how subtracting painter paths works but apart from that it's quite functional.

ankireddy
17th January 2012, 04:19
Hello wysota,

Than you very much for the example.

As I am very much beginner I am not able to understand how to run this. Can you please guide me how to run this?

Qt don't have any call to get event directly from event queue?

Thanks and regards
anki.n

ChrisW67
17th January 2012, 04:41
Open the pro file in Qt Creator and click run! or, at a command prompt:


cd {source directory}
qmake
mingw32-make (or nmake)
.\imagemasking

ankireddy
17th January 2012, 05:20
Hello Chirs,

The attached "imagemasking.tar.gz‎" doesn't not have any .pro file. It has only one file and it includes some xml data also. I don't have any idea how to deal with this xml file.

can you please help me how to runt this?


Qt don't have any call to get event directly from event queue?

Thanks & Regards
anki.n

ChrisW67
17th January 2012, 06:53
The file is a doubly gzipped tar archive... I assume that wysota did not do that deliberately. I have attached a zip version of the same for the Windows-challenged.

7281

Repeatedly asking the same question will not change the answer. You do not "get" events from the queue they are delivered to your event handlers.

wysota
17th January 2012, 09:01
The file is a doubly gzipped tar archive... I assume that wysota did not do that deliberately.
Again? I'm starting to think this forum does something to the archives I upload.