我为QLineEdit创建了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;
}
它没有像我预期的那样工作,边界仍然存在状态disabled
和noFrame = true
。我是否错误地将CSS的属性组合在一起?
你真的非常非常接近。尝试
MyLineEdit[style="Normal"][noFrame="true"]:disabled
{
border: none;
background: gray;
}
来自CSS2文档(Qt StyleSheets supports):
多个属性选择器可用于引用元素的多个属性,甚至可以多次引用相同的属性。
这里,选择器匹配所有SPAN元素,其“hello”属性具有“Cleveland”值,其“goodbye”属性具有“Columbus”值:
span[hello="Cleveland"][goodbye="Columbus"] { color: blue; }