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