Results 1 to 7 of 7

Thread: Q_OBJECT and CSS background-image

  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Q_OBJECT and CSS background-image

    Hello,

    I have a problem with Q_OBJECT and background image.

    Qt Code:
    1. #ifndef MYCLASS_HH
    2. # define MYCLASS_HH
    3.  
    4. # include <QWidget>
    5.  
    6. class myclass: public QWidget
    7. {
    8.  
    9. public:
    10. myclass(QWidget* parent = 0);
    11.  
    12. Q_OBJECT
    13. };
    14.  
    15. #endif // MYCLASS_HH
    16.  
    17. #include "myclass.hh"
    18.  
    19. myclass::myclass(QWidget* parent)
    20. : QWidget(parent)
    21. {
    22. setStyleSheet("background-image: url(../QSS/background.png)");
    23. }
    To copy to clipboard, switch view to plain text mode 

    When i put in comment Q_OBJECT macro, i see my background image on my widget.
    But when Q_OBJECT macro is not commented, i don't see my background image.

    Why ?

    I have test this code on my widget :
    Qt Code:
    1. QPalette palette;
    2. QPixmap pixmap = QPixmap("../QSS/background.jpg");
    3. palette.setBrush((this)->backgroundRole(), QBrush(pixmap));
    4. (this)->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    It works, bug i don't see how i can use a CSS external file with this...

    Thanks for you help.

  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: Q_OBJECT and CSS background-image

    Q_OBJECT has nothing to do with CSS. Make sure you rebuild everything properly and change your relative path to an absolute one to make sure it is not influencing the outcome.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT and CSS background-image

    Hi wysota,

    I have rebuild everything, clear build dir for all tests.
    I have change relative path to an absolute one.

    I have test on Windows, Linux (Fedora 14 x86_64) with Qt version 4.6.3, 4.7.0 and 4.7.1.

    But i don't see my background image on my widget

  4. #4
    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: Q_OBJECT and CSS background-image

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT and CSS background-image

    Done.

    if you put in comment Q_OBJECT macro, it works.
    Attached Images Attached Images
    Attached Files Attached Files

  6. #6
    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: Q_OBJECT and CSS background-image

    If you want custom QWidget subclasses to support stylesheets, you need to provide the following code:
    Qt Code:
    1. void myclass::paintEvent(QPaintEvent *pe) {
    2. o.initFrom(this);
    3. QPainter p(this);
    4. style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
    5. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following 4 users say thank you to wysota for this useful post:

    danipga (3rd July 2013), marwyn (19th August 2011), nelbok (20th January 2011), Tottish (18th June 2011)

  8. #7
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT and CSS background-image

    Thanks a lot, it works perfectly now.

Similar Threads

  1. Image as a background
    By Tomasz in forum Newbie
    Replies: 5
    Last Post: 6th December 2010, 14:37
  2. Replies: 1
    Last Post: 25th June 2010, 18:31
  3. No Background image showed
    By jiveaxe in forum Installation and Deployment
    Replies: 2
    Last Post: 30th December 2007, 13:25
  4. No Background Image on Example
    By igor in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2007, 12:41
  5. [QT4.1.1 XP] background image
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 1st March 2006, 13:02

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.