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. Qt Custom Widgets, CSS and Layouts
Forum Updated to NodeBB v4.3 + New Features

Qt Custom Widgets, CSS and Layouts

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k Views 1 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by webzoid
    #1

    I'm trying to figure out what logic Qt applies when it comes to loading in a CSS file and using the QApplication::setStyleSheet function. I have an application whereby my QMainWindow contains a number of QHBoxLayout and QVBoxLayout all containing promoted custom widgets derived from QWidget.

    I load in a CSS file into a QString before passing it into setStyleSheet. As I would expect, a lot of the styles of widgets change (push buttons, labels, etc) however, the custom widgets that I have inside the layouts behave rather oddly when it comes to being styles.

    As an example, my CSS file may contain the following styles:

    QPushButton {
        background-color: red;
    }
    
    MyWidget {
        background-color: blue;
    }
    
    

    If MyWidget widget is placed inside one of the QVBoxLayout or QHBoxLayout layouts then it would appear that the background colour is ignored. The QPushButton's are styled correctly as per the CSS. If I then add a CSS style overriding the QMainWindow background colour only then do I see a change to MyWidget - albeit the wrong change, the colour is taken from the QMainWindow widget.

    I'm already guessing that this isn't a very good explanation of my problem so in which case, I'll try and upload an example and link a screenshot here. To anyone that does understand, can anyone suggest anything? I can't quite figure out why Qt wouldn't apply my style when I explicitly give the name of the Widget class as the CSS class name.

    mrjjM 1 Reply Last reply
    0
    • webzoidW webzoid

      I'm trying to figure out what logic Qt applies when it comes to loading in a CSS file and using the QApplication::setStyleSheet function. I have an application whereby my QMainWindow contains a number of QHBoxLayout and QVBoxLayout all containing promoted custom widgets derived from QWidget.

      I load in a CSS file into a QString before passing it into setStyleSheet. As I would expect, a lot of the styles of widgets change (push buttons, labels, etc) however, the custom widgets that I have inside the layouts behave rather oddly when it comes to being styles.

      As an example, my CSS file may contain the following styles:

      QPushButton {
          background-color: red;
      }
      
      MyWidget {
          background-color: blue;
      }
      
      

      If MyWidget widget is placed inside one of the QVBoxLayout or QHBoxLayout layouts then it would appear that the background colour is ignored. The QPushButton's are styled correctly as per the CSS. If I then add a CSS style overriding the QMainWindow background colour only then do I see a change to MyWidget - albeit the wrong change, the colour is taken from the QMainWindow widget.

      I'm already guessing that this isn't a very good explanation of my problem so in which case, I'll try and upload an example and link a screenshot here. To anyone that does understand, can anyone suggest anything? I can't quite figure out why Qt wouldn't apply my style when I explicitly give the name of the Widget class as the CSS class name.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @webzoid
      Hi
      Did you custom widget override the paintEvent ?

      If yes, make sure you do in fact use the stylesheet

      void CustomWidget::paintEvent(QPaintEvent *)
       {
           QStyleOption opt;
           opt.init(this);
           QPainter p(this);
           style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
       }
      
      webzoidW 1 Reply Last reply
      2
      • mrjjM mrjj

        @webzoid
        Hi
        Did you custom widget override the paintEvent ?

        If yes, make sure you do in fact use the stylesheet

        void CustomWidget::paintEvent(QPaintEvent *)
         {
             QStyleOption opt;
             opt.init(this);
             QPainter p(this);
             style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
         }
        
        webzoidW Offline
        webzoidW Offline
        webzoid
        wrote on last edited by
        #3

        @mrjj This has solved the problem. I've created by own base Widget which overrides the paintEvent as you've suggested and everything works beautifully. Thank you.

        1 Reply Last reply
        1

        • Login

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