Empty style sheet and *.ui
-
wrote on 24 Sept 2011, 06:09 last edited by
Some of the windows of my application must have a header widget. And this widget's style sheet is constant.
I'm trying to accomplish this by creating the class named HeaderWidget, adding a form to it (single widget) and setting style sheet on it.
Then i redefine paintEvent to draw style sheet.
Next i create widgets on all relevant windows and promote them to HeaderWidget. And it works fine.When i change promoted widget's style sheet it, of course, redefines HeaderWidget's style sheet. But! When i erasing promoted widget's style sheet back to empty uic.exe doesn't just remove the "widget->setStyleSheet("...");" line but replaces "..." with "". And thus still redefines HeaderWidget's style sheet.
Can it be considered as a bug? Or what should i do to accomplish this task? BTW, i really don't want to create custom widget plugin for Qt Creator since.
-
wrote on 24 Sept 2011, 06:45 last edited by
And additional question.
When i add a style sheet like this:
@w->setStyleSheet( "background-color: red;" );@
not only invoking widget w will have red background but also every child of it.
Can i somehow mark lines in the style sheet that they are relevant only to widget which invokes setStyleSheet() (so only w will have red background, no matter what object name it has)? -
wrote on 24 Sept 2011, 07:27 last edited by
The ugliest solution ever.
@HeaderWidget::HeaderWidget( QWidget* parent ) :
QWidget( parent ),
ui( new Ui::HeaderWidget )
{
ui->setupUi( this );
}void HeaderWidget::setStyleSheet( const QString& styleSheet )
{
static QString ss;
static QString defaultSS;
if( defaultSS.isEmpty() )
{
HeaderWidget tmp;
ui->setupUi( &tmp );
defaultSS = tmp.styleSheet();
}
ss = styleSheet.isEmpty() ? defaultSS : styleSheet;
ss.replace( "HeaderWidget", objectName() );
QWidget::setStyleSheet( ss );
}void HeaderWidget::setObjectName( const QString& objectName )
{
YB_DEBUG_FUNC_BEGIN;
QWidget::setObjectName( objectName );
setStyleSheet( "" );
}@I have a feeling that i will burn in hell for this.
-
wrote on 24 Sept 2011, 08:29 last edited by
bq. not only invoking widget w will have red background but also every child of it.
Have a look at the" stylesheet Reference and examples":http://doc.qt.nokia.com/4.7/stylesheet-examples.html. You can even use the name of your widget to fine tune it.
-
wrote on 24 Sept 2011, 08:42 last edited by
If you mean this:
@HeaderWidget
{
background-color: red;
}@
This doesn't work.
And if by "name of your widget" you mean objectName() this won't work too since this problem appers not only for headers (which are, of course, single on every window) but for example for address boxes too (and window can contain many of them). -
wrote on 24 Sept 2011, 09:04 last edited by
[quote author="p-himik" date="1316849264"]The ugliest solution ever.
...
I have a feeling that i will burn in hell for this.[/quote]You will, because neither QObject::setObjectName() nor QWidget::setStyleSheet() is declared virtual, which means that both method implementations will be skipped when called through base class pointers.
[quote author="p-himik" date="1316853767"]If you mean this:
@HeaderWidget
{
background-color: red;
}@
This doesn't work.
[/quote]
But it should.
[quote author="p-himik" date="1316846718"]Can i somehow mark lines in the style sheet that they are relevant only to widget which invokes setStyleSheet() (so only w will have red background, no matter what object name it has)?[/quote]No, but you can use a "property selector":http://doc.qt.nokia.com/latest/stylesheet-syntax.html#selector-types to select single widget objects of the same type.
@
HeaderWidget
{
...
}
HeaderWidget[type="redBackgroundType"]
{
background-color: red;
}
HeaderWidget* widget = new HeaderWidget;
widget->setProperty("type", "redBackgroundType");
@ -
wrote on 24 Sept 2011, 09:06 last edited by
Can you show us a bitmap of what you want to accomplish. Eg. A screen shot of your ui file?
I don't understand why you are subclassing QWidget and not one of it's descendants that looks like what you want.
-
wrote on 24 Sept 2011, 09:27 last edited by
bq. You will, because neither QObject::setObjectName() nor QWidget::setStyleSheet() is declared virtual, which means that both method implementations will be skipped when called through base class pointers.
I know about absence of "virtual" modifier but these functions won't ever be called through base class pointers (even in ui_*.h file).
bq. But it should.
Here is a picture.
!http://dl.dropbox.com/u/26596688/notworking.JPG(useless style sheet for header widget)!Eddy, all i want is ability to create multiple widgets of the same my class on a single window. And style sheet for every widget must be defined globally (my previous approach was to just copy style sheet over and over. But that's sucks). The reason why i don't want to create Qt Designer's plugin is that appearance of my widget could be changed a few times per day.
Wherever i write style sheet for my widget it'll be overwritten by parent's style sheet with ui->setupUi( this ) call.
And i subclass QWidget because there is no other widget that looks the way i need (already mentioned address box. Separate fields for region, state, city, etc. And style sheet different from the rest of the widgets).bq. No, but you can use a property selector [doc.qt.nokia.com] to select single widget objects of the same type.
I'll try in a few minutes.
-
wrote on 24 Sept 2011, 10:01 last edited by
Forgot to mention - there is not only a window with multiple widget. There is multiple windows. So this style sheet have to be written in a place somehow related to HeaderWidget (i can assign it to QApplication, but it removes a great ability of WYSIWYG-like Qt Designer to show you changes as they are made).
[quote author="Lukas Geyer" date="1316855095"]No, but you can use a "property selector":http://doc.qt.nokia.com/latest/stylesheet-syntax.html#selector-types to select single widget objects of the same type.[/quote]
Tried. Didn't work either which is logically since HeaderWidget's style sheet is overwritten by parent widget's .ui file independently of what properties i used.Again, overwriting with empty style sheet happens only when i press OK button in style sheet editor for widget promoted to HeaderWidget. If i don't touch anything (just place widget, adjust it's size and geometry and promote it to HeaderWidget) style sheet won't be overwritten and everything is OK.
Maybe it's worth to add a few lines to Qt Designer's sources so that it will remove "< property name="styleSheet">" completely from .ui file if there is no style sheet? Now it just places
@
< property name="styleSheet">
<string notr="true"/>
</property>@
there thus overwriting any style sheet provided by HeaderWidget.And why reply editor removes "< property name="styleSheet">" when there is no space after left bracket?
-
wrote on 24 Sept 2011, 10:10 last edited by
I think we have come at a stadium that it is better to make a small compilable example and show us the code. This can help to fine tune the problem.
-
wrote on 24 Sept 2011, 10:49 last edited by
-
wrote on 24 Sept 2011, 12:42 last edited by
I tried your code out and it works if you use it in main on the qapplication like this :
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyleSheet("CommonWidget { ......
MainWindow w;
w.show();return a.exec();
}@
I omitted the @,*[class="CommonWidget"] @
Now throughout your application if there is a commonWidget it will be styled using this stylesheet.
-
wrote on 24 Sept 2011, 14:56 last edited by
Eddy, thank you for your answer but i've written:
[quote author="p-himik" date="1316858503"]... (i can assign it to QApplication, but it removes a great ability of WYSIWYG-like Qt Designer to show you changes as they are made).[/quote]Of course if there is no other way to do it, i'll do it this way.
And i still sure that Qt Designer must not insert “< property name=“styleSheet”>” tag into the .ui file when there is no any style sheet set. -
wrote on 24 Sept 2011, 15:49 last edited by
Sorry i didn't see that.
Afaik stylesheets are not used the way you want. The power of stylesheets is exactly that you can give the whole of your application the same look in one place.
1/14