一、
1.為了用戶界面外觀的動態變化,屬性選擇器可以與動態屬性組合使用。
2.當一個屬性值變化時,所引用的樣式不會自動更新。相反地,必須手動觸發更新才會生效。
unpolish()用於清理之前的樣式,而polish()則用於添加新的樣式。
二、使用舉例
qss
QFrame#frmPreImg[selected=false]{ border:none; background-color:#D8DFEA; } QFrame#frmPreImg[selected=true]{ border:4px solid #32CD32; background-color:#D8DFEA; }
使用
m_frmPreImg1->setProperty("selected",true); m_frmPreImg1->style()->unpolish(m_frmPreImg1); m_frmPreImg1->style()->polish(m_frmPreImg1);
m_frmPreImg1->update();
qss
QLabel#lblConnectStatusStyle[connected="close"]{ background:url(:/images/gray.png) no-repeat; } QLabel#lblConnectStatusStyle[connected="ok"]{ background:url(:/images/green.png) no-repeat; }
void SerialPortWidget::showConnectStatus(bool isConnected) { if(isConnected) { lblConnectStatus->setProperty("connected","ok"); } else { lblConnectStatus->setProperty("connected","close"); } lblConnectStatus->style()->unpolish(lblConnectStatus); lblConnectStatus->style()->polish(lblConnectStatus); lblConnectStatus->update(); }
