Set tableWidget item background color 60% red and 40% white



  • Hello!

    I had an idea to visualize results displayed in a tableWidget by setting the background color. Lets say 60% voted for Pizza, i though it may be a nice idea to set the background like a progress bar to visualize it. At the moment i use QLinearGradient and it does look very nice.

    QLinearGradient linearGrad(QPointF(0, 25), QPointF(fillme, 25));
    linearGrad.setColorAt(0, Qt::red);
    linearGrad.setColorAt(1, Qt::white);
    
    ui->tableWidget->item(row, 8)->setBackground(linearGrad);
    

    The downside of QLinearGradient is this fluent transition between the colors, because there is no exact endpoint. I know, i kind of use it for the wrong reason, but so far i did fail to paint the background partial. I was able to set colors and images for the background but so far only for the whole background of the item, not the just 60% of it.

    Can i disable the color transition in QLinearGradient?
    Is there a better solution to my problem?

    Thank you!


  • Moderators

    @qDebug
    Do you mean something like this?

    QLinearGradient linearGrad(QPointF(0, 25), QPointF(fillme, 25));
    linearGrad.setColorAt(0, Qt::red);
    linearGrad.setColorAt(0.6, Qt::red);
    linearGrad.setColorAt(0.600000001, Qt::white);
    linearGrad.setColorAt(1, Qt::white);
    
    ui->tableWidget->item(row, 8)->setBackground(linearGrad);
    

    Not the most beautiful solution but it works.



  • Thank you!

    Just perfect! :D


Log in to reply
 

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