June 30, 2011

ronM71 ronM71
Lab Rat
225 posts

Qt Stylesheet mystery

 

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

  1. 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:

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

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

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

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

3 replies

June 30, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

I’ve seen this before with promoting widgets.

The only solution i could find is to do al stylesheet stuff in code not in Qt Designer.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 30, 2011

peppe peppe
Ant Farmer
1025 posts

It’s a FAQ. Read the entry for QWidget in here: stylesheet-reference


If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
  1.  void CustomWidget::paintEvent(QPaintEvent *)
  2.  {
  3.      QStyleOption opt;
  4.      opt.init(this);
  5.      QPainter p(this);
  6.      style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  7.  }

The above code is a no-operation if there is no stylesheet set.

Warning: Make sure you define the Q_OBJECT macro for your custom widget.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

June 30, 2011

ronM71 ronM71
Lab Rat
225 posts

that did it. my bad. thanks.

 
  ‹‹ QString::sprintf question      How to use Qt with Mercurial and Google Code? ››

You must log in to post a reply. Not a member yet? Register here!