Results 1 to 14 of 14

Thread: Using QPainter to 'erase' an area of widget?

  1. #1
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Using QPainter to 'erase' an area of widget?

    I have a qpainter painting a certain image over an entire area. Now, i want to erase part of it completely so i can basically see the desktop behind it kind of thing. i am currently using:

    painter.eraseRect(0, 0, width()/2, height()/2)

    and it just makes a white background in that part of the ui. I have tried looking around to forums on setbackground or setattribute:

    setBackgroundRole(QPalette::Base);
    setAttribute(Qt::WA_NoSystemBackground);

    but these seem to have no effect, and I can't seem to get that white 'erased' area to be completely gone or transparent. Might be a simple fix, any ideas?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using QPainter to 'erase' an area of widget?

    I would suggest to not paint at all, then no need to erase it. I mean don't paint the area which you want to erase. I hope you got it

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QPainter to 'erase' an area of widget?

    Qt::WA_TranslucentBackground
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QPainter to 'erase' an area of widget?

    hmm, still cant seem to get this working, basically i have a screen that i draw, and i need to get ride of a portion of it completely, including that 'white box' that is left behind when i erase rect. I basically want to get ride of that white area and any remnants left over after erasing, so i can see whats behind the application, since my widgets are painted over top of everything else.
    (here is my current code for erasing it

    painter.eraseRect(0,0,width()/2,height()/2);
    this->setBackgroundRole(QPalette::Base);
    this->setAttribute(Qt::WA_TranslucentBackground);

  5. #5
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Using QPainter to 'erase' an area of widget?

    Try to test this:

    Qt Code:
    1. #include <QtGui>
    2. #include <QLabel>
    3.  
    4. //! [main function]
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QLabel widget("TEST");
    10. QFont f = widget.font();
    11. f.setPointSize(100);
    12. widget.setFont(f);
    13. widget.setWindowFlags(Qt::WindowFlags(Qt::FramelessWindowHint));
    14. widget.setAttribute(Qt::WA_TranslucentBackground);
    15. widget.show();
    16.  
    17. return app.exec();
    18. }
    19. //! [main function]
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QPainter to 'erase' an area of widget?

    this worked and painted text with a transparent background over my windows, allowing me to see behind it where the text was not, but im not sure how to apply this to my problem

  7. #7
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Using QPainter to 'erase' an area of widget?

    Does this help? Use your widget as I have used MyWidget in code below. You don't have to erease anything in your paintEvent

    Qt Code:
    1. #include <QtGui>
    2. #include <QWidget>
    3.  
    4. class MyWidget : public QWidget
    5. {
    6. public:
    7. explicit MyWidget(QWidget *parent = 0):QWidget(parent){};
    8.  
    9. protected:
    10. void paintEvent ( QPaintEvent * );
    11.  
    12. };
    13.  
    14. void MyWidget::paintEvent ( QPaintEvent * )
    15. {
    16. QPainter p(this);
    17. p.setPen(QPen(Qt::red,5));
    18.  
    19. p.drawRect(rect());
    20. }
    21.  
    22. //! [main function]
    23. int main(int argc, char *argv[])
    24. {
    25. QApplication app(argc, argv);
    26.  
    27. MyWidget widget;
    28. widget.setFixedSize(300,200);
    29. widget.setWindowFlags(Qt::WindowFlags(Qt::FramelessWindowHint));
    30. widget.setAttribute(Qt::WA_TranslucentBackground);
    31. widget.show();
    32.  
    33. return app.exec();
    34. }
    35. //! [main function]
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QPainter to 'erase' an area of widget?

    I hate attached a small file which hopefully demonstrates what i am trying to do
    Attached Images Attached Images

  9. #9
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Using QPainter to 'erase' an area of widget?

    Qt Code:
    1. void MyWidget::paintEvent ( QPaintEvent * )
    2. {
    3. QPainter p(this);
    4. p.setPen(QPen(Qt::red,5));
    5.  
    6. p.fillRect(rect(), Qt::white);
    7. p.drawText(0,0,width(), height(),Qt::AlignCenter || Qt::TextWordWrap, "THIS IS THE AREA WHERE I DRAW IMG");
    8.  
    9. p.setCompositionMode(QPainter::CompositionMode_Source);
    10. p.fillRect(rect().adjusted(0,0,-width()/2,-height()/2), Qt::transparent);
    11. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to Rachol for this useful post:

    altec42lansing (17th June 2011)

  11. #10
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QPainter to 'erase' an area of widget?

    awesome, thank you!


    Added after 56 minutes:


    One more problem i am having though, now that i can see whatever is behind it on the desktop, i do not want to be able to click on that area, otherwise i'd lose focus of my current application, i just want to be able to see it, like a 'window' in the middle of my app. i tried changing the mode and redrawing a transparent rect on top but that didnt work:

    painter.setCompositionMode(QPainter::CompositionMo de_SourceIn);
    painter.fillRect(pipRect, Qt::transparent);
    painter.setCompositionMode(QPainter::CompositionMo de_DestinationAtop);
    painter.fillRect(pipRect, Qt::transparent);
    Last edited by altec42lansing; 17th June 2011 at 15:21.

  12. #11
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Using QPainter to 'erase' an area of widget?

    I don't know anything else but something like this:

    Qt Code:
    1. void MyWidget::paintEvent ( QPaintEvent * )
    2. {
    3. QPainter p(this);
    4. p.setPen(QPen(Qt::red,5));
    5.  
    6. p.fillRect(rect(), Qt::white);
    7. p.drawText(0,0,width(), height(),Qt::AlignCenter || Qt::TextWordWrap, "THIS IS THE AREA WHERE I DRAW IMG");
    8.  
    9. p.setCompositionMode(QPainter::CompositionMode_Source);
    10. p.fillRect(rect().adjusted(0,0,-width()/2,-height()/2), QColor(255,255,255,1));
    11. }
    To copy to clipboard, switch view to plain text mode 

    instead of Qt:transparent use something with low alfa value, so the user can't even notice that there is something. QColor(255,255,255,1) works well for such purpose.

  13. The following user says thank you to Rachol for this useful post:

    altec42lansing (17th June 2011)

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QPainter to 'erase' an area of widget?

    Maybe it will easier for you if you use QPixmap::grabWindow() or QPixmap::grabWidget() on your desktop and simply set that as the background of your widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #13
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Using QPainter to 'erase' an area of widget?

    Well, as long as nothing in the background changes. Also positioning of such pixmap gets complicated.

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QPainter to 'erase' an area of widget?

    You can't eat the cake and still have the cake.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. The following user says thank you to wysota for this useful post:

    d_stranz (18th June 2011)

Similar Threads

  1. How to get an area of the widget transparent...???
    By kapoorsudhish in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 5th March 2010, 04:20
  2. How to erase everything widget has painted?
    By TheNewGuy in forum Newbie
    Replies: 1
    Last Post: 12th December 2009, 07:23
  3. Replies: 19
    Last Post: 26th November 2008, 19:54
  4. update(), aber not erase the the widget's area?
    By blm in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2008, 15:26
  5. QRubberBand painting in the scroll area widget
    By SkripT in forum Qt Programming
    Replies: 7
    Last Post: 17th January 2006, 16:48

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.