首页 文章

Qt设置QLineEdit的背景颜色

提问于
浏览
9

我正在尝试更改 QLineEdit 的背景颜色,我根本无法弄明白 .

我最初尝试使用 stylesheets

QLineEdit *le = new QLineEdit();
le->setStyleSheet("background:#000;");

但那没有做任何事情 . 我试过像这样使用 QPalette

QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Background, Qt::black);
le.setPalette(palette);

但这也没有做任何事情 . 我一直在寻找,找不到任何东西 . 我做错了什么或有其他办法吗?

4 回答

  • 2

    我不得不使用标准css的背景颜色,如下所示:

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

    我正在使用Qt 5.4

  • 2

    对我来说很好:

    QLineEdit *le = new QLineEdit();
    le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");
    
  • 7

    您可以通过设置调色板来设置行编辑的背景和文本颜色:

    QLineEdit *le = new QLineEdit();
    
    QPalette palette;
    palette.setColor(QPalette::Base,Qt::black);
    palette.setColor(QPalette::Text,Qt::white);
    le->setPalette(palette);
    
  • 9

    你的代码几乎是正确的 . 只有QLine编辑使用基色 . 因此,如果您不想替换可以包含边框填充和边距的现有样式表,并且您只想更改背景,请使用QPalette:

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

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

相关问题