重置Qt样式表

amr*_*ree 2 qt stylesheet signals-slots

我设法将我的QLineEdit设置为这样的样式:

替代文字http://www.kimag.es/share/54278758.png

void Utilities::setFormErrorStyle(QLineEdit *lineEdit)
{
    lineEdit->setStyleSheet(
            "background-color: #FF8A8A;"
            "background-image: url(:/resources/warning.png);"
            "background-position: right center;"
            "background-repeat: no-repeat;"
            "");
}
Run Code Online (Sandbox Code Playgroud)

我用这个函数调用了

Utilities *util = new Utilities;
util->setFormErrorStyle(lineNoStaf);
Run Code Online (Sandbox Code Playgroud)

流程应该是这样的:

  1. 用户打开表单
  2. 用户填写数据
  3. 用户提交数据
  4. 得到了错误
  5. 使用 setFormErrorStyle()
  6. 用户编辑QLineEdit中的文本,样式消失

此功能应该可以一遍又一遍地重复使用,但是如何将QLineEdit信号连接textChanged()到其他类中的功能,该功能将重置样式表,然后断开信号,使其在每次文本更改时都不会连续运行?

Car*_*sch 5

Qt还允许在其样式表中使用动态属性,这意味着您不需要为表单中的每个窗口小部件类型编写自己的类.

来自http://qt-project.org/doc/qt-4.8/stylesheet-examples.html

使用动态属性进行自定义

在许多情况下,我们需要提供具有必填字段的表单.为了向用户指示该字段是强制性的,一种有效的(尽管是美学上可疑的)解决方案是使用黄色作为那些字段的背景颜色.事实证明,使用Qt样式表很容易实现.首先,我们将使用以下应用程序范围的样式表:

 *[mandatoryField="true"] { background-color: yellow }
Run Code Online (Sandbox Code Playgroud)

这意味着其mandatoryField Qt属性设置为true的每个窗口小部件都将具有黄色背景.然后,对于每个必需字段小部件,我们只需动态创建mandatoryField属性并将其设置为true.例如:

 QLineEdit *nameEdit = new QLineEdit(this);
 nameEdit->setProperty("mandatoryField", true);

 QLineEdit *emailEdit = new QLineEdit(this);
 emailEdit->setProperty("mandatoryField", true);

 QSpinBox *ageSpinBox = new QSpinBox(this);
 ageSpinBox->setProperty("mandatoryField", true);
Run Code Online (Sandbox Code Playgroud)

也适用于Qt 4.3!