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. QPainter and QStyleSheet
Forum Updated to NodeBB v4.3 + New Features

QPainter and QStyleSheet

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 417 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.
  • V Offline
    V Offline
    Vlad02
    wrote on last edited by
    #1

    Hi everyone, I've run into a small problem. I am setting a QStyleSheet in a widget using the "background-color" attribute. I am also drawing a rectangle in the paintEvent event. Below are code fragments:

    ui->centralwidget->setStyleSheet("background-color: black");
    ...
    void EmyWindow::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setBrush(QColor(QString("#FFFFFF")));
        painter.setPen(QPen(Qt::red, 2));
        painter.drawRect(50, 25, 100, 30);
    }
    
    

    Problem is next: my rectangle drawing early than background-color, so I don't see it. How I can draw rectangle after filling background?

    M 1 Reply Last reply
    0
    • V Vlad02

      Hi everyone, I've run into a small problem. I am setting a QStyleSheet in a widget using the "background-color" attribute. I am also drawing a rectangle in the paintEvent event. Below are code fragments:

      ui->centralwidget->setStyleSheet("background-color: black");
      ...
      void EmyWindow::paintEvent(QPaintEvent *event)
      {
          QPainter painter(this);
          painter.setBrush(QColor(QString("#FFFFFF")));
          painter.setPen(QPen(Qt::red, 2));
          painter.drawRect(50, 25, 100, 30);
      }
      
      

      Problem is next: my rectangle drawing early than background-color, so I don't see it. How I can draw rectangle after filling background?

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @Vlad02
      Maybe by calling QWidget::painEvent()
      on top.
      Anyway, I don't understand why you don't draw the background yourself in paintEvent.

      V 1 Reply Last reply
      2
      • M mpergand

        @Vlad02
        Maybe by calling QWidget::painEvent()
        on top.
        Anyway, I don't understand why you don't draw the background yourself in paintEvent.

        V Offline
        V Offline
        Vlad02
        wrote on last edited by
        #3

        @mpergand I just need to redraw it periodically, won't that slow down the redraw? Given that I will be redrawing this widget and another child widget

        M 1 Reply Last reply
        0
        • V Vlad02

          @mpergand I just need to redraw it periodically, won't that slow down the redraw? Given that I will be redrawing this widget and another child widget

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          @Vlad02

          Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget :

          void CustomWidget::paintEvent(QPaintEvent* event)
          {
              QStyleOption opt;
              opt.init(this);
              QPainter p(this);
              style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
          
              QWidget::paintEvent(event);
          }
          

          https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget

          Warning: Function setStyleSheet is particularly useful for demonstration purposes, where you want to show Qt's styling capabilities. Real applications should avoid it and use one consistent GUI style instead. As applicable to setStyle too.

          Agree 200%

          1 Reply Last reply
          2

          • Login

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