How to set last(right) chunk radius of QProgressBar with QSS ?



  • As we know, in QTabBar can set firts and last tab QSS, like below QSS:

    QTabBar::tab {
        color:blue;
    }
    
    /** first and last set color red. */
    QTabBar::tab:first {
        color:red;
    }
    
    QTabBar::tab:last {
        color:red;
    }
    

    my QProgressBar is horizontal, And I want the right(last) chunk has border-top-right-radius and border-bottom-right-radius.

    But, It failed:

    QProgressBar {
        border: 1px solid gray;
        border-radius: 4px;
        text-align: center;
        height:8px; 
        font-size:1px;
    }
    
    QProgressBar::chunk {
        width: 10px;
        /*border-top-right-radius: 4px;*/ // not good
        /*border-bottom-right-radius: 4px;*/
        background-color: rgb(0,65,153);
    }
    
    /** failed */
    QProgressBar::chunk:last { // or right
        width: 10px;
        border-top-right-radius: 4px;
        border-bottom-right-radius: 4px;
        background-color: rgb(0,65,153);
    }
    

    if QSS not, I will try reimplement QProgressBar::paintEvent.



  • I save my problem, not use QSS. I reimplement QProgressBar::paintEvent. Share for you.

    If someone knows how to use QSS to work it out. can reply me. Thank u.

    void ProgressBar::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing,true);
    
        QRect ret = this->rect();
    
        QPen pen;
        int x,y,w,h;
    
        w=1;
        pen.setWidth(w);
        pen.setColor(Qt::gray);
        painter.setPen(pen);
    
        QRect arcRect;
        arcRect = QRect(0,0,ret.height(),ret.height());
        int startAngle = 90 * 16;
        int spanAngle  = 180 * 16;
        painter.drawArc(arcRect,startAngle,spanAngle);
    
        arcRect = QRect(ret.width()-ret.height(),0,ret.height(),ret.height());
        startAngle = 270 * 16;
        spanAngle  = 180 * 16;
        painter.drawArc(arcRect,startAngle,spanAngle);
    
        QPoint p1(ret.height()/2,0);
        QPoint p2(ret.width()-ret.height()/2,0);
        painter.drawLine(p1,p2);
    
        p1 = QPoint(ret.height()/2,ret.height());
        p2 = QPoint(ret.width()-ret.height()/2,ret.height());
        painter.drawLine(p1,p2);
    
        int len = ret.width()/maximum();
        x = y = 0;
        w = h = ret.height();
       
        QColor col = QColor(0,65,153);
        int val = value();
        for(int i=0; i < val; i++){
            QPainterPath path;
            x = len*i;
            path.addEllipse(x,y,w,h);
            painter.fillPath(path,col);
        }
    
        painter.fillRect(QRect(h/2,0,x,h),col);
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.