Qt Creator source code analysis series --UI interface: StyleBar class

Analysis of Qt Creator software interface part of the code of the article content focused. Starting styledbar analysis plug-in module, the project file in the path \ qt-creator-master \ qt-creator-master \ src \ libs \ utils \ utils-lib.

StyleBar class

Qt Creator StyleBar class is relatively simple interface classes, and files containing utils_global.h QWidget file. The utils_global.h and QTCREATOR_UTILS_EXPORT macro related.

class QTCREATOR_UTILS_EXPORT StyledBar : public QWidget
{
    Q_OBJECT
public:
    StyledBar(QWidget *parent = nullptr);
    void setSingleRow(bool singleRow);
    bool isSingleRow() const;
    void setLightColored(bool lightColored);
    bool isLightColored() const;
protected:
    void paintEvent(QPaintEvent *event) override;
};

From the statement it can be seen in its class inherited from QWidget, no member variables to customize the only member functions. In the class implementation file, it contains QPainter and QStyleOption header files.
Constructor look as follows:

StyledBar::StyledBar(QWidget *parent) : QWidget(parent)
{
    setProperty("panelwidget", true);
    setProperty("panelwidget_singlerow", true);
    setProperty("lightColored", false);
}

Constructor sets of attribute panelwidget true, panelwidget_singlerow is true, lightColored is fasle.
Here Insert Picture Description
The value of the name attribute of an object to value. If you use Q_PROPERTY defines the attributes in the class Returns true if successful, otherwise false. If the property is not defined Q_PROPERTY use, and therefore not listed in the meta-object of the meta-object, it is added as a dynamic property and returns false.
Provide information on all available properties by metaObject () and dynamicPropertyNames ().
You can use property () query again dynamic attributes and attribute values can be set to an invalid QVariant to remove it. Change the dynamic property values cause the QDynamicPropertyChangeEvent sent to the object.

setSingleRow and isSingleRow, setLightColored and isLightColored is set and query functions of the above properties.

void StyledBar::setSingleRow(bool singleRow)
{
    setProperty("panelwidget_singlerow", singleRow);
}

bool StyledBar::isSingleRow() const
{
    return property("panelwidget_singlerow").toBool();
}

Here the function of the ratio more than the setLightColored foreach code. - "Qt will be discussed a class of QStyle
Here Insert Picture Description
appearance initialized to the widget. After you've created a full part in the first show at some point before the parts, each part will call this function. Note that the default implementation does nothing. This function in operation may be reasonable to call QWidget :: setBackgroundMode () function for the widget. Do not use this function to set geometry. Re-implement this feature provides a back door, you can change the widget's appearance through the back door, but for Qt style engine, it is rarely necessary to implement this feature. Reimplement drawItemPixmap (), drawItemText (), drawPrimitive () and so on.
QWidget :: inherits () function may provide sufficient information to allow specific custom class. However, because the desired new subclass can reasonably QStyle work in all current and future widgets, it is recommended to limit the use of hard-coded customizations.

bool StyledBar::isLightColored() const
{
    return property("lightColored").toBool();
}
void StyledBar::setLightColored(bool lightColored)
{
    if (isLightColored() == lightColored)
        return;
    setProperty("lightColored", lightColored);
    foreach (QWidget *childWidget, findChildren<QWidget *>())
        childWidget->style()->polish(childWidget);
}

paintEvent event is drawing class

void StyledBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    QStyleOptionToolBar option;
    option.rect = rect();
    option.state = QStyle::State_Horizontal; //用于指示小部件是否水平放置
    style()->drawControl(QStyle::CE_ToolBar, &option, &painter, this);
}

QStyleOptionToolBar class used to describe the parameters for drawing toolbar. QStyleOptionToolBar QStyle function contains all the information needed to draw QToolBar .
For performance reasons, direct access to the member variable (i.e., with or -> operator). This low-level sense of the structure is easy to use, and stressed that these parameters are only the style used by the function. QStyleOptionToolBar class contains lineWidth for drawing and midLineWidth widget. Information on the position should have a toolbar line position (positionOfLine) and toolbar in the line should also stores information about what region the toolbar, if movable. In addition, this class provides the two enumeration: ToolBarFeature enumeration is used to describe whether the toolbar is movable, while the position ToolBarPosition enumeration is used to describe the position of the toolbar toolbar and row in the row.

Here Insert Picture Description
Use the painter drawing a given element, which is specified by the painter option. widget parameter is optional and can be used to help draw controls. option parameter is a pointer pointing QStyleOption object, the object may be used qstyleoption_cast () function is cast to the correct subclasses.
The following table lists the style options subclass and its associated control elements. Style Options contains all the parameters needed to draw the control, including QStyleOption :: state, flag style used when drawing that contains them. The table also describes the flag is set when a given options to the appropriate sub-class.
Note that if the control element is not listed here, it is because it uses ordinary QStyleOption object.
Here all the contents of the table does not give a detailed reference manual.
Here Insert Picture Description

--------- "
a QStyle class is an abstract base class that encapsulates the appearance of the GUI. Qt QStyle contains a set of sub-categories, sub-categories can simulate different platforms Qt supports (QWindowsStyle, QMacStyle etc.) style. By default, these styles built in Qt GUI module. Styles can be used as plug-ins. Qt built WidgetUse QStyle perform almost all the graphics rendering, so as to ensure that they look exactly the same as the equivalent native widgets .
The following figure shows nine different styles QComboBox.
Here Insert Picture Description
The development of perception-style custom widgets:
If you are developing custom widgets, and want them all to look good on all platforms, you can use the various parts of widgets drawn QStyle function execution window, for example drawItemText (), drawItemPixmap (), drawPrimitive (), drawControl () and drawComplexControl ().
Most QStyle Draw function takes four parameters:

  • An enumeration value that specifies the graphical element to draw
  • A QStyleOption specify how and where to render that element
  • A QPainter should be used to draw elements
  • (Optional) performed thereon QWidget drawing
    QStyle obtain all the information required to render graphical elements from QStyleOption. If the style its implementation requires special effects (e.g., animation on macOS default button), then the last widget as an argument, but this is not mandatory. In fact, by properly setting QPainter, you can use any QStyle painting on painting equipment, not just widgets. QStyleOption having various subclasses, for drawing various types of graphical elements . For example, PE_FrameFocusRect need a QStyleOptionFocusRect parameters.
    To ensure that the drawing operation as quickly as possible, QStyleOption and its subclasses have public data members. For more information about how to use it, see QStyleOption class documentation.Here Insert Picture Description

StyledSeparator

A simpler class:

class QTCREATOR_UTILS_EXPORT StyledSeparator : public QWidget
{
    Q_OBJECT
public:
    StyledSeparator(QWidget *parent = nullptr);
protected:
    void paintEvent(QPaintEvent *event) override;
};
StyledSeparator::StyledSeparator(QWidget *parent) : QWidget(parent)
{
    setFixedWidth(10);
}
void StyledSeparator::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    QStyleOption option;
    option.rect = rect();
    option.state = QStyle::State_Horizontal;
    option.palette = palette();
    style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &option, &painter, this);
}

Here Insert Picture Description
Use option specified style options, use the drawing tools to draw given the basic elements. Widget parameter is optional, and may help draw the basic elements comprises small parts.

Published 101 original articles · won praise 108 · views 20000 +

Guess you like

Origin blog.csdn.net/asmartkiller/article/details/104350744