Results 1 to 16 of 16

Thread: QRubberBand problem

  1. #1
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QRubberBand problem

    Hi there,

    I'm using customGL widget which derived from QGLWidget. I wanna use QRubberBand on this widget my code like this ;

    Qt Code:
    1. class HSGLWidget : public QGLWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. HSGLWidget(QWidget *parent = 0);
    7.  
    8. private:
    9. QRubberBand *rubberBand;
    10. QPoint origin;
    11.  
    12. protected:
    13.  
    14. void initializeGL();
    15. void resizeGL(int w, int h);
    16. //void paintGL();
    17.  
    18. void mousePressEvent(QMouseEvent *e);
    19. void mouseMoveEvent(QMouseEvent *e);
    20. void mouseReleaseEvent(QMouseEvent *e);
    21. void keyPressEvent(QKeyEvent *e);
    22. };
    23.  
    24.  
    25. void HSGLWidget::mousePressEvent( QMouseEvent *e )
    26. {
    27. origin = e->pos();
    28. if (!rubberBand)
    29. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    30. rubberBand->setGeometry(QRect(origin, QSize()));
    31. rubberBand->show();
    32.  
    33. }
    34.  
    35. void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
    36. {
    37. rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
    38.  
    39. }
    40.  
    41. void HSGLWidget::mouseReleaseEvent(QMouseEvent *e)
    42. {
    43. rubberBand->hide();
    44. }
    To copy to clipboard, switch view to plain text mode 

    It's working with this implementation but there is something weird when i press the mouse button and moving mouse i see many Qrubberband flooding on the HSGLWidget if i move mouse quickly there is few if move slowly there is huge flooding. What is the problem am i using something wrong or this is QRubberBand issue , thanks for you help.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    show your implementation - specially where you allocate 'rubberBand'.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    thx for reply ; I allocated it

    Qt Code:
    1. if (!rubberBand)
    2. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    If its not a case of re-allocation of the rubber band, then it probably has to do with the way you update the QGLWidget such that the old content is not cleared, and only the new one is added.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    Quote Originally Posted by high_flyer View Post
    If its not a case of re-allocation of the rubber band, then it probably has to do with the way you update the QGLWidget such that the old content is not cleared, and only the new one is added.
    yup, it's look like my problem , thenm i don't know how can i refresh my HSGLWidget , could you help me about that ?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    I can try, but there are all kind of things that can play in to this.
    I will try the simple stuff first.
    Show the implementation of the constructor.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    thanks for your help high_flyer ,

    to be honest i just trying to figur out using of RubberBand on simply GLWidget, so there is no code at ctor,

  8. #8
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    I really stuck with refresh CustomWidgets content, could you help figure out this ?

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    I mean the ctor of your QGLWidget implementation.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    hi, high_flyer ;

    As i said before for moment there is no code at ctor. I'm just trying using QRubberBand on QGLWidget so there is problem as you noticed QGLWidget doesn't refresh itself and there is problem like flooding QRubberBand, can i use QRubberBand on QGLWidget actually i wanna know this atm ... Thanks for your interest ...

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    Ok, so it means you have default flags on it - but then I don't know why its not getting updates when you drag the rubber band on it.
    You can try sub classing QRubberBand, and override the resizeEvent() and send a signal from it.
    And in your QGLWidget, intercept that signal, and call update().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    Thanks for advice high_flyer, i've actually tried bind a pushbutton clicked signal with qglwidget its , update and repaint slots but it doesn't any effect, i even tried custom rubberband which derived form Qrubberband override paint events also it's not working too =) , so let me know is it possible to use QRubberBand on QGLWdiget or not ?

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    is it possible to use QRubberBand on QGLWdiget or not ?
    It must be possible, since QGLWidget is a QWidget.

    I don't know what it is you are doing wrong, based on the information you provided so far...

    Try to make a test project where you only have a widget and QRubberBand drawn on it.
    Once you get it to work there, try comparing between that and you QGLWidget usage of QRubberBand.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    hi high_flyer ;

    I found something when i was digging to google :
    Qt Code:
    1. myRubberBand = new QRubberBand( QRubberBand::Rectangle, this );
    2. 00090 if (myRubberBand)
    3. 00091 {
    4. 00092 // If you don't set a style, QRubberBand doesn't work properly
    5. 00093 // take these lines out if you don't believe me.
    6. 00094 // QStyle* ps = (QStyle*) new QutlProxyStyle("Plastique");
    7. 00095 QStyle* ps = (QStyle*) new QPlastiqueStyle();
    8. 00096 myRubberBand->setStyle( ps );
    9. 00097 }
    To copy to clipboard, switch view to plain text mode 

    many people have same problem with QGLWidget so there is a solution who claim that but it's not workinf still :s


    Added after 5 minutes:


    So i made a simple test project i send fully code what i've done ;

    Qt Code:
    1. #ifndef HSGLWIDGET_H
    2. #define HSGLWIDGET_H
    3.  
    4. #include <QtOpenGL/QGLWidget>
    5. #include <QMouseEvent>
    6. #include <QRubberBand>
    7. #include <QPoint>
    8.  
    9. class CustomRB : public QRubberBand
    10. {
    11. public:
    12. CustomRB(Shape s, QWidget *parent) : QRubberBand(s,parent) {
    13.  
    14. }
    15.  
    16. void paintEvent(QPaintEvent *event)
    17. {
    18. Q_UNUSED(event);
    19.  
    20. QPainter painter;
    21. QPen pen = QPen(Qt::yellow);
    22. pen.setWidth(5);
    23. pen.setStyle(Qt::DashLine);
    24.  
    25. QBrush brush = QBrush(Qt::red);
    26.  
    27. painter.begin(this);
    28. painter.setPen(pen);
    29. painter.setOpacity(0.5);
    30. painter.setBrush(brush);
    31. painter.drawRect(event->rect());
    32. painter.end();
    33. }
    34.  
    35. };
    36.  
    37.  
    38. class HSGLWidget : public QGLWidget
    39. {
    40. Q_OBJECT
    41.  
    42.  
    43. public:
    44. HSGLWidget(QWidget *parent = 0);
    45.  
    46. private:
    47. CustomRB *rubberBand;
    48. QPoint origin;
    49.  
    50. protected:
    51.  
    52. void initializeGL();
    53. void resizeGL(int w, int h);
    54. //void paintGL();
    55.  
    56. void mousePressEvent(QMouseEvent *e);
    57. void mouseMoveEvent(QMouseEvent *e);
    58. void mouseReleaseEvent(QMouseEvent *e);
    59. void keyPressEvent(QKeyEvent *e);
    60. };
    61.  
    62.  
    63. #endif // HSGLWIDGET_H
    64.  
    65. #include "hsglwidget.h"
    66.  
    67. HSGLWidget::HSGLWidget(QWidget *parent)
    68. :QGLWidget(parent),rubberBand(0)
    69. {
    70. rubberBand = new CustomRB(QRubberBand::Rectangle, this);
    71.  
    72. }
    73.  
    74. void HSGLWidget::initializeGL()
    75. {
    76.  
    77. }
    78.  
    79. void HSGLWidget::resizeGL( int w, int h )
    80. {
    81.  
    82. }
    83.  
    84. void HSGLWidget::mousePressEvent( QMouseEvent *e )
    85. {
    86. origin = e->pos();
    87. rubberBand->setUpdatesEnabled(true);
    88. rubberBand->setGeometry(QRect(origin, QSize()));
    89. rubberBand->show();
    90. }
    91.  
    92. void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
    93. {
    94. rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
    95. }
    96.  
    97. void HSGLWidget::keyPressEvent( QKeyEvent *e )
    98. {
    99.  
    100. }
    101.  
    102. void HSGLWidget::mouseReleaseEvent(QMouseEvent *e)
    103. {
    104. rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
    105. rubberBand->show();
    106. }
    To copy to clipboard, switch view to plain text mode 

    I promote to Widget in qtcreator with hsglwidget so it's not working properly ...
    Last edited by nightroad; 29th March 2011 at 11:38.

  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    What happens if in the original code you posted in the firt post you do this:
    Qt Code:
    1. void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
    2. {
    3. rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
    4. update(); //add this
    5. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. The following user says thank you to high_flyer for this useful post:

    nightroad (29th March 2011)

  17. #16
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QRubberBand problem

    Quote Originally Posted by high_flyer View Post
    What happens if in the original code you posted in the firt post you do this:
    Qt Code:
    1. void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
    2. {
    3. rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
    4. update(); //add this
    5. }
    To copy to clipboard, switch view to plain text mode 
    Mate Thanks for your effort , it doesn't any effect on QGLWidget =) , anyway i already give up =.=

Similar Threads

  1. Change the color of QRubberBand
    By franco.amato in forum Qt Programming
    Replies: 14
    Last Post: 4th August 2011, 16:30
  2. How to draw QRubberBand with red color
    By lni in forum Qt Programming
    Replies: 15
    Last Post: 10th March 2010, 09:49
  3. Use of QRubberBand
    By lni in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2009, 12:59
  4. slightly more advanced QRubberBand
    By momesana in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2008, 01:00
  5. Using QRubberBand class?!?!?
    By nupul in forum Newbie
    Replies: 3
    Last Post: 1st April 2006, 17:19

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.