Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Help] : How to style sub-control for inherited classes?
Forum Updated to NodeBB v4.3 + New Features

[Help] : How to style sub-control for inherited classes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 345 Views
  • 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.
  • B Offline
    B Offline
    Broot7
    wrote on last edited by Broot7
    #1

    Hey there, hope all's well - I couldn't find any obvious online so I'm seeking help here.

    I implemented a subclass for QTabBar because I needed to override the tabSizeHint function.

    Unfortunately after implementing the new class the style sheet, my style sheet no longer works for sub-controls, the style for custom-class works however. I'd like to mention I also have both Q_OBJECT macro and have overridden the QWidget::PaintEvent in my class.

    Appreciate any help - thanks in advance :].

    // This works!
    NodeViewTab { // My custom class inheriting QTabBar
        background-color: #353535;
        border: 0;
        padding: 0;
        margin: 0;
        outline: 0;
        border-bottom: 2 solid #505050;
    
    }
    
    // This does NOT!
    NodeViewTab::tab, QTabBar::tab {
        background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                    stop: 0 #454545, stop: 1.0 #353535);
        color: #c0c0c0;
        min-width: 8ex;
        padding: 2px;
        max-width: 80ex;
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      When you paint yourself the stylesheet can not be in effect. What do you do in the paint event?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Broot7
        wrote on last edited by Broot7
        #3

        hey Christian - I've looked at other topics and they said it's required to override the paintEvent in order for QWidget sub-classes to use style sheets. And in my experience this has proven true - as without it the stylesheet wouldn't work to begin with.

        void MyQWidget::paintEvent(QPaintEvent* e){
            QStyleOption opt;
            opt.initFrom(this);
            QPainter p(this);
            style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
        }
        

        HOWEVER, I realized I didn't actually need a subclass for QTabBar since there was a func called setExpanding that I needed to set to false. I just deleted my subclass and I'm using the QTabBar style sheets and.. I guess things just work as I want it to for now.

        Having said that, do you know what would be the proper method to set the style of subcontrols for custom classes?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So you call the painting stuff and the style sheet can apply. But you don't style MyWidget but NodeViewTab.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Broot7
            wrote on last edited by Broot7
            #5

            That was just an example of the paintEvent override, I have the exact same thing in NodeViewTab.

            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 Extensions
            • Unsolved