如何在 CSS 中设置多个属性?

How to set multiple properties in CSS?

我已经为 LineEdit 创建了 CSS。

LineEdit.cpp

void MyLineEdit::initStyleSheet()
{
  QString css = Css::instance().css( m_element );
  setProperty( "style", "normal" );
  setStyleSheet( css );
}

我有一个单独的 .css 样式文件:

MyLineEdit.css

....
MyLineEdit[style="Normal"]:focus
{
    border: 1px solid red;
}

MyLineEdit[style="Normal"]:disabled
{
    border: 1px solid gray;
    background: gray;
}

现在有一个奇怪的要求:MyLineEdit应该有一个方法叫做setNoFrame,在这个函数中我们为它设置了一个属性,这个属性仅对状态 disabled.

有效

这是我所做的:

MyLineEdit::setNoFrame()
{
  setProperty("noFrame","true");
  initSyleSheet();
}

这是我更新的 .css 数据

....
MyLineEdit[style="Normal"]:focus
{
  border: 1px solid red;
}

MyLineEdit[style="Normal"]:disabled
{
  border: 1px solid gray;
  background: gray;
}

MyLineEdit[style="Normal", noFrame="true"]:disabled
{
  border: none;
  background: gray;
}

它没有像我预期的那样工作,状态 disablednoFrame = true 的边界仍然存在。我在组合上述 CSS 的属性时出错了吗?

真的,真的很接近。尝试

MyLineEdit[style="Normal"][noFrame="true"]:disabled
{
  border: none;
  background: gray;
}

来自 CSS2 文档(Qt StyleSheets supports):

Multiple attribute selectors can be used to refer to several attributes of an element, or even several times to the same attribute.

Here, the selector matches all SPAN elements whose "hello" attribute has exactly the value "Cleveland" and whose "goodbye" attribute has exactly the value "Columbus":

span[hello="Cleveland"][goodbye="Columbus"] { color: blue; }