Qt5 Style Sheets Introduction and Example

Qt5 Style Sheets Introduction And Example – In this Qt5 article we are going to talk about Qt5 Style Sheets Introduction and also we are going to create some examples.

 

Qt5 Tutorial: Qt Style Sheets

Qt Style Sheets are a powerful mechanism that allows you to customize the appearance of widgets, in addition to what is already possible by subclassing QStyle. The concepts, terminology, and syntax of Qt Style Sheets are heavily inspired by HTML Cascading Style Sheets (CSS) but adapted to the world of widgets. Styles sheets are textual specifications that can be set on the whole application using QApplication::setStyleSheet() or on a specific widget (and its children) using QWidget::setStyleSheet(). If several style sheets are set at different levels, Qt derives the effective style sheet from all of those that are set. This is called cascading. For example, the following style sheet specifies that all QLineEdits should use yellow as their background color, and all QCheckBoxes should use red as the text color:

 

 

 

Style Sheet Syntax And Rules

Qt Style Sheet terminology and syntactic rules are almost identical to those of HTML CSS. If you already know CSS, you can probably skim quickly through this section.Style sheets consist of a sequence of style rules. A style rule is made up of a selector and a declaration. The selector specifies which widgets are affected by the rule; the declaration specifies which properties should be set on the widget. For example:

In the above style rule, QPushButton is the selector and { color: red } is the declaration. The rule specifies that QPushButton and its subclasses (e.g., MyPushButton) should use red as their foreground color. Qt Style Sheet is generally case insensitive (i.e., color, Color, COLOR, and cOloR refer to the same property). The only exceptions are class names, object names, and Qt property names, which are case sensitive.

 

 

Several selectors can be specified for the same declaration, using commas (,) to separate the selectors. For example, the rule

is equivalent to this sequence of three rules:

 

 

So now create a new project in Qt5 C++ framework. also we are going to add some QPushButtons in our window.

 

 

OK now for creating style sheets you need to right click on the specific widget, after that click on Change Stylesheets. you see a dialog, this dialog is the place that you can add CSS style sheets. the second way is that you can click of the window, we are going to click on the and add our CSS styles for all widgets.

Qt5 Style Sheets
Qt5 Style Sheets

 

 

 

OK now can add some more buttons in our window also we add a QLineEdit, after that right click on the window also click on Change Stylesheets, add these styles.

 

 

 

 

 

 

So run your complete code and this will be the result

Qt5 Style Sheets Introduction and Example
Qt5 Style Sheets Introduction and Example

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×