Results 1 to 3 of 3

Thread: Transparent background can't erase the old when using qpainter in paintevent

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Exclamation Transparent background can't erase the old when using qpainter in paintevent

    I use QT4.8 in embeded linux arm platform

    the below layer of /dev/fb0 is video,So I want the whole QT program is transparent!

    when I update the OSD(developed by QT) every second with different content, I found the new content was stacks with the old.
    the old can't be erased auto even by hand if the background or the fill color is transparent!
    And hide() and show() doesn't work in the same time,It will show always except not update the content when hide().
    but if I set background opaque, it will ok!

    How can I make the old content transparecy or erase them.

    thanks, I really want your help!!!

    below is the code:

    Window::Window(): QWidget()
    {
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
    setAttribute(Qt::WA_NoSystemBackground);
    setFixedSize(100, 100);
    textPen = QPen(Qt::red);
    textFont.setPixelSize(40);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(animate()));
    timer->start(1000);
    }
    void Window::animate()
    {
    strTime = (QTime::currentTime()).toString(tr("hh:mm:ss"));
    update();
    }
    void Window::paintEvent(QPaintEvent *event)
    {
    QPainter painter;
    painter.begin(this);
    painter.setBackgroundMode(Qt::TransparentMode);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(textPen);
    painter.setFont(textFont);
    painter.fillRect(event->rect(), QColor(0,0,0,0));
    painter.drawText(QRect(0, 0, 100, 100), Qt::AlignCenter, strTime);
    painter.end();
    }
    int main(int argc, char *argv[])
    {
    QWSServer::setBackground(QBrush(QColor(0,0,0,0)));
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
    }
    Last edited by ipx; 8th April 2013 at 08:28.

  2. #2
    Join Date
    Apr 2013
    Posts
    2

    Default Re: Transparent background can't erase the old when using qpainter in paintevent

    who can help me? God!

  3. #3
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Transparent background can't erase the old when using qpainter in paintevent

    Before calling
    Qt Code:
    1. painter.fillRect(event->rect(), QColor(0,0,0,0));
    To copy to clipboard, switch view to plain text mode 
    Set painter composition mode to Source. This will disable alpha blending for subsequent draw operations. Don't forget to reset it back to SourceOver though.
    Qt Code:
    1. painter.setCompositionMode (QPainter::CompositionMode_Source);
    2. painter.fillRect(event->rect(), Qt::transparent);
    3. painter.setCompositionMode (QPainter::CompositionMode_SourceOver);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Using QPainter to 'erase' an area of widget?
    By altec42lansing in forum Qt Programming
    Replies: 13
    Last Post: 17th June 2011, 16:19
  2. Paintevent and transparent children
    By wishper in forum Newbie
    Replies: 2
    Last Post: 18th August 2010, 14:17
  3. Replies: 1
    Last Post: 25th June 2010, 18:31
  4. Transparent background on QLabel on transparent QWidget
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2008, 02:10
  5. Replies: 12
    Last Post: 5th February 2006, 10:34

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.