Qt World Summit: Submit your Presentation

Is there a way I can add extra ColorRole to QPalette?

  • The QPalette::ColorRole enum defines 20 color roles. I want to add 4 new color roles to this enum so that I can assign separate background colors to widgets based on their level in my GUI. Is there any way this can be achieved? Can I do it by subclassing QPalette?

    The best solution for me till now is to setAutoFillBackground(true) and then change the background role, e.g. setBackgroundRole(QPalette::Link) where I have already set QPalette::Link to my desired color. But this method interferes with the normal use of QPalette::Link color role which I would like to avoid. Any suggestions? I don't want to use StyleSheets, it is messy.

  • Lifetime Qt Champion


    You might be better off creating a proxy style to handle your custom colors.

    You need to do custom stuff anyway since Qt would not know about your custom QPalette color.

  • @SGaist Thanks! But doing a custom style seems like too much work for too little change. I guess I will just use QPalette::Link and a few others to implement my colours. I hope it won't create any problems.

  • Lifetime Qt Champion

    You do not a full custom QStyle, there's QProxyStyle for lighter weight customization.

Log in to reply