Results 1 to 15 of 15

Thread: How to get mouse release event from event queue

  1. #1

    Default How to get mouse release event from event queue

    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

    Qt Code:
    1. void drawing::mouseMoveEvent(QMouseEvent *mouseEvent)
    2. {
    3. cp=mouseEvent->pos();
    4.  
    5.  
    6. if(cur.x()==-1)
    7. cur=cp;
    8.  
    9. LineDrawing(cur.x(),cur.y(),cp.x(),cp.y(),20); //Collects pixels of 20 Cursor size
    10. cur=cp;
    11. }
    To copy to clipboard, switch view to plain text mode 

    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

    }

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get mouse release event from event queue

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,728
    Thanks
    13
    Thanked 1,608 Times in 1,535 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get mouse release event from event queue

    Quote Originally Posted by ankireddy View Post
    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.
    Last edited by ChrisW67; 12th January 2012 at 00:42.

  4. #4

    Default Re: How to get mouse release event from event queue

    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

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,728
    Thanks
    13
    Thanked 1,608 Times in 1,535 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get mouse release event from event queue

    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.
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Widget: public QWidget {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *p = 0): QWidget(p), rubberBand(0)
    8. {
    9. resize(640, 480);
    10. }
    11. protected:
    12. void mousePressEvent(QMouseEvent *event)
    13. {
    14. origin = event->pos();
    15. if (!rubberBand)
    16. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    17. rubberBand->setGeometry(QRect(origin, QSize()));
    18. rubberBand->show();
    19. }
    20.  
    21. void mouseMoveEvent(QMouseEvent *event)
    22. {
    23. rubberBand->setGeometry(geo);
    24. }
    25.  
    26. void mouseReleaseEvent(QMouseEvent *event)
    27. {
    28. rubberBand->hide();
    29. // do stuff with the selected pixels
    30. qDebug() << "Grab pixels inside" << rubberBand->geometry();
    31. }
    32.  
    33. private:
    34. QPoint origin;
    35. QRubberBand *rubberBand;
    36. };
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QApplication app(argc, argv);
    41.  
    42. Widget w;
    43. w.show();
    44. return app.exec();
    45. }
    46. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. #6

    Default Re: How to get mouse release event from event queue

    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

    Qt Code:
    1. #ifndef WIMAGEVIEW_H
    2. #define WIMAGEVIEW_H
    3.  
    4. #include <QWidget>
    5. #include<QMessageBox>
    6. #include<QImage>
    7. #include<QPixmap>
    8. #include<QPaintEvent>
    9. #include<QPainter>
    10. #include<QMouseEvent>
    11. #include<QPointF>
    12. #include<QEvent>
    13. #include<QList>
    14. #include<QMap>
    15.  
    16.  
    17.  
    18. class WImageView : public QWidget
    19. {
    20. Q_OBJECT
    21. public:
    22. explicit WImageView(QWidget *parent = 0);
    23.  
    24. void paintEvent(QPaintEvent *event);
    25. QSize minimumSizeHint() const;
    26. QSize sizeHint() const;
    27. void mousePressEvent(QMouseEvent *mouseEvent);
    28. void mouseReleaseEvent(QMouseEvent *mouseEvent);
    29. void mouseMoveEvent(QMouseEvent *mouseEvent);
    30.  
    31.  
    32. signals:
    33.  
    34. public slots:
    35. void OpenImage(QString path);
    36.  
    37. private:
    38.  
    39. QImage image;
    40. QPixmap pxmap;
    41. bool readImage;
    42. QPointF start,end;
    43. QRgb penColor;
    44.  
    45. const QByteArray *imgData;
    46. const uchar *cimgData;
    47.  
    48.  
    49.  
    50.  
    51. QPoint cp,cur,prev;
    52. QList<QPoint> points;
    53. QMap<long,int> dpoints;
    54.  
    55. void linePoint(QPoint pt);
    56. void LineDrawing(int x,int y,int x2,int y2,int cursize);
    57. void RectangleAddNext(const int x,const int y,int size);
    58.  
    59. };
    60.  
    61. #endif // WIMAGEVIEW_H
    To copy to clipboard, switch view to plain text mode 

    wimageview.cpp

    Qt Code:
    1. #include "wimageview.h"
    2. #include<QColor>
    3. #include<QApplication>
    4. WImageView::WImageView(QWidget *parent) :
    5. QWidget(parent)
    6. {
    7. readImage=false;
    8. }
    9. void WImageView::OpenImage(QString path)
    10. {
    11.  
    12. cur.setX(-1);
    13. image.load(path);
    14.  
    15. pxmap=QPixmap::fromImage(image);
    16.  
    17.  
    18.  
    19. readImage=true;
    20. update();
    21. }
    22. void WImageView::paintEvent(QPaintEvent *event)
    23. {
    24.  
    25. QPainter p(this);
    26.  
    27. p.drawPixmap(0,0,pxmap.width(),pxmap.height(),pxmap);
    28.  
    29.  
    30. }
    31. QSize WImageView::minimumSizeHint() const
    32. {
    33. return QSize(400, 400);
    34. }
    35.  
    36. QSize WImageView::sizeHint() const
    37. {
    38. return QSize(600, 600);
    39. }
    40. void WImageView::mousePressEvent(QMouseEvent *mouseEvent)
    41. {
    42. start.setX(mouseEvent->x());
    43. start.setY(mouseEvent->y());
    44.  
    45. cp=cur=prev=mouseEvent->pos();
    46.  
    47. }
    48. void WImageView::mouseReleaseEvent(QMouseEvent *mouseEvent)
    49. {
    50.  
    51.  
    52. start=end;
    53.  
    54. pxmap=QPixmap::fromImage(image);
    55.  
    56. update();
    57. }
    58. void WImageView::mouseMoveEvent(QMouseEvent *mouseEvent)
    59. {
    60.  
    61.  
    62. QRgb color=qRgb(255,0,0);
    63. penColor=color;
    64.  
    65.  
    66. cp=mouseEvent->pos();
    67.  
    68. prev=cp;
    69. if(cur.x()==-1)
    70. cur=cp;
    71.  
    72. LineDrawing(cur.x(),cur.y(),prev.x(),prev.y(),20);
    73. cur=prev;
    74.  
    75.  
    76.  
    77.  
    78. }
    79.  
    80. void WImageView::linePoint(QPoint pt)
    81. {
    82.  
    83. QPainter p(&pxmap);
    84. p.setPen(penColor);
    85. p.drawPoint(pt);
    86.  
    87. QRgb color=qRgb(255,255,0);
    88. image.setPixel(pt,color);
    89.  
    90. update();
    91.  
    92. }
    93.  
    94.  
    95. void WImageView::LineDrawing(int x0,int y0,int x1, int y1,int CursorFactor)
    96. {
    97. int x,y;
    98. int dx = x1 - x0;
    99. int dy = y1 - y0;
    100.  
    101. x=x0; y=y0;
    102.  
    103. RectangleAddNext(x,y,CursorFactor);
    104.  
    105.  
    106.  
    107. int dxx=(dx>0)?dx:(-dx);
    108. int dyy=(dy>0)?dy:(-dy);
    109. if (dxx > dyy)
    110. {
    111. float m = (float) dy / (float) dx;
    112. float b = y0 - m*x0;
    113. dx = (dx < 0) ? -1 : 1;
    114. while (x0 != x1)
    115. {
    116. x0 += dx;
    117. x=x0;
    118. y=(int)(m*x0 + b);
    119. RectangleAddNext(x,y,CursorFactor);
    120.  
    121.  
    122.  
    123. }
    124. } else if (dy != 0)
    125. {
    126. float m = (float) dx / (float) dy;
    127. float b = x0 - m*y0;
    128. dy = (dy < 0) ? -1 : 1;
    129. while (y0 != y1)
    130. {
    131. y0 += dy;
    132. x=(int)(m*y0 + b);
    133. y=y0;
    134. RectangleAddNext(x,y,CursorFactor);
    135.  
    136.  
    137. }
    138. }
    139.  
    140.  
    141.  
    142.  
    143. }
    144. void WImageView::RectangleAddNext(const int x,const int y,int size)
    145. {
    146. int limit=size/2;
    147. int x1,y1;
    148. for (int i=-limit; i<=limit; i++)
    149. {
    150.  
    151. x1=x+i;
    152. y1=y+limit;
    153. if(dpoints.find(x1*10000+y1)==dpoints.end())
    154. {
    155. dpoints.insert(x1*10000+y1,10);
    156. linePoint(QPoint(x1,y1));
    157. }
    158.  
    159. x1=x+i;
    160. y1=y-limit;
    161. if(dpoints.find(x1*10000+y1)==dpoints.end())
    162. {
    163. dpoints.insert(x1*10000+y1,10);
    164. linePoint(QPoint(x1,y1));
    165. }
    166.  
    167. x1=x-limit;
    168. y1=y-i;
    169. if(dpoints.find(x1*10000+y1)==dpoints.end())
    170. {
    171. dpoints.insert(x1*10000+y1,10);
    172. linePoint(QPoint(x1,y1));
    173. }
    174.  
    175. x1=x+limit;
    176. y1=y-i;
    177. if(dpoints.find(x1*10000+y1)==dpoints.end())
    178. {
    179. dpoints.insert(x1*10000+y1,10);
    180. linePoint(QPoint(x1,y1));
    181. }
    182.  
    183.  
    184.  
    185.  
    186. }
    187.  
    188. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "wimageview.h"
    6. #include<QFileDialog>
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. signals:
    20. void openImage(QString path);
    21.  
    22. private slots:
    23. void on_actionOpen_triggered();
    24.  
    25. private:
    26. Ui::MainWindow *ui;
    27.  
    28. WImageView *gView;
    29.  
    30.  
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. gView=new WImageView();
    11.  
    12.  
    13. ui->WHLayout->addWidget(gView);
    14.  
    15. connect(this,SIGNAL(openImage(QString)),gView,SLOT(OpenImage(QString)));
    16.  
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void MainWindow::on_actionOpen_triggered()
    25. {
    26. QString fileName = QFileDialog::getOpenFileName(this, "Choose Image");
    27. //QMessageBox::information(this,"MWindow",fileName);
    28. emit openImage(fileName);
    29. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. //w.show();
    9. w.showMaximized();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get mouse release event from event queue

    So what's wrong with this code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8

    Default Re: How to get mouse release event from event queue

    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

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,393
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get mouse release event from event queue

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get mouse release event from event queue

    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.
    Attached Files Attached Files
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11

    Default Re: How to get mouse release event from event queue

    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

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,728
    Thanks
    13
    Thanked 1,608 Times in 1,535 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get mouse release event from event queue

    Open the pro file in Qt Creator and click run! or, at a command prompt:
    Qt Code:
    1. cd {source directory}
    2. qmake
    3. mingw32-make (or nmake)
    4. .\imagemasking
    To copy to clipboard, switch view to plain text mode 

  13. #13

    Default Re: How to get mouse release event from event queue

    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

  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,728
    Thanks
    13
    Thanked 1,608 Times in 1,535 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get mouse release event from event queue

    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.

    imagemasking.zip

    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.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get mouse release event from event queue

    Quote Originally Posted by ChrisW67 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Getting mouse release event system-wide
    By rittchat in forum Newbie
    Replies: 4
    Last Post: 9th February 2012, 08:08
  2. Replies: 3
    Last Post: 7th January 2012, 08:38
  3. Event queue size
    By naroin in forum Qt Programming
    Replies: 7
    Last Post: 29th November 2010, 13:26
  4. What's the size of Qt event queue?
    By ShaChris23 in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2009, 17:54
  5. Event Queue Question
    By TheGrimace in forum Qt Programming
    Replies: 10
    Last Post: 5th October 2007, 16:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.