Application Development in Linux: Use QT to create a new style

Source: Internet
Author: User
Article Title: Application Development in Linux: Use QT to create a new style. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   1. Qt Style
A) Qt Introduction
Qt is a cross-platform C ++ graphical user interface application development library. using Qt, you can develop high-quality graphical user interfaces, it is fully object-oriented, easy to expand, and allows true component programming. Qt has achieved great success. In particular, its signal-slot mechanism is a communication mechanism worth studying. It is also the basis of KDE (K Desktop Enviroment), a standard component of Linux release.
  
B) style Mechanism
Qt's style mechanism implements the look and feel of GUI on different platforms. For example, Windows or Windows-xp are usually used on Windows platforms, on Unix platforms, Motif and CDE are usually used.
  
Shows the inheritance relationships of style-related classes in Qt.
   
QStyle is the base class of all style classes. It controls the interface style or perception of all components (widgets are controls in windows programming, it defines a large number of enumeration types and a dozen functions. The enumeration type indicates different elements on the interface (such as buttons in the combo box and button borders). The function controls the drawing of the graphic user interface, however, most functions are basically just declaration without function implementation. Their implementation is in QCommonStyle, QWindowStyle, QMotifStyle and its subclass. QStyle only implements three functions: drawItem (), itemRect (), and visualRect ().
  
DrawItem (): draws text and pixel graphs.
ItemRect (): the area of the returned text or image.
VisualRect (): returns the logical coordinates. This function enables Qt to implement the right-to-left style (AVEN and vivin are traditionally used to display text from right to left, so the control layout is also from right to left ). As shown in:
   
You can see that the menu, toolbar is right aligned, and button with a single button is on the right.
  
C) Steps for creating a new style
To implement a new style in Qt, you only need to select a style class (such as QCommonStyle or QStyle) as the parent class and then implement the functions you are interested in. The difficulty lies in the implementation of functions.
  
Select parent class: You can select QStyle, QCommonStyle, QWindowStyle, QMotifStyle, and any of their child classes as the parent class. You can choose either QWindowsStyle or QMotifStyle, or QCommonStyle or even QStyle, but the workload will be large, because many interface details need to be implemented by yourself.
  
Re-implement the necessary functions: If you want to modify the part of the interface style, re-implement the functions related to the part to be drawn. The following describes several functions in the QStyle to be reloaded, these functions control the layout and view of different elements on the graphic user interface.
  
1) void drawPrimitive (PrimitiveElement pe,
QPainter * p,
Const QRect & r,
Const QColorGroup & cg,
SFlags flags = Style_Default,
Const QStyleOption & opt = QStyleOption: Default );
            
Function: draws basic graphic elements, such as buttons with arrows in QSpinBox.
Parameter: PrimitiveElement pe: This enumeration variable represents the basic graphic interface elements to be drawn (the basic graphic user interface elements mentioned here refer to an atomic element that cannot be further divided in the GUI, for example, if the button in the combo box is painted with a black triangle
QPainter * p: pointer to the QPainter class. This class processes all painting operations in Qt, including text, graphics, and images.
QRect & r: indicates a rectangular area in which Qt draws the basic interface element (PrimitiveElement ).
QColorGroup & cg: QColorGroup indicates the color group of a widget. The color group contains various colors used when the widget draws itself, such as the foreground color and background color. Displays various color attributes in a color group.
   
SFlags flags: controls how to draw graphical element labels.
QStyleOption & opt: different parameters are required to draw different Widgets. For example, to draw a panel, you may need to use the line width as an additional parameter to draw a focus rectangle (focus rect) the background color may be used as an additional parameter. Therefore, Qt provides a special QStyleOption class to encapsulate different parameters that may be required by different Widgets. opt points to the object of such a class.
  
  
2) void drawComplexControl (ComplexControl control,
QPainter * p,
Const QWidget * widget,
Const QRect & r,
Const QColorGroup & cg,
SFlags flags = Style_Default,
SCFlags controls = QStyle: SC _All,
SCFlags active = QStyle: SC _None,
Const QStyleOption & opt = QStyleOption: Default)
     
Function: draws widgets such as SpinWidget, comboBox, slider, and listView.
  
Parameters:
  
ComplexControl control: it is an enumeration volume that indicates the complex control components (widgets) to be drawn, such as a combo box and a list box.
QPainter * p: pointer to QPainter. This class processes all painting operations in Qt, including text, graphics, and images.
QWidget * widget: pointer to QWdget or its subclass. You can convert the value of the control to a suitable type. For example, if you want to draw a QSpinWidget, set the control value to CC_SpinWidget, the widget points to an instance of a QSpinWidget (subclass of QWidget ). You can use this variable to access the member functions and member variables of QSpinWidget. For example, you can call the sizeHint function of QSpinWidget to obtain the default size (a rectangle space) of this component ).
QRect & r: indicates a rectangular area in which Qt draws controls or its child widgets.
QColorGroup & cg: QColorGroup indicates the color group of a widget. The color group contains various colors used when the widget draws itself, such as the foreground color and background color.
SFlags flags: controls how to draw graphical element labels
SCFlags controls indicates the child part of the control component for drawing the complex control component. The default value is SC _All. That is, the entire control is drawn instead of a child part of the control component. (Note that control and controls are two different parameters)
QStyleOption & opt: different additional parameters may be required when drawing different Widgets. This variable provides different information when drawing different widgets.
  
  
3) QRect querySubControlMetrics (ComplexControl control,
Const QWidget * widget,
SubControl SC,
Const QStyleOption & = QStyleOption: Default)
  
Function: obtains coordinates and dimensions of child parts. This function controls the layout of a complex control. By reloading this function, you can draw the drop-down buttons of the combo box on the left rather than the default right.
  
Parameters:
ComplexControl control: the enumerated quantity, indicating the complex control components (widgets) to be drawn, such as the combo box and list box.
QWidget * widget: a pointer to a QWidget or its subclass. You can convert it to a suitable type based on the preceding control value (cast). For example, if you want to draw a QSpinWidget, set the control value to CC_SpinWidget, the widget points to an instance of QSpinWidget (a subclass of QWidget. You can use this variable to access the member functions and member variables of QSpinWidget. For example, you can call the sizeHint function of QSpinWidget to obtain the default size (a rectangle space) of this component ).
SubControl SC: enumeration quantity. A complex part may consist of multiple child parts. The SC variable is used to obtain the coordinates and dimensions of the Child part.
QStyleOption & opt: different extra information may be required to calculate the size of different parts. QStyleOption encapsulates this information.
  
   2. Create a New Style
Next we will use an example to introduce the entire process of creating a new style. Before programming, let's take a look at what the final result is. (The QSpinBox class in Qt is implemented through QSpinWidget)
Effect of the default style: effect of the new style:
We can see that in the new style, our SpinBox has a vertical display effect. Next we will follow the steps described above to create a new style.
  
1) Select the base class: We select the QWindowsStyle class as the base class of our new style class. Of course, you can also choose QMotifStyle. In this example, you can also choose QCommonStyle. We generally do not recommend that you use QCommonStyle as the base class, because QCommonStyle only implements some interface components. to implement a complete style class, we need to write a lot of code again.
  
2) reload-related functions: In this routine, we only modify the spinBox style. The implementation of this component (widget) is only related to the three functions of the QStyle class drawPrimitive, drawComplexControl, and qureySubControlMerics, therefore, we only need to reload the Code related to these three functions. next, comment on the key part of the code. Detailed code can be downloaded later.
  
Function for drawing buttons in the spinbox:
  
Void CustomStyle: drawPrimitive (PrimitiveElement pe,
QPainter * p,
Const QRect & r,
Const QColorGroup & cg,
SFlags flags,
Const QStyleOption & opt) const
{
/* PE_SpinWidgetUp and PE_SpinWidgetDown indicate the bottom and top buttons in the spinBox. The following Code sets the triangles in the two buttons to the left and right respectively */
If (pe = PE_SpinWidgetUp) | (pe = PE_SpinWidgetDown )){
Int fw = pixelMetric (pm_defaframeframewidth, 0); // fw indicates the Border width. The default value is 2.
QRect br; // The button boundary rectangle on the spinBox is not the boundary rectangle of the spinBox.
Br. setRect (r. x () + fw, r. y () + fw, r. width ()-fw * 2,
R. height ()-fw *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.