Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Trying to set color



  • I am trying to draw an arc when we slide the slider i will draw the arc with Red color . Now when i set the backgroud color of the ui to black the arc which i draw is not visible. My code:

    this->setStyleSheet("background-color:black;");
    //this->setBackgroundRole(QPalette::Foreground);
    QPainter p(this);
    QPen pen;
    pen.setWidth(10);
    pen.setColor(Qt::red);
    p.setPen(pen);
    
    p.setRenderHint(QPainter::Antialiasing);
    
    QRectF rectangle(ui->LB_Progress->x()-20, ui->LB_Progress->y()-20, 80.0, 80.0);
    //to understand these magic numbers, look drawArc method in Qt doc
     int startAngle = -(270 * 16);
     int spanAngle =-(progress * 360 * 16);
    
    p.drawArc(rectangle, startAngle, spanAngle);
    
    p.drawText(rectangle,Qt::AlignCenter,QString::number(progress*100)+" %");

  • Lifetime Qt Champion

    Hi,

    Why are you using a stylesheet since you do the painting yourself ?
    Start by filling the widget in black and then paint the rest over it.



  • @SGaist said in Trying to set color:

    how to do that sir,
    I am not getting your point



  • @SGaist

    Sir i used something like this and it worked

     QPalette pal(this->palette());
     QColor color(Qt::black);
     pal.setColor (QPalette::Background, color);
     this->setAutoFillBackground (true);
     this->setPalette(pal);
    

    Instead of using stylesheet for filling background i used this type to fill my widget background.


  • Lifetime Qt Champion

    @ManiRon
    Hi
    Since you already have a paintEvent,
    all you need to paint it black is
    p.setBrush(Qt::black);
    p.drawRect( rect () );


Log in to reply