Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Background-colour to a widget - window

Background-colour to a widget - window

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 387 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arsinte_andrei
    wrote on last edited by
    #1

    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??

    1 Reply Last reply
    0
    • A Offline
      A Offline
      arsinte_andrei
      wrote on last edited by
      #2

      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??

      1 Reply Last reply
      0
      • A Offline
        A Offline
        arsinte_andrei
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved