[Qt record] attribute Q_PROPERTY

use:

Qt has a property system. I often use in QSS

QWidget#SWNotifyMsgDialog QLabel#label_sure[status="normal"]

Use in code with

ui.label_sure->setProperty("status","warning");
函数原型:bool QObject:setProperty(const char *name,const QVariant &value)

Set the QSS style of label. The above is actually a way of using the Qt property system.

Learn more about:

However, the Qt property system can be seen from the setProperty() function, which is similar to a key-value key-value pair.
Also, setProperty() can configure property() to use. The former is to set the property, and the latter is to get the property.

ui.label_sure->property("status").toString();
函数原型:QVariant QObject::property(const char *name)const

Coding convenience can be enhanced by setting properties and getting properties.

For example: I get the current status of a control by setting the "status" property. At least one variable and two state settings are saved.

custom control

If I want to change the color and style of the blinking insertion cursor in the QLineEdit control, this setting is not supported by default, and I can only use a custom control to draw the cursor and draw it. And, I wish I could use qss styles to control.

Using the Q_PROPERTY macro, you can achieve your goal.

class CodeLineEdit : public QLineEdit
{
	QOBJECT
		QPROPERTY(QColor caretColor READ caretColor WRITE setCaretColor DESIGNABLE true)
		Q_PROPERTY(int caretwidth READ caretwidth WRITE setCaretwidth DESIGNABLE true)
}

The brackets are: type, variable name, READ, the function name to read this value (usually the variable name), WRITE, the function name to write this value (usually set+variable name). The latter can be omitted.

  1. With this Q_PROPERTY macro, you can also use setProperty() to set this value.
  2. Also available in the property editor in Qt Designer. Click the icon to create a dynamic attribute
    insert image description here
    . The attribute name and type must be consistent with the definition.
    insert image description here
    Then it will appear in the property editor.
    Please add a picture description
    Use this, which is the same as the default property above.
    3) You can also use qproperty- in QSS to add styles.
    Please add a picture description

Guess you like

Origin blog.csdn.net/qq_41214278/article/details/128159828