2

My Desktop Qt app has a large stylesheet applied. It's applied for the QApplication derived class I am using:

this->ApplyStyleSheet(":/qss/default.qss");

It works well for all QWidget objects I define and use. (using *.ui files).

My problem begins when i promote one of my QWidgets in the *.ui file I'm using to one of my own QWidget derived classes.

When my widget was QWidget, the following worked and changed the background image:

QWidget#myWidget {
    background: transparent;
    background-image: url(:/images/bg_img.png);
    background-repeat: repeat-x;
}

When I promoted the element to my custom QWidget derived class and changed to:

QMyDerivedClass#myWidget {
    background: transparent;
    background-image: url(:/images/bg_img.png);
    background-repeat: repeat-x;
}

I no longer see my background image. Obviously I am missing something. What is it... I hope one of you knows.

JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • all of those tags are relevant to QWidget .. did you try leaving the CSS as is - ie use QWidget#myWidget{ ? – Abhijith Jun 30 '11 at 17:00
  • can you see background image when application runs? also which functions have you changed in derived class? give its header please – Raiv Jun 30 '11 at 17:17
  • That's my bad. didn't add "paintEvent" to my custom derived classs. – JasonGenX Jun 30 '11 at 18:27

1 Answers1

9

My bad. Posting this so that people with similar problem can find a solution:

I did not add "paintEvent" to my custom class. (which in this case draws nothing but it enables the stylesheet adherence).

void CustomWidget::paintEvent (QPaintEvent *)
{
    QStyleOption opt;
    opt.init (this);
    QPainter p (this);
    style ()->drawPrimitive (QStyle::PE_Widget, &opt, &p, this);
}
JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • 1
    For those with a similar problem to mine, the following code works perfectly if you already have a style sheet initialized to that widget. If you don't, an easy way to set it programatically is to include the following somewhere in the widgets initialization: this->setStyleSheet("QWidget {background-color:blue}"); Without that the style()->drawPrimitive command won't do anything. – Flarex Jun 06 '12 at 15:25