Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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");
    	file.open(QFile::ReadOnly);
    	QString styleSheet = QLatin1String(file.readAll());
    	this->setStyleSheet(styleSheet);
    
    	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;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    
     QWidget::paintEvent(event);
    }
    

    I do not take credit for this.. here is the coplete ansewr
    https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget


Log in to reply