Changing the css of QLineEdit



  • Hi,

    I'm trying to update info in a lineedit but I want the modified info to display in red. When it's modified (even if the same exact info is retyped), I want it to display in red.

    What I did is :

    1. Using Qt designer I have the info inside the lineedit display in green.
    2. retrieve info and load it in lineedit
    3. after info is loaded, I try to change future text by ui->updateNameLEdit->setStyleSheet("color: red");

    the lineedit info display in geen. When I attempt to modify, it remains in green. How can I get it to work?


  • Lifetime Qt Champion

    Hi,

    This wiki article might be of interest.



  • I followed the instructions to the best of my ability but it doesn't seem to be work still.

        ui->updateNameLEdit->setStyleSheet("QLineEdit[urgent=true] { color: red;}");
        ui->updateNameLEdit->setProperty("urgent", true);
        ui->updateNameLEdit->style()->unpolish(ui->updateNameLEdit);
        ui->updateNameLEdit->style()->polish(ui->updateNameLEdit);
    

  • Moderators

    @WhatIf

    ui->updateNameLEdit->setStyleSheet("QLineEdit { color: red;}");
    

    If you set the stylesheet directly this way, it definitly works. No need to do the polishing again.
    If it still doesnt work you need to show your code, because you might change something in another place?



  • My mistake...had the code in the wrong place. But I'm not getting the output I'm after.

    If I only include

        ui->updateNameLEdit->setStyleSheet("QLineEdit[urgent=true] { color: red;}");
        
    

    the text from the start display in black.

    if I include the four lines of code

        ui->updateNameLEdit->setStyleSheet("QLineEdit[urgent=true] { color: red;}");
        ui->updateNameLEdit->setProperty("urgent", true);
        ui->updateNameLEdit->style()->unpolish(ui->updateNameLEdit);
        ui->updateNameLEdit->style()->polish(ui->updateNameLEdit);
    

    the text display in red from start.

    What I was looking for is for the text to display first in green and if it's changed, the text should display in red.


  • Moderators

    @WhatIf said:

    ui->updateNameLEdit->setProperty("urgent", false);
    ui->updateNameLEdit->setStyleSheet(
          "QLineEdit { color: green; }"
          "QLineEdit[urgent=true] { color: red;}"
    );
    connect( ui->updateNameLEdit, SIGNAL(textChanged(const QSTring &)), this, SLOT(onTextChanged()),  )
    ...
    void onTextChanged()
    {
         ui->updateNameLEdit->setProperty("urgent", true);
         ui->updateNameLEdit->style()->polish( ui->updateNameLEdit );
    }
    

    this should be all you need to achieve this


Log in to reply
 

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