Results 1 to 14 of 14

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 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.


  2. #2
    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);

  3. #3
    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 

  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?

    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

  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?

    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 

  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?

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

  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?

    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 

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

    altec42lansing (17th 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
  •  
Qt is a trademark of The Qt Company.