Important: Please read the Qt Code of Conduct -

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(", 10);");

    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