set background image filled all elements in my QWidget?



  • folks, I have built a QWidget, which contains several QLabels and smaller re-painted QWidget.
    I wanted to change the background-image of the big QWidget with setStyleSheet, which results in change of all elements background image...
    I mean every QLabel and QWidget will be filled with its own background image(the image I've introduced in). how could this happen? is there is a way just fill the big QWidget background without changing the rest?



  • @qtpi

    As QLabel is also a QWidget it will get all stylesheets that apply to QWidget.

    One way is to access the QWidget by its name

    //cpp file
    QWidget *bigwidget;
    bigwidget->setObjectName("yourbigwidget");
    
    //css file
    #yourbigwidget {background-image: url(path/to/background-image); }
    

    Another way could be by a given property

    //cpp file
    QWidget *bigwidget;
    bigwidget->setProperty("fill","1");
    
    //css file
    QWidget[fill="1"] {background-image: url(path/to/background-image); }
    

    See also
    https://wiki.qt.io/Dynamic_Properties_and_Stylesheets



  • @the_ it worked! thanks a lot!



  • @qtpi
    I have the sample code as @the_ said, if you want you van have look on it. downlaod it here

    I have changed the SetObjectName in cppfile. you can do the same .ui form->rightclcik->changeObjectname.
    Then I wrote all css's in the "setstylesheet". Go to "widget.ui" , right click on it and go "ChangeStyleSheet".

    edit: you have already made it. Sorry, I commented in the same time.



  • @Ni.Sumi lmao. thanks anyway.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.