Qt World Summit: Submit your Presentation

Background-colour to a widget - window

  • So.. basically I'm trying to add a background colour to a window that is a subclass to a widget which itself is a child of the main window (this is also a subclass from the widget)

    border-color: rgb(127, 255, 80);
    background-color: rgb(124, 124, 255);

    I've put this in Qt designer - witch is shown exactly how I want it... but when I run is not working.. I get the normal widget colour - the background colour is applied only to the children of the Widget and not to the widget itself - I 've put a Qpush button there to test it...

    I've tried with qrc - resource file and put the stylesheet.qss file there.. but without any success at the moment...

    QFile file(":/atpsettings.qss");
    	QString styleSheet = QLatin1String(file.readAll());
    	qDebug() <<this << styleSheet;

    and yes on Qdebug I see the whole file.. is reading it - again the settings affect only the children of the widget, not the widget itself - this is what I'm after

    So the big questin.. how can I change the background colour of the widget??

  • if in the main window I start the child like that (which I do not want to)

    widgetSettings = new atpWidgetSettings();

    then I get a second window with everything as I want it to be
    but if I start it like

    widgetSettings = new atpWidgetSettings(this);

    then all my stylesheets and everything is going away... any help how to overcome this and not to inhered the styles from the parent?? it seems just a parent problem....hm how can I overcome it??

  • ok.. this is how you can sort it out in order to have any widget with custom colours...

    void atpWidgetSettings::paintEvent(QPaintEvent* event) {
     QStyleOption opt;
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    I do not take credit for this.. here is the coplete ansewr

Log in to reply