[Reserved] QT skin (QSS) Programming

Original link: http://www.cnblogs.com/raymon/archive/2012/04/23/2487068.html

Borrowing inspiration css of, Qt also supports the Qt own css, referred to QSS . With similar css, QSS main function of the ultimate goal is to make the interface elements of performance and interface separation, that is, the quality and form of separation, just as a person can put on different clothes, like at different times, css mechanism the introduction of such a skin design and interface controls separate software became possible, the application can freely change the appearance of the web interface like that.

A, QSS grammar

with css, he also has a selector and a declaration by the composition, selector specifies which is an effect on the control, and the statement is true declaration produces its effects. Such as:
QPushButton {color: red}
QPushButton specified to have an impact on all of its subclasses QPushButton or control (e.g., user defined MyPushButton), and the color: red foreground color indicating that all affected control are red.
In addition to "class name", "object name", "Qt attribute name" These three things are case sensitive among other things are not case-sensitive, such as representatives of the same color with the Color property.
If there are several selector specifies the same Declaration, can use a comma (,) to separate each of the selector, such as:
the QPushButton, QLineEdit, a QComboBox {Color: Red}
him corresponds to:
the QPushButton {Color: Red}
QLineEdit {Color: Red }
a QComboBox {Color: Red}
declaration is a series of part (attribute: value), a semicolon (;) of the various attribute-value pairs separated, curly braces ({}) together contain all the declaration.

1, the general selector (Selector)

Qt supports all of CSS2 defined selector Xiangxi content can be found on the w3c site http://www.w3.org/TR/CSS2/selector.html , one of the more common the selector types are:

  • Universal Type Selector : * All controls will be effective.
  • Selector Type : QPushButton matching instance QPushButton all instances and subclasses thereof.
  • Attribute selector : QPushButton [flat = "false" ] matches any instance of false QPushButton flat properties, properties divided into two, static and dynamic, static attributes () can be specified by Q_PROPERTY, dynamic attributes may be used to setProperty be specified as:
    QLineEdit * = nameEdit new new QLineEdit (the this);
    nameEdit-> the setProperty ( "mandatoryField", to true);
    if set qss after the Qt property changes, need to reset qss to put it into effect, may be used to unset then the SET QSS .
  • Class Selector : .QPushButton QPushButton all the matching instance, but not its subclasses, which is equivalent to: * [~ class = "QPushButton"]      ~ test = mean a type attribute contains QStringList given QString
  • ID selector : QPushButton # okButton Qt corresponding to the inside of the object name setting, the first setting corresponding to the control prior to use this as the okButton the CSS object name, such as: Ok-> setObjectName (tr ( " okButton"));
  • Descent selector : Match all examples QDialog QPushButton QDialog descent (including son, recursion and son son) as the QPushButton
  • Child selector : QDialog> QPushButton all QDialog matching instance a direct subclass QPushButton, contains no son son recursion.


2, the child controls selector

  • For complex control, which may be contained in other sub controls, such as a QComboxBox there is a drop-down button. So now if you want to set up drop-down button QComboxBox of it, so you can access:
    QComboBox :: drop-Down {Image: url (dropdown.png)} is marked by (: :)
  • Child controls the selector is represented by reference position of an element, the control element can be a single or a composite control that contains another child controls. Use subcontrol-origin attribute can change the default placement of the child controls, such as:
    a QComboBox {
           margin-right: 20px;
    }
    a QComboBox drop-down :: {
           subcontrol-origin: margin;
    }

    above statements can be used to change the drop-down margin .
  • Attribute can be used to change the relative positions of the sub-control with respect to the initial offset position, such as when a drop-down QCombox the button is pressed, we can use a small internal offset pressed to represents the effect, as follows:
    a QComboBox-Down arrow arrow :: {
           Image: URL (down_arrow.png);
    }
    a QComboBox ::-Down arrow arrow: pressed {
           position: relative;
           Top: 1px; left: 1px;
    }
  • The absolute position of the property allows the child controls to change their position and size of the control elements without a reference. Once the position is set up, the child controls may be treated as an ordinary widget, and the model may be used together. About combined model can refer to the following.

If you want to see what the child controls selector Qt support, may refer to: http://pepper.troll.no/s60prereleases/doc/stylesheet-reference.html#list-of-sub-controls-syntax.html
3, select the pseudo is (pseudo-states)

  • Pseudo selector colon (:) indicates, similar to the css pseudo selector, based on some rules to define the basic state of the control program, such as the mouse hover through the rule indicates the control state, and the press means press the button status. Such as:
    QPushButton: hover {
           Background-Color: Red;
    }
    represents the mouse passes QPushButton red background.
  • Pseudo tilde supports, such as (!):
    QRadioButton:! {Hover Color: Red}
    indicates no color when the mouse is moved on the display of his QRadioButton red.
  • Pseudo may be chained together, such as:
    QPushButton: hover: {pressed Color: blue;}!
    Denotes a display character when QPushButton blue did not move the mouse click, but if not displayed when clicked blue color.
  • And can also be used in combination with the previously spoken child controls selectors, such as:
    a QSpinBox ::-Down Button: hover {Image: URL (BTN-ComboBox-press.bmp)}
  • Like stated before, the dummy selector, the child controls the like can all be used to select a comma (,) represents a continuous partition provided the same, such as:
    the QPushButton: hover, a QSpinBox ::-Down Button, QCheckBoxes: Color the checked { : white; image: url (btn -combobox-press.bmp);} is expressed as follows
  • Please refer to: http://pepper.troll.no/s60prereleases/doc/stylesheet-reference.html#list-of-pseudo-states


Second, resolve conflicts

  • Use object name
    • First control program which is provided, such as:
      btnOne the QPushButton new new = (centralWidget);
      btnOne-> setObjectName (QString :: fromUtf8 ( "btnOneCh"));
    • Thus, we have an object name for the btnOneCh of the QPushButton, to test, using the object name specified manner, such as:
      the QPushButton # btnOneCh {color: red}
      the QPushButton {color: White}
      As can be seen, btnOncCh the color becomes red
  • Pseudo-selectors, such as hover, press, enabled the like, such as: buttons, "1" is in a disable, QPushButton:! Enabled {color: white}
  • All types selectors have a common characteristic, that is, if there are two properties conflict, they would at last appear in a subject, such as:
    QPushButton {Color: Red}
    QAbstractButton {Color: Gray}
    Because of QPushButton as QAbstractButton sub-category, if only the result QAbstractButton can imagine is that all QPushButton are gray, if only to set all QPushButton will QPushButton is red, when the two can set the time of onset, whichever occurs on the final text of , so the result is Grey
  • Of course, if there is # specified object name, he set the priority of the biggest, specific rules can refer to: http://www.w3.org/TR/CSS2/cascade.html#specificity , or http: // pepper.troll.no/s60prereleases/doc/stylesheet-syntax.html#conflict-resolution
    the QPushButton btnOneCh {# Color: Red}
    the QPushButton {Color: Blue}
    QAbstractButton {Color: Gray}
    Although QAbstractButton at the end, but before the specified #btnOneCh the QPushButton "a" of color is red so the last show is "a" is red.


Third, the cascading effect

  • Subclass can inherit StyleSheet parent class, but if the subclass which set up a conflict StyleSheet the parent class in the set, then of course will give priority to their own subclass,
    likewise, if you set the qApp when the plane, but in a which also controls a specific set, if there is a conflict, but also control their own priorities, for example, my face is set in the program: btnOneEn-> setStyleSheet ( "QPushButton { color: red}");
    and, when I then set qApp If the QPushButton the color is set to grey, the result for btnOneEn this QPushButton for his color was red.
    That's why here set grey of the btnOneEn but still red.
  • If we set a StyleSheet control is:
    QPushButton * myPushButton;
    myPushButton-> setStyleSheet ( "{* Color: Blue}");
    in fact, he and set: myPushButton-> setStyleSheet ( "color: blue");
    the same effect, but the latter setting will not have an effect on the subclass QPushButton, but first it will.


Fourth, the succession

of different and a little CSS, the CSS box model, if an element in it not an element, then the inside of the element will automatically inherit the property outside elements, but QSS which will not, such as:
a QPushButton If you put in one QGroupBox which, if: qApp-> setStyleSheet ( "QGroupBox {
does not represent the QGroupBox inside QPushButton will have color: red attribute, if any, to be displayed in order to indicate the , such as: qApp-> setStyleSheet ( "QGroupBox,
or QWidget :: setFont can also be used in applications which like property provided to the child controls.

Five, the Namespace conflict

type selector can be used to a particular type, such as:
class MyPushButton: the QPushButton public {
      // ...
}
qApp-> setStyleSheet ( "MyPushButton {background: Yellow;}");

because QSS use QObject: : className to determine the type of control to be endowed with a style sheet, if a user-defined control type in a namespace inside the case, QObject :: className returns <namespace> :: <classname> name syntax with this and the child controls selector conflict, in order to solve this problem, use "-" instead of "::
NS {namespace
      class MyPushButton: the QPushButton public {
          // ...
      }
}
qApp-> setSytleSheet ( "NS - MyPushButton {background: Yellow;}");

six, set attributes like

properties if used in a program arranged inside Q_PROPERTY , can qss use which: qproperty- <property name> form to access and set values. Such as:
MyLabel {qproperty-a pixmap: URL (pixmap.png);}
MyGroupBox {qproperty-titleColor: RGB (100, 200 is, 100);}
the QPushButton {qproperty-iconSize: 20px 20px;}
if the attribute is a reference to the when Q_ENUMS stated enum, to quote the name of its properties defined name to use instead of numbers.
Quote: http://pepper.troll.no/s60prereleases/doc/stylesheet

Reproduced in: https: //www.cnblogs.com/raymon/archive/2012/04/23/2487068.html

Guess you like

Origin blog.csdn.net/weixin_30501857/article/details/94794849