Results 1 to 3 of 3

Thread: QWidgets and QPixmaps and Stylesheets .... oh my.

  1. #1
    Join Date
    May 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QWidgets and QPixmaps and Stylesheets .... oh my.

    using QT4.5, I have a class derived from QWidget. It uses stylesheets to loads a bitmap into the background. This stylesheet is loaded at runtime and is user made.

    Qt Code:
    1. class MyClass : public QWidget
    2. Q_OBJECT
    3. public:
    4. MyClass (QWidget *pParent ) : QWidget( pParent) {}
    5. ~MyClass () {}
    6. virtual void paintEvent(QPaintEvent *ev);
    7. };
    To copy to clipboard, switch view to plain text mode 


    ex from a stylesheet file:
    Qt Code:
    1. MyClass {
    2. background-image: url(images/background.png);
    3. }
    To copy to clipboard, switch view to plain text mode 



    in my app, I load the stylesheet as runtime using something like:
    Qt Code:
    1. QFile file( "thesheet.qss" );
    2. file.open( QFile::ReadOnly );
    3. QString styleSheet = QLatin1String( file.readAll() );
    4. qApp->setStyleSheet(styleSheet);
    To copy to clipboard, switch view to plain text mode 


    The background loads fine from the stylesheet....

    But in my MyClass paintEvent() code, I want to be able to access the QPixmap that the base QWidget class creates for the background (... but it seems that QWidget doesn't provide any accessors.).

    I know I can pre-parse the stylesheet to find the image filename, or I could use an inelegant hack and render the widget to a Painter using:
    Qt Code:
    1. QPainter_end(Painter);
    2. QPixmap_grabWidget(Widget);
    3. QPainter_begin(Painter);
    To copy to clipboard, switch view to plain text mode 


    but isn't there a simple way to access the background image?

  2. #2
    Join Date
    May 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidgets and QPixmaps and Stylesheets .... oh my.

    hmmm.... just noticed QWidget's backgroundRole() method....

  3. #3
    Join Date
    May 2009
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidgets and QPixmaps and Stylesheets .... oh my.

    for the time being I'm using the following
    little hack within my myClass code to get the background.

    Qt Code:
    1. QPalette p = this->palette();
    2. QBrush bk = palette.background();
    3. QPixmap pm = bk.texture();
    4. ...
    5. //do some mods on background pixmap, stored in "pm"
    6. ...
    7. // redraw the image in QRects defined earlier...
    8. painter.drawPixmap( qr1, pm, qr2 );
    To copy to clipboard, switch view to plain text mode 


    still not happy using an ugly hack like this when access to stylesheet defined properties should be easily accessed within the classes themselves...

Tags for this Thread

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.