Results 1 to 16 of 16

Thread: How to draw QRubberBand with red color

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to draw QRubberBand with red color

    Hi,

    I am using QRubberBand to zoom the scene when user clicks and drags a rectangle area. How can I set the QRubberBand to different color, such as red?

    Thanks

  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw QRubberBand with red color

    Never mind, soloved!

  3. #3
    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: How to draw QRubberBand with red color

    Can you post the solution too

  4. #4
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw QRubberBand with red color

    Quote Originally Posted by aamer4yu View Post
    Can you post the solution too
    You won't like it. Codes were developed in Linux, and after moved to Windows, I got rubber band filled with red, so now I have to use #ifdef... too bad...

    Qt Code:
    1. class LinuxRubberBand : public QRubberBand
    2. {
    3. public:
    4.  
    5. LinuxRubberBand( Shape s, QWidget * p = 0 ) : QRubberBand( s, p ) {
    6. QPalette palette;
    7. palette.setBrush( QPalette::WindowText, QBrush( Qt::red ) );
    8. setPalette(palette);
    9. }
    10.  
    11. protected:
    12.  
    13. virtual void paintEvent( QPaintEvent * ) {
    14. QStylePainter painter(this);
    15.  
    16. option.initFrom( this );
    17.  
    18. painter.drawControl(QStyle::CE_FocusFrame, option);
    19. }
    20.  
    21. };
    22.  
    23. QRubberBand* createRubberBand()
    24. {
    25. QRubberBand* rubberBand( 0 );
    26.  
    27. #ifdef Q_WS_WIN
    28. rubberBand = new QRubberBand( QRubberBand::Rectangle, 0 ) ),
    29. #else
    30. rubberBand = new LinuxRubberBand( QRubberBand::Rectangle, 0 ) ),
    31. #endif
    32.  
    33. return rubberBand;
    34. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: How to draw QRubberBand with red color

    One question ...
    why did you inherit ? you could have very well set the palette on the object of QRubberBand, isnt it ?

  6. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to draw QRubberBand with red color

    Quote Originally Posted by aamer4yu View Post
    One question ...
    why did you inherit ? you could have very well set the palette on the object of QRubberBand, isnt it ?
    I did that but it didn't work...

    I switch to QStyleOptionFocusRect and it works (on Linux)...

  7. #7
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to draw QRubberBand with red color

    Hi,

    As aamer pointed out, is there a way to draw QRubberBand object with another color without inheriting the QRubberBand?

    I've tried to solve it using style sheet and palette but neither of them worked.

  8. #8
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to draw QRubberBand with red color

    Hi,


    Regarding the solution posted above, is it possible to make the rubberband dashed line also by using QPalette or something?

  9. #9
    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: How to draw QRubberBand with red color

    You may try subclassing QRubberBand and setting palette while drawing.

  10. #10
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to draw QRubberBand with red color

    Hi,

    That's what I exactly did..but I couldn't find to how to set "dashed line" for QPalette.

  11. #11
    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: How to draw QRubberBand with red color


  12. #12
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to draw QRubberBand with red color

    I did it as well in the paintEvent method of my rubberband class but it didn't make any difference. I guess QPalette overwrites QPainter or something.

  13. #13
    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: How to draw QRubberBand with red color

    May be if you show some code, or give compilable code we can see whats wrong

  14. #14
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to draw QRubberBand with red color

    I love to..

    Qt Code:
    1. class CustomRubberBand : QRubberBand
    2. {
    3.  
    4.  
    5. CustomRubberBand( Shape s) : QRubberBand (s)
    6. {
    7. QPalette palette;
    8. palette.setBrush(QPalette::WindowText, QBrush(Qt::red));
    9. setPalette(palette);
    10.  
    11. repaint();
    12. }
    13.  
    14. void paintEvent(QPaintEvent *)
    15. {
    16. QStylePainter painter(this);
    17.  
    18.  
    19. option.initFrom(this);
    20.  
    21. painter.begin(this);
    22.  
    23. QPen pen;
    24. pen.setStyle(Qt::DashLine);
    25. pen.setWidth(2);
    26. pen.setColor(QColor(Qt::red));
    27. painter.setPen(pen);
    28.  
    29. painter.drawControl(QStyle::CE_FocusFrame, option);
    30. }
    31.  
    32. friend class MyGraphView;
    33.  
    34. };
    To copy to clipboard, switch view to plain text mode 

  15. #15
    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: How to draw QRubberBand with red color

    What if you simply draw a rectangle painter.drawRect(...)
    instead of the painter.drawControl(QStyle::CE_FocusFrame, option);

    ??

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

    zgulser (10th March 2010)

  17. #16
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to draw QRubberBand with red color

    Simply worked beautifully. Thanks

Similar Threads

  1. Using a QFrame as a color selection indicator
    By KShots in forum Qt Tools
    Replies: 8
    Last Post: 14th June 2011, 23:55
  2. Replies: 12
    Last Post: 25th December 2009, 15:07
  3. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 07:14
  4. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 15:02
  5. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22: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.