Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set stylesheet on widgets containing a given string?
Qt 6.12 LTS is out! See what's new in the release blog

How to set stylesheet on widgets containing a given string?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 578 Views 2 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.
  • D Offline
    D Offline
    Daniella
    wrote on last edited by
    #1

    Is it possible on qt designer to apply the stylesheet only on widgets containing a given string?
    Something like

    [objectName*="test_"] 
    {
        background-color: red;
    }
    

    It would set the bg red on any widget containing test_ on its object name.

    JonBJ 1 Reply Last reply
    0
    • D Daniella

      Is it possible on qt designer to apply the stylesheet only on widgets containing a given string?
      Something like

      [objectName*="test_"] 
      {
          background-color: red;
      }
      

      It would set the bg red on any widget containing test_ on its object name.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Daniella
      Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types

      Instead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.

      D 1 Reply Last reply
      0
      • JonBJ JonB

        @Daniella
        Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types

        Instead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.

        D Offline
        D Offline
        Daniella
        wrote on last edited by
        #3

        @JonB said in How to set stylesheet on widgets containing a given string?:

        Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types

        Instead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.

        When i have a QWidget inside another QWidget the padding doesnt work, im doing something wrong?

        QWidget[test="true"]
        {
        	background-color: #3c3f45;
        	padding-left: 32px;
        }
        
        Axel SpoerlA 1 Reply Last reply
        0
        • D Daniella

          @JonB said in How to set stylesheet on widgets containing a given string?:

          Your only chance would be by using dynamic properties, but I don't think that will allow objectName as a partial match, no "containing" a substring. The only mention of "syntax" is https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types

          Instead of =, you can also use ~= to test whether a Qt property of type QStringList contains a given QString.

          When i have a QWidget inside another QWidget the padding doesnt work, im doing something wrong?

          QWidget[test="true"]
          {
          	background-color: #3c3f45;
          	padding-left: 32px;
          }
          
          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          @Daniella
          Things can go wrong on parenting the widgets into each other. The child may inherit its parent’s stylesheet. In that case the parent probably has the same background color. So the only noticeable difference is padding.

          It also matters if test becomes true before or after the setting the stylesheet. If it happens afterwards, you have to enforce manual recomputing.

          Whatever it is, setting the child’s stylesheet (again), should solve both issues.

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          0

          • Login

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