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
    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

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

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

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

  5. 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.