Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved set background image filled all elements in my QWidget?

    General and Desktop
    3
    5
    1190
    Loading More Posts
    • 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.
    • Q
      qtpi last edited by

      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?

      the_ 1 Reply Last reply Reply Quote 0
      • the_
        the_ @qtpi last edited by

        @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

        -- No support in PM --

        Q 1 Reply Last reply Reply Quote 3
        • Q
          qtpi @the_ last edited by

          @the_ it worked! thanks a lot!

          1 Reply Last reply Reply Quote 0
          • Ni.Sumi
            Ni.Sumi last edited by Ni.Sumi

            @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.

            Q 1 Reply Last reply Reply Quote 0
            • Q
              qtpi @Ni.Sumi last edited by

              @Ni.Sumi lmao. thanks anyway.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post