Results 1 to 10 of 10

Thread: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Hi all,
    I know there's a lot of stuff on transparency, but I haven't quite found what I'm looking for.

    I've a QLabel. I'd like the baskground to be transparent - that is, it should appear as text 'on the desktop'

    this is what I'm doing:
    Qt Code:
    1. LabelBase(QWidget *parent): QLabel(parent)
    2. {
    3. QPalette p(palette());//get the widget's default palette
    4. p.setColor ( QPalette::WindowText, Qt::darkGreen );
    5. setPalette(p);
    6. }
    7. void LabelBase::update (const QString text)
    8. {
    9. setText(text);
    10. QWidget::setFixedSize(QLabel::sizeHint());
    11.  
    12. QPixmap pixmap = QPixmap::grabWidget ( this );
    13. setPixmap(pixmap);
    14. theMask = pixmap.createHeuristicMask(false);
    15. //theMask = createMaskFromColor(Qt::darkGreen);
    16. setMask(theMask);
    17. }
    To copy to clipboard, switch view to plain text mode 
    (theMask, above, is a QRegion)
    The problem that I can't seem to make a good mask from the text. createHeuristicMask() leaves unmasked 'holes' in the text (the character 'o' is filled, for instance), and createMaskFromColor() doesn't seem to do anything for me. examples are attached.

    So, the question, how can I best make a mask for my text.

    thanks
    K
    Attached Images Attached Images

  2. #2
    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: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    You can make a transparent pixmap, draw the text on it and then mask all pixels which are transparent by transforming that pixmap to a bitmap and applying the mask. Heuristic mask is not needed here.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Ok, sounds good, but , I'm sorry but all I can manage is to make a transparent pixmap of the right size!
    Qt Code:
    1. func(QString text)
    2. {
    3. QPixmap pixmap(QPixmap::grabWidget ( this ));//just to get the right size
    4. pixmap.fill ( Qt::transparent );//ok, now I have a transparent pixmap
    5.  
    6. //draw text onto the pixmap ??? how??
    7.  
    8. QBitmap bmp(QBitmap::fromImage(pixmap.toImage()));//is this the only way from QPixmap to QBitmap?
    9.  
    10. QBitmap mask(pixmap.mask());//what mask will this give me??
    11. label->setMask(theMask);
    12. }
    To copy to clipboard, switch view to plain text mode 

    sorry, I'm a little confused

    oh, one more question: why won't pixmap.createMaskFromColor() work?

    thanks, for your help
    K

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Try this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MaskedLabel : public QLabel
    4. {
    5. protected:
    6. void resizeEvent(QResizeEvent* event)
    7. {
    8. QLabel::resizeEvent(event);
    9.  
    10. QPixmap pixmap(size());
    11. pixmap.fill(Qt::transparent);
    12. QPainter::setRedirected(this, &pixmap);
    13. QPaintEvent pe(rect());
    14. paintEvent(&pe);
    15. QPainter::restoreRedirected(this);
    16. setMask(pixmap.mask());
    17. }
    18. };
    19.  
    20. int main(int argc, char* argv[])
    21. {
    22. QApplication a(argc, argv);
    23. QLabel* label = new MaskedLabel();
    24. label->setText("Qt Centre!");
    25. QFont font = label->font();
    26. font.setPointSize(72);
    27. label->setFont(font);
    28. label->show();
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following 2 users say thank you to jpn for this useful post:

    TheKedge (6th February 2007), yurenjimi (14th January 2009)

  6. #5
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    I think you're looking for something like an "overlay widget", that is, an invisible surface/layer that you can draw onto, which composes its content straight onto the desktop.

    Afaik[*], Qt doesn't support this on any platform but Qtopia Core, but there are several approaches to "hacking" your way around it. The most common approach is to grab the desktop widget, and draw its contents onto your widget's background. Some people combine this with the Qt::WA_NoSystemBackground flag, which prevents the window system from auto-filling the widget background for every paint event (i.e., you're drawing onto what was there before). It's not a perfect solution by far, but it's what many do today.

    Using masks doesn't really give you a good result; it both looks bad (mal-aliasing edges), and the performance of a complex mask can be... pretty discouraging.

    You and all other Qt developers are waiting for the day that all platforms can support fast top-level alpha composition... When that day comes, Qt is likely to support what you want with a nice easy-to-use interface.
    [*] Disclaimer: This isn't my strongest field.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  7. The following user says thank you to Bitto for this useful post:

    TheKedge (6th February 2007)

  8. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Thanks, jpn, that's exactly it. That's what I need. (see the attachment). Bitto is right about the masks not quit working neatly, but it's what I need right now.
    fantastic.
    thanks,

    why does
    QBitmap QPixmap::createMaskFromColor ( const QColor & maskColor )
    not work - or do I misunderstand what it's supposed to do?

    K
    Attached Images Attached Images

  9. #7
    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: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Quote Originally Posted by TheKedge View Post
    why does
    QBitmap QPixmap::createMaskFromColor ( const QColor & maskColor )
    not work - or do I misunderstand what it's supposed to do?
    Antialiasing and palette optimisations may be breaking it.

  10. #8
    Join Date
    Aug 2007
    Location
    Fresno - Colombia
    Posts
    26
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Quote Originally Posted by Bitto View Post
    The most common approach is to grab the desktop widget, and draw its contents onto your widget's background. Some people combine this with the Qt::WA_NoSystemBackground flag, which prevents the window system from auto-filling the widget background for every paint event (i.e., you're drawing onto what was there before). It's not a perfect solution by far, but it's what many do today.
    Hi Bitto, how can I get this approach?
    Could you please show me a little example?

  11. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    It has already been done on Windows.

    Go and download the sources from this thread :http://www.qtcentre.org/forum/f-qt-p...highlight=argb

    regards

  12. #10
    Join Date
    Jul 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: transparency, setMask, createHeuristicMask, createMaskFromColor, QPixmap

    Quote Originally Posted by jpn View Post
    Try this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MaskedLabel : public QLabel
    4. {
    5. protected:
    6. void resizeEvent(QResizeEvent* event)
    7. {
    8. QLabel::resizeEvent(event);
    9.  
    10. QPixmap pixmap(size());
    11. pixmap.fill(Qt::transparent);
    12. QPainter::setRedirected(this, &pixmap);
    13. QPaintEvent pe(rect());
    14. paintEvent(&pe);
    15. QPainter::restoreRedirected(this);
    16. setMask(pixmap.mask());
    17. }
    18. };
    19.  
    20. int main(int argc, char* argv[])
    21. {
    22. QApplication a(argc, argv);
    23. QLabel* label = new MaskedLabel();
    24. label->setText("Qt Centre!");
    25. QFont font = label->font();
    26. font.setPointSize(72);
    27. label->setFont(font);
    28. label->show();
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 
    Hi,

    I tired this approach to create Picture In Picture(PIP) window on QWebView widget. And my goal is to create hole on QwebView so that video playing in the background is visible through PIP window.

    When I first create this Masked Label its showing me the video playing in background, all is good at this point. But when I try to change the height/width of the Masked Label using setGeometry() method of QLabel, its is not showing me transparent window. But when I only(not changing the height/widht) move it around on the screen using same setGeometry(), it just work fine.

    Any idea OR approach to achieve my goals.

    Thanks.

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.