Results 1 to 16 of 16

Thread: QRubberBand make background invisible

  1. #1

    Default QRubberBand make background invisible

    I'm trying to modify the painter method of QRubberBand to fit my taste of style, and for the most part it works just like it should, but I have tried to get the background to be invisible, but I have found no method that I had any luck with.

    With no painting method for the background it is a transparent white.

    Qt Code:
    1. class CustomRubberBand : public QRubberBand
    2. {
    3. public:
    4. CustomRubberBand(Shape s, QWidget * p = 0) : QRubberBand(s, p)
    5. {
    6. }
    7.  
    8. protected:
    9. void paintEvent(QPaintEvent *pe)
    10. {
    11. Q_UNUSED(pe);
    12.  
    13. QPainter painter;
    14. QPen pen(Qt::red, 6);
    15. pen.setStyle(Qt::SolidLine);
    16.  
    17. painter.begin(this);
    18. painter.setPen(pen);
    19. painter.drawRect(pe->rect());
    20. painter.end();
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 



    I've attached an example where you would be able to see what I'm talking about.
    Does anyone know how to make it invisible?

  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 make background invisible

    If what you want is a non transparent rubber band, I think setting autoFillBackground will do the trick.
    ==========================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
    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: QRubberBand make background invisible

    Did you try setting brush to transparent ?
    painter.setBrush(Qt::transparent)

  4. #4

    Default Re: QRubberBand make background invisible

    @high_flyer
    I want the background to be completely invisible and the borders to fully visible. Tried autoFillBackground, but it doesn't seem to have any effect at all.

    @aamer4yu
    Just tried and it have no effect at all. No matter what I do the background is always visible.

    Even if I do this, which have an empty paint method, it still gets drawn. I get a white semi-transparent background with no border, while the default is a blue semi-transparent background and a border.
    Qt Code:
    1. class CustomRubberBand : public QRubberBand
    2. {
    3. public:
    4. CustomRubberBand(Shape s, QRubberBand * p = 0) : QRubberBand(s, p)
    5. {
    6. }
    7. protected:
    8. void paintEvent(QPaintEvent *pe)
    9. {
    10. Q_UNUSED(pe);
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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 make background invisible

    What happens if in the original code you posted you set a brush (non transparent)?

    EDIT:
    By default a rectangular rubber band (s is Rectangle) will use a mask, so that a small border of the rectangle is all that is visible. Some styles (e.g., native Mac OS X) will change this and call QWidget::setWindowOpacity() to make a semi-transparent filled selection rectangle.
    You might want try that.
    ==========================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.

  6. #6

    Default Re: QRubberBand make background invisible

    It will turn green, the background, if I set the brush as following:
    Qt Code:
    1. painter.setBrush(QBrush(QColor(0, 255, 0, 255)));
    To copy to clipboard, switch view to plain text mode 

    So I tried doing this in the hope of getting rid of the background color, but it is still white:
    Qt Code:
    1. painter.setBrush(QBrush(QColor(0, 0, 0, 0)));
    To copy to clipboard, switch view to plain text mode 

    And I just noticed that the border actually gets the same amount of transparency as the background. My plan was to have a transparent background and fully visible borders.

  7. #7
    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 make background invisible

    try:
    Qt Code:
    1. painter.setBrush(QBrush(QColor(0, 0, 0, 255)));
    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.

  8. #8

    Default Re: QRubberBand make background invisible

    Qt Code:
    1. painter.setBrush(QBrush(QColor(0, 0, 0, 255)));
    To copy to clipboard, switch view to plain text mode 
    That makes it black instead of white.

    Qt Code:
    1. CustomRubberBand *selection = new CustomRubberBand(QRubberBand::Rectangle, 0);
    2. selection->setGeometry(150,150,250,250);
    3. selection->show();
    To copy to clipboard, switch view to plain text mode 
    There is no effect with "selection->setWindowOpacity(0.0);" at all.

  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 make background invisible

    That makes it black instead of white.
    Correct - I thought that this is what you wanted?!

    I am confused - if you don't want it opaque, and not transparent, what do you want then?
    ==========================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

    Default Re: QRubberBand make background invisible

    I want the background to be completely transparent, not semi, which is what it is stuck at right now. It should be a transparent box with red none transparent borders.

  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 make background invisible

    I see...
    and what does a brush with any other color than black and full transparency do?
    for example:
    Qt Code:
    1. painter.setBrush(QBrush(QColor(0, 255, 0, 0)));
    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.

  12. #12

    Default Re: QRubberBand make background invisible

    They do nothing, the white semi-transparent background is still there

  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 make background invisible

    it seems to be the semi transparent part is a child of the rubber band.
    This would explain why an empty paintEvent() still draws it like that.
    Try getting the children list, see if its empty, and if not, set the transparency on the children.
    Better yet - look at the QRubberBand source.
    ==========================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

    Default Re: QRubberBand make background invisible

    Sorry, didn't see that last part, my mistake.

    How would I iterate through the QRubberBand's children?

    I'll give the source a try.

    If this won't result in a positive result I'll just use four QWidgets to make a box instead.

  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 make background invisible

    How would I iterate through the QRubberBand's children?
    http://doc.trolltech.com/4.7/qobject.html#children

    If this won't result in a positive result I'll just use four QWidgets to make a box instead.
    Wouldn't a one QFrame be better?
    ==========================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. #16

    Default Re: QRubberBand make background invisible

    Thanks, I'll try it at some point in the new year.

    I made the function, for testing purpose, and used QWidgets without give a thought to QFrame, but indeed they are better suited for the task. For now this is the method I will use, since it is much simpler to use.

    Thanks for your time.

Similar Threads

  1. Custom QRubberBand with transparent background
    By mdomke in forum Qt Programming
    Replies: 1
    Last Post: 11th June 2010, 11:34
  2. make label of QwtPlotMarker invisible
    By rambo83 in forum Qwt
    Replies: 1
    Last Post: 26th November 2009, 13:39
  3. Replies: 1
    Last Post: 30th October 2009, 21:02
  4. make background invisible
    By punit.doshi.85 in forum Qt Programming
    Replies: 1
    Last Post: 1st October 2008, 11:48
  5. Replies: 4
    Last Post: 23rd June 2008, 18:11

Tags for this Thread

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.