stylsheet is not working in main widget

  • qt 5.11.1 , linux.
    i applied this stylesheet in the main widget .

    QWidget {
    background-color: #1C2836;
    color: #ffffff;

    the widget shows fine in designer.


    after build i am seeing it is only applied in the label ,buttons not in the main widgets backgrounds and SPACING .

    when add another widget in the main widget and apply stylesheet on that secind widget it works fine. like this


  • Like in List of Stylable Widgets said, QWidget only supports 3 style properties: background, background-clip, background-origin.
    You might using QPalette:

    m_myWidget = new QWidget(this);
    m_myWidget->setGeometry(0, 0, 300, 100);
    QPalette pal = palette();
    // set black background
    pal.setColor(QPalette::Background, Qt::black);


  • @nkiy

    i am not using QPalette . if i wanted to use qpalette i have do alot of work.

    i was using stylsheet for each widgets and it works fine then .but i want to set one stylesheet in main widget so it is a better and perfect way.
    but it is giving me this.

  • Lifetime Qt Champion

    Try to set
    setAutoFillBackground(true); for Wbattery
    (its a property so you can do in Designer)
    Also, did you set the layouts margins to zero- so there is no spaces ?
    alt text

    I think what you are seeing is once you use wBattery in app,
    it get more space, so layout spaces the items. Widgets have
    no background pr default so you look through and see the other window under it.

    Depending on what you need, you might also want to set its maximumsize so it stays like in Designer. Else when you insert in some other widget, it might be bigger if more space for it.
    alt text
    Only do this if goal is to stay same size even when mainwindow resizes.

  • @mrjj

    i need those spaces for looks. so i gess i have to add padding in qstylesheet to get those spaces.


  • Lifetime Qt Champion

    Well the margins for layout works just as well.
    so maybe just setAutoFillBackground look like you want.

  • @mrjj
    any solution for this post
    stylsheet is not working properly

Log in to reply