Qt Set Background Color of QLineEdit

前端 未结 4 1101

I\'m trying to change the background color of the QLineEdit and I can\'t figure it out at all.

I tried using stylesheets originally like th

相关标签:
4条回答
  • 2020-12-18 21:17

    Your code is almost correct. Only QLine edit uses the Base color. So if you do not want to replace existing stylesheet which can contain borders padding and margin and you want to change background only, use QPalette:

    QPalette palette = _ui->lnSearch->palette();
    palette.setColor(QPalette::Base, Qt::green);
    _ui->lnSearch->setPalette(palette);
    

    Thanks to: https://forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect

    0 讨论(0)
  • 2020-12-18 21:19

    I had to use background-color from standard css like this:

    QLineEdit* edit = new QLineEdit();
    edit->setStyleSheet("QLineEdit {background-color: black;}");
    

    I am using Qt 5.4

    0 讨论(0)
  • 2020-12-18 21:22

    You can set the background and text colors of line edit by setting the palette like :

    QLineEdit *le = new QLineEdit();
    
    QPalette palette;
    palette.setColor(QPalette::Base,Qt::black);
    palette.setColor(QPalette::Text,Qt::white);
    le->setPalette(palette);
    
    0 讨论(0)
  • 2020-12-18 21:33

    Works fine for me:

    QLineEdit *le = new QLineEdit();
    le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");
    
    0 讨论(0)
提交回复
热议问题