Results 1 to 10 of 10

Thread: Drawing a Rubberband

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

    Default Drawing a Rubberband

    Clearly this is not useful, i need to do this on mouse events, but as a demo I thought I would just draw a static rectangle. Surprisingly I see no rectangle! Where have I gone wrong?
    Qt Code:
    1. //compile and link with
    2. // g++ main.cpp -o ss -I/usr/include/QtGui -lQtGui
    3.  
    4. #include <QWidget>
    5. #include <QDesktopWidget>
    6. #include <QApplication>
    7. #include <QPixmap>
    8. #include <QRubberBand>
    9. #include <iostream>
    10.  
    11. using namespace std;
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QApplication MyScreenshot(argc,argv);
    16.  
    17. QPoint TopLeft(100,100);
    18. QPoint BottomRight(200,200);
    19. QRect SelectionRectangle(TopLeft, BottomRight);
    20.  
    21. QRubberBand outline (QRubberBand::Rectangle);
    22. outline.setGeometry(SelectionRectangle);
    23. outline.show();
    24.  
    25. //pause
    26. int a;
    27. cin >> a;
    28.  
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Dave

  2. #2
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Drawing a Rubberband

    Where do you "exec()" your application ?

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

    Default Re: Drawing a Rubberband

    I don't want an application - i just want to use the rubberband

    I did something similar for a screenshot - and that seems to work fine:

    Qt Code:
    1. //compile and link with
    2. // g++ main.cpp -o ss -I/usr/include/QtGui -lQtGui
    3.  
    4. #include <QWidget>
    5. #include <QDesktopWidget>
    6. #include <QApplication>
    7. #include <QPixmap>
    8. #include <QRubberBand>
    9. #include <iostream>
    10.  
    11. using namespace std;
    12.  
    13. void shootScreen(char* fileName);
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QApplication MyScreenshot(argc,argv);
    18. shootScreen("test.png");
    19.  
    20. return 0;
    21. }
    22.  
    23. void shootScreen(char* fileName)
    24. {
    25. QPixmap originalPixmap;
    26. originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), 100, 500, 200 , 50);//x, y, width, height
    27. originalPixmap.save(fileName);
    28. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2008
    Location
    Ukraine, Berdyansk
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing a Rubberband

    QRubberBand inherit QWidget sow you should call exec() to process application events, paint event at least

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

    Default Re: Drawing a Rubberband

    ok cool, that drew the static rectangle!
    now to handle mouse events - i was trying to use these functions (below) but they don't seem to catch the events?
    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.  
    10. #include <iostream>
    11.  
    12. using namespace std;
    13.  
    14. void mouseMoveEvent( QMouseEvent * e);
    15. void mousePressEvent(QMouseEvent *event);
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication MyScreenshot(argc,argv);
    20.  
    21. QPoint TopLeft(100,100);
    22. QPoint BottomRight(200,200);
    23. QRect SelectionRectangle(TopLeft, BottomRight);
    24.  
    25. QRubberBand outline (QRubberBand::Rectangle);
    26. outline.setGeometry(SelectionRectangle);
    27. outline.show();
    28.  
    29. return MyScreenshot.exec();
    30. }
    31.  
    32. void mouseMoveEvent( QMouseEvent * e)
    33. {
    34. cout << "Mouse moved!" << endl;
    35. }
    36.  
    37. void mousePressEvent(QMouseEvent *event)
    38. {
    39. if (event->button() == Qt::LeftButton)
    40. {
    41. cout << "Left click!" << endl;
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Drawing a Rubberband


  7. #7
    Join Date
    May 2008
    Location
    Ukraine, Berdyansk
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing a Rubberband

    in sample above you have only one widget, you can try catch event to this widget

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

    Default Re: Drawing a Rubberband

    So what did I do wrong? It seems that I overloaded the function as it said to in that link? I guess I haven't attached it properly? But all the examples have a big class and stuff, I just want this small function - can I not do it like this?

  9. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Drawing a Rubberband

    That's not how QT works.

    Qt has a widget per widget approach,

    So you have two options :

    - Connect to a "clicked" signal (when your widget has one, which is not the case of QRubberBand).

    - Reimplement your own widget, and subclass the event functions.

    In your case I would create a class screenshotWidget and reimplement events.
    But I suggest you take a look at examples and tutorials in the Qt documentation.

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

    Default Re: Drawing a Rubberband

    so a "widget" Is like a "form" in VB? (a graphical window?) Is there no way to catch mouse events without having anything graphical? I just want to select a region of the screen during a c++ program - is using Qt not the right way to go?

Similar Threads

  1. A less demanding rubberband wanted
    By bnilsson in forum Qt Programming
    Replies: 6
    Last Post: 24th February 2008, 14:01
  2. Starting Point of Rectangle in RubberBand implementation
    By cdemirkir in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2007, 16:05
  3. QGV: ItemIgnoresTransformations and RubberBand
    By Vladimir in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2007, 15:38
  4. Drawing on QWidget - strech & resize
    By kemp in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2007, 15:39
  5. Replies: 3
    Last Post: 7th November 2006, 09:35

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.