Allow user to change table color during run-time



  • Hi, i have a the following code:
    table->setStyleSheet("background-color: rgba(255, 255, 255, 10);");
    Now, i want to allow user to change the colors during run time, and i have 3 variables color_red, color_blue, color_green. The user writes the color to these 3 variables, is there any simplier way i can put these variables into the stylesheet?

    Currently what i did was:
    QString tempColor = "background-color: rgba("
    tempColor.append(color_red);
    tempColor.append(",");
    tempColor.append(color_green);
    tempColor.append(",");
    tempColor.append(color_blue);
    tempColor.append(", 10);");
    table->setStyleSheet(tempColor);

    Is there any way i can do these appends in one like?


  • Moderators

    What you are looking for is:

    QString tempColor = QString("background-color: rgba(%1,%2,%3,10)").arg(color_red).arg(color_green).arg(color_blue);
    

    Oh and you could even skip the tempColor and assign directly like,

    table->setStyleSheet(QString("background-color: rgba(%1,%2,%3,10)").arg(color_red).arg(color_green).arg(color_blue));
    

    And just for the furture, you can use QString::arg() to parse arguments like your would use sprintf(). Instead of things like %d, %s, etc, you use argument numbers, i.e. %1 %2 %3 %4 ... and then each call to .arg() at the end replaces #X with the argument.


Log in to reply
 

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