Results 1 to 7 of 7

Thread: Mouse Events

  1. #1
    Join Date
    May 2008
    Posts
    20
    Thanks
    1

    Default Mouse Events

    I am trying to draw a rectangle anywhere on the screen when the user drags the mouse - as an easier problem I was trying to draw a static rectangle when the mouse is either moved or clicked. Here's what I came up with by following the examples/tutorials, but it doesn't do anything??

    Qt Code:
    1. // g++ rubber.cpp -o ss -lQtGui
    2.  
    3. #include <QtGui/QWidget>
    4. #include <QtGui/QDesktopWidget>
    5. #include <QtGui/QApplication>
    6. #include <QtGui/QPixmap>
    7. #include <QtGui/QRubberBand>
    8. #include <QtGui/QMouseEvent>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QFont>
    11.  
    12. #include <iostream>
    13.  
    14. using namespace std;
    15.  
    16. class MyWidget : public QWidget
    17. {
    18. public:
    19. MyWidget(QWidget *parent = 0);
    20. protected:
    21. void mouseMoveEvent( QMouseEvent * e);
    22. void mousePressEvent(QMouseEvent *event);
    23. };
    24.  
    25. MyWidget::MyWidget(QWidget *parent)
    26. : QWidget(parent)
    27. {
    28. setFixedSize(200, 120);
    29.  
    30. QPushButton *quit = new QPushButton(tr("Quit"), this);
    31. quit->setGeometry(62, 40, 75, 30);
    32.  
    33. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    34. }
    35.  
    36.  
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QApplication MyScreenshot(argc,argv);
    41.  
    42. /*
    43. QPoint TopLeft(100,100);
    44. QPoint BottomRight(200,200);
    45. QRect SelectionRectangle(TopLeft, BottomRight);
    46.  
    47. QRubberBand outline (QRubberBand::Rectangle);
    48. outline.setGeometry(SelectionRectangle);
    49. outline.show();
    50. */
    51. return MyScreenshot.exec();
    52. }
    53.  
    54. void MyWidget::mouseMoveEvent( QMouseEvent * e)
    55. {
    56. cout << "Mouse moved!" << endl;
    57.  
    58. QPoint TopLeft(100,100);
    59. QPoint BottomRight(200,200);
    60. QRect SelectionRectangle(TopLeft, BottomRight);
    61.  
    62. QRubberBand outline (QRubberBand::Rectangle);
    63. outline.setGeometry(SelectionRectangle);
    64. outline.show();
    65.  
    66. }
    67.  
    68. void MyWidget::mousePressEvent(QMouseEvent *event)
    69. {
    70. if (event->button() == Qt::LeftButton)
    71. {
    72. cout << "Left click!" << endl;
    73. }
    74.  
    75. QPoint TopLeft(100,100);
    76. QPoint BottomRight(200,200);
    77. QRect SelectionRectangle(TopLeft, BottomRight);
    78.  
    79. QRubberBand outline (QRubberBand::Rectangle);
    80. outline.setGeometry(SelectionRectangle);
    81. outline.show();
    82.  
    83. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse Events

    You are making multiple QRubberBane objects in mousemove and mouse press events.
    Cant say how wil they work. What you need is one QRubberband object in the widget. On mouse press event just show the rubberband with some smallest size. On mousemove event, set the geometry of the rubberband only.

    If you just want to show static rectangle, u cud keep a variable for mousepressed. In the paintevent of the widget, check that variable and draw a rectangle. something like -
    Qt Code:
    1. paintEvent()
    2. {
    3. QPainter painter(this);
    4. painter.drawRectangle();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I hope you got the idea

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

    Default Re: Mouse Events

    The rubber band is a local object that gets destroyed one the flow leaves the mousePressEvent method. Anyway if you want to do region selection for a screenshot program, it might be simpler to grab the pixmap of the whole desktop, show it over the real desktop and then do the selection.

  4. #4
    Join Date
    May 2008
    Posts
    20
    Thanks
    1

    Default Re: Mouse Events

    Do I have to create an instance of MyWidget (as shown in main() below)? If so, then how do I access the "outline" object in the widget from the mouse event function?

    Qt Code:
    1. // g++ rubber.cpp -o ss -lQtGui
    2.  
    3. #include <QtGui/QWidget>
    4. #include <QtGui/QDesktopWidget>
    5. #include <QtGui/QApplication>
    6. #include <QtGui/QPixmap>
    7. #include <QtGui/QRubberBand>
    8. #include <QtGui/QMouseEvent>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QFont>
    11.  
    12. #include <iostream>
    13.  
    14. using namespace std;
    15.  
    16. class MyWidget : public QWidget
    17. {
    18. public:
    19. MyWidget(QWidget *parent = 0);
    20. QRubberBand outline (QRubberBand::Rectangle);
    21. protected:
    22. void mousePressEvent(QMouseEvent *event);
    23.  
    24. };
    25.  
    26. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    27. {
    28.  
    29. }
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QApplication MyScreenshot(argc,argv);
    34. QPoint TopLeft(100,100);
    35. QPoint BottomRight(200,200);
    36. QRect SelectionRectangle(TopLeft, BottomRight);
    37.  
    38. MyWidget A();
    39. A.outline.setGeometry(SelectionRectangle);
    40. A.outline.show();
    41.  
    42. return MyScreenshot.exec();
    43. }
    44.  
    45. void MyWidget::mousePressEvent(QMouseEvent *event)
    46. {
    47. //move the rectangle
    48. QPoint TopLeft(100, 200);
    49. QPoint BottomRight(200,200);
    50. QRect SelectionRectangle(TopLeft, BottomRight);
    51.  
    52. A.outline.setGeometry(SelectionRectangle); //this doesn't work because A is not declared in this scope
    53. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Mouse Events

    Skip the "A." part in the event handler - you are already in the "A" object.

  6. #6
    Join Date
    May 2008
    Posts
    20
    Thanks
    1

    Default Re: Mouse Events

    haha oh yea of course. then how about this:

    in the mouse event function:
    error: ‘((MyWidget*)this)->MyWidget:utline’ does not have class type


    Qt Code:
    1. // g++ rubber.cpp -o ss -lQtGui
    2.  
    3. #include <QtGui/QWidget>
    4. #include <QtGui/QDesktopWidget>
    5. #include <QtGui/QApplication>
    6. #include <QtGui/QPixmap>
    7. #include <QtGui/QRubberBand>
    8. #include <QtGui/QMouseEvent>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QFont>
    11. #include <QtGui/QPainter>
    12.  
    13. #include <iostream>
    14.  
    15. using namespace std;
    16.  
    17. class MyWidget : public QWidget
    18. {
    19. public:
    20. MyWidget(QWidget *parent = 0);
    21. QRubberBand outline();
    22.  
    23. protected:
    24. void mousePressEvent(QMouseEvent *event);
    25.  
    26. };
    27.  
    28. MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    29. {
    30.  
    31. }
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35. QApplication MyScreenshot(argc,argv);
    36. QPoint TopLeft(100,100);
    37. QPoint BottomRight(200,200);
    38. QRect SelectionRectangle(TopLeft, BottomRight);
    39.  
    40. MyWidget A();
    41. return MyScreenshot.exec();
    42. }
    43.  
    44. void MyWidget::mousePressEvent(QMouseEvent *event)
    45. {
    46.  
    47. QPoint TopLeft(100, 200);
    48. QPoint BottomRight(200,200);
    49. QRect SelectionRectangle(TopLeft, BottomRight);
    50. outline.setGeometry(SelectionRectangle);
    51. outline.show();
    52. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Mouse Events

    Quote Originally Posted by daviddoria View Post
    g++ rubber.cpp -o ss -lQtGui
    You should use qmake to generate a Makefile. Please, take a look at Qt Tutorial 1 (pay special attention to the qmake commands in the end).
    J-P Nurmi

Similar Threads

  1. Replies: 9
    Last Post: 22nd June 2008, 22:26
  2. Replies: 2
    Last Post: 2nd April 2008, 14:19
  3. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  4. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25
  5. Forwarding mouse events to another widget.
    By yogeshm02 in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2006, 13:25

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.