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

Qt 如何在CSS中设置多个属性?,qt,stylesheet,qtstylesheets,qlineedit,Qt,Stylesheet,Qtstylesheets,Qlineedit,我已经为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"]:fo

我已经为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;
}
它没有像我预期的那样工作,对于state
disabled
noFrame=true
,边界仍然存在。我在组合上面CSS的属性时有错误吗?

你真的非常接近了。试一试

MyLineEdit[style="Normal"][noFrame="true"]:disabled
{
  border: none;
  background: gray;
}
从CSS2文件(其中):

多个属性选择器可用于引用元素的多个属性,甚至多次引用同一属性

这里,选择器匹配所有SPAN元素,其“hello”属性的值正好是“Cleveland”,其“bye”属性的值正好是“Columbus”:

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