×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    113

    Post QTextBrowser with transparent background

    Hey guys,

    I'm trying to make a QTextBrowser's background transparent. I googled solutions to this and found that I should setAutoFillBackground(false) the viewport() of my QTextBrowser instance.

    That isn't doing anything in my case... I also tried setting the body style to 'background-color:transparent' but that won't do anything as well...

    The background of the QWidget is drawn using paintEvent.

    Am I doing something terribly wrong? Here is some of my code:

    Code:
    MyCustomWidget::MyCustomWidget(QWidget *parent) :
        QWidget(parent)
    {
    legalText = new QTextBrowser(this);
        legalText->setObjectName(QString::fromUtf8("legalText"));
        legalText->setGeometry(QRect(37, 184, 296, 344));
        legalText->setHtml(QString::fromUtf8("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
                                             "<html>"
                                             "<head>"
                                                "<meta name=\"qrichtext\" content=\"1\" />"
                                                "<style type=\"text/css\">\n"
                                                    "p, li { white-space: pre-wrap; }\n"
                                                "</style>"
                                             "</head>"
                                             "<body style='background-color:transparent'>\n"
                                                "<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
                                                    "<span style=\" font-size:6pt; color:#c7c7c7;\">"
                                                        "My text"
                                                    "</span>"
                                                "</p>"
                                             "</body>"
                                             "</html>"));
        legalText->viewport()->setAutoFillBackground(false);
    }
    
    void MyCustomWidget::paintEvent(QPaintEvent *paintEvent)
    {
        QPainter painter(this);
        if( !background.isNull() )
            painter.drawPixmap(0, 0, background);
    }
    Everything is displayed perfectly, I have a couple of labels, the background and two custom buttons and they're all fine. This is my only issue!

    Thanks a lot in advance!

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTextBrowser with transparent background

    Did you try on other platforms and/or with other styles?
    http://doc.qt.nokia.com/4.6/qapplication.html#setStyle

  3. #3
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    113

    Re: QTextBrowser with transparent background

    Quote Originally Posted by divanov View Post
    Did you try on other platforms and/or with other styles?
    http://doc.qt.nokia.com/4.6/qapplication.html#setStyle
    Hey divanov, thank you for your reply. It seems you're always there to rescue me

    After getting the list of available styles, I tried all of them, Windows, Motif, CDE, S60, Plastique and Cleanlooks. Not one of them seemed to do the trick. I also checked the setStyle returned value to see if the actual style was being set, and tried with both first letter upper case and all lower case.
    Funny thing, when setting the "S60" style, it crashed...

    I also tried several combinations of setAutoFillBackground, on the parent QWidget, its viewport, QTextBrowser and its viewport as well. I also tried setBackgroundRole(QPalette::NoRole);.

    Any ideas? I'm banging my head at this, as widgets with transparent backgrounds should be something quite common on a Smartphone!

    Best!

  4. #4
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    113

    Re: QTextBrowser with transparent background

    Is there any other Control to show multilined text anyways? Perhaps since this uses HTML it has its added complexities...

  5. #5
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    113

    Re: QTextBrowser with transparent background

    I managed to fix this using StyleSheets.

    The strange thing is that a StyleSheet with "background-color: transparent;" or "background-color: rgba(0, 0, 0, 0);" wouldn't make the background transparent. But when I tried a random gradient, voila! The background was transparent!
    So, I simply grabbed the linear black to white gradient from Qt Designer, got it from the autogenerated .h and placed in my QWidget like this:
    Code:
    my_qwidget->setStyleSheet(QString::fromUtf8("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));"));
    That seemed to do the trick! Now my background is transparent.

    The only left issue is that now I get a world of Painter not active errors or warnings in the console. Some examples:
    QPainter::begin: Paint device returned engine == 0, type: 3
    QPainter::setWorldTransform: Painter not active
    QPainter::worldTransform: Painter not active
    QPainter::setBrush: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints

    And so on, and so on.

    Since I'm still a newbie in Qt, I really can't decipher those errors. Perhaps there is something Very wrong going on, but everything seems to be working just fine on the Simulator and the actual Phone.

    Hope this quasi-solution helps anyone with my same issue, though.

    Thanks!
    Last edited by MatuX; 2010-07-23 at 07:35.

  6. #6
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTextBrowser with transparent background

    QPainter::begin() has failed for some reason. Could be that some other QPainter is already painting on the same device.
    http://doc.trolltech.com/4.6/qpainter.html#begin

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Re: QTextBrowser with transparent background

    If you want to make only your textbrowser transparent you can use QGraphicsOpacityEffect on it. But it will make it transpatent wrt to its parent window base color. So it you want a complete transparency, make the parent window also transparent, by using a setAttribute(Qt::WA_TranslucentBackground, true). Unfortunely you have to make it frameless windows to get the transparency effect.

  8. #8
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTextBrowser with transparent background

    Quote Originally Posted by srikanthsombhatla View Post
    If you want to make only your textbrowser transparent you can use QGraphicsOpacityEffect on it.
    You forgot to mention that one should be using QGraphicsView for that.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Re: QTextBrowser with transparent background

    QGraphicsEffect can be applied on any QWiget also using QWidget::setGraphicsEffect(), only requirement is that the effect should not be a stack variable.

  10. #10
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    113

    Re: QTextBrowser with transparent background

    Hey!

    I tried to do what you said, but that makes the whole Widget semi-transparent, I only want the Background of that control to be transparent.

    I'm still using my style sheet solution and the Paint errors are still there

    Thanks anyways!

  11. #11
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: QTextBrowser with transparent background

    There are a bunch of widget attributes you can set that manipulate the transparency of the widgets. They're rather arcane, and some are platform-specific, though.

    See "Transparency and Double Buffering" in the QWidget doc, and also http://doc.qt.nokia.com/4.6-snapshot...Attribute-enum .

Similar Threads

  1. Transparent background
    By santinomazza in forum Symbian
    Replies: 3
    Last Post: 2009-03-01, 18:45
  2. CEikRichTextEditor and transparent background
    By piotr_kacprzak in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-01-16, 13:04
  3. Transparent background, how?
    By doesitmatter in forum Symbian
    Replies: 1
    Last Post: 2008-01-11, 21:50
  4. Transparent background
    By nadav70 in forum Symbian User Interface
    Replies: 4
    Last Post: 2007-12-08, 23:42
  5. Background transparent
    By lilingphilip in forum Symbian User Interface
    Replies: 0
    Last Post: 2003-10-17, 10:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×