Let's use an example to explain the application of the stylesheet. This example is based on the QT Demo, more complex, there is a certain degree of difficulty, basically covers the previous chapters of the various skill points, mainly including:
How to customize the style sheet for QT
How to apply a style sheet in an application
How to set the style of an application without using a style sheet
How to use a single inheritance method to create a derived class from a. ui file
How to customize a resource set file
How to make a signal and slot connect automatically
How to establish an association between two windows
The use of the Meta object System method
This program is named stylesheet, and its effect is shown in Figure 9-17.
Figure 9-17 Example Run effect
This example is based on the main window style, and some similar to the Web site registration procedures that are common to you to fill out personal data.
The program has two main menus, followed by the "File"-> "Edit Style" menu item, will pop up as shown in Figure 9-18 to set the Style Sheet dialog box, where several styles are built into the selection, the user can also enter the custom style in the edit box. When Setup is complete, the style of the window part of the main interface changes accordingly. The coffee style is customized with push button, frames, and tooltip, but uses a lower style (for example, Windows XP style) to draw the Checkbox,combobox and radio button. The Pagefold style completely redefined the appearance of all the controls used in the dialog box to achieve a unique, platform-independent look.
Figure 9-18 Style sheet editor
This program contains the following source files:
Mainwindow.ui
Stylesheeteditor.ui
Stylesheet.qrc
/qss/coffee.qss
/qss/default.qss
/qss/pagefold.qss
/images/*.png
Mainwindow.h
Mainwindow.cpp
Stylesheeteditor.h
Stylesheeteditor.cpp
The Mainwindow.ui and Stylesheeteditor.ui, respectively, are the interface layout files of the main program and the style editor, which are made using QT Designer. STYLESHEET.QRC is a resource set file that describes the location and name of the style sheet files and picture files that are used by your program. The QSs folder contains 3. qss files that describe the styles that are used in the program, and our programs will read them and convert them to style sheets. A picture file that is used in the program is placed in the Images folder. Mainwindow.h and Mainwindow.cpp form the main program class in the program, and Stylesheeteditor.h and stylesheeteditor.cpp form the style editor classes.
Here we will combine the source code for you to explain the function of this program is how to achieve.
First look at mainwindow.h.
1 #ifndef mainwindow_h
2 #define Mainwindow_h
3 #include <QtGui>
4 #include "ui_mainwindow.h"
5 class Stylesheeteditor;
6 Class Mainwindow:public Qmainwindow
{
7 Q_object
8 Public:
9 MainWindow ();
Ten Private Slots:
one void on_editstyleaction_triggered ();
void On_aboutaction_triggered ();
Private:
Stylesheeteditor *stylesheeteditor;
Ui::mainwindow Ui;
};
#endif
Lines 1th, 2, and 16th together form the predefined guardian posts for headers, which are designed to prevent repetitive definition or inclusion of header files in a program, and suggest that you follow this paradigm.
Line 3rd introduces the declaration of the Qtgui module, which contains the definition of the Qmainwindow class.
Line 4th introduces the Ui_mainwindow.h declaration.
Line 5th introduces the style editor class stylesheeteditor using a forward declaration.
Line 6th declares that the main program class MainWindow a single public inheritance from the Qmainwindow class.
The 7th row is required because the core mechanism of QT is used, such as signal/slot mechanism.
Lines 8th and 9 declare the constructor of the MainWindow class.
10–12 lines declare private slots, and their names follow the "Automatic Association Rules" of QT signals/slots.
The 第13-15 line declares private members, respectively, of the object of the style editor class and the object of the main program interface class. It can be seen from here that our introduction of the. ui file will take the form of single inheritance.
Let's take a look at the contents of the Mainwindow.cpp file.
1 #include "mainwindow.h"
2 #include "Stylesheeteditor.h"
3 Mainwindow::mainwindow ()
{
4 Ui.setupui (this);
5 Ui.namelabel->setproperty ("Class", "Mandatory Qlabel");
6 stylesheeteditor = new Stylesheeteditor (this);
7 StatusBar ()->addwidget (New Qlabel (tr ("Ready"));
8 Connect (ui.exitaction, SIGNAL (triggered ()), Qapp, SLOT (Quit ()));
9 Connect (ui.aboutqtaction, SIGNAL (triggered ()), Qapp, SLOT (ABOUTQT ()));
}
Ten void mainwindow::on_editstyleaction_triggered ()
{
One stylesheeteditor->show ();
Stylesheeteditor->activatewindow ();
}
void Mainwindow::on_aboutaction_triggered ()
{
Qmessagebox::about (This, tr ("About Style sheet"),
TR ("The <b>style sheet</b> example shows how widgets can be styled"
"Using <a href=/" http://doc.trolltech.com/4.5/stylesheet.html/">QT"
"Style Sheets</a>. Click <b>file| Edit Style sheet</b> to pops up the
"Style editor, and either choose an existing style sheet or"
"Your own.");
}
The 1th and 22 lines introduce the header file declaration used in the program.
Line 4th initializes the interface layout, and the SETUPUI () function is typically placed in the first row within the constructor.
Line 5th sets the properties of the Namelabel, and the SetProperty () method has the following prototype:
BOOL Qobject::setproperty (const char * name, const qvariant & value)
It sets the property value of the object's name to value. For this sentence, the Namelabel class attribute value is set to "mandatory Qlabel", which causes the widget to be highlighted.
Line 6th instantiates the object of the Stylesheeteditor class, whose parent window is MainWindow.
The 7th Act status bar adds a widget that displays the word "Ready", which is "ready" in Chinese.
The 8th and 9 lines are respectively connected to the trigger signal of the menu item exitaction and aboutqtaction triggered () and the Quit () slot and ABOUTQT () slot of the global object Qapp.
The 10–12 line defines the on_editstyleaction_triggered () slot function.
Line 11th Displays the Style editor window.
Line 12th calls the Activatewindow () method to set the Style editor window as the active window. The prototype of the Activatewindow () method is as follows:
void Qwidget::activatewindow ()
It is used to set a top-level window as the current active window.
Tip : the active window and how to set the active window
The active window is the visible top-level window that can currently have keyboard input focus.
The effect of the Activatewindow () method is the same as using the mouse to click the title bar of the top-level window. On X11, this effect is not certain, because it relies on the specific window manager, if you want to ensure that a window is active window, it is best to call the Activatewindow () method after calling the raise () method, which itself is QT built-in a slot function. However, before you do this, you need to make sure that the window is visible first, otherwise there will be no effect.
On the Windows platform, this situation is somewhat different. Because Microsoft does not allow one application to interrupt a user's dialog (that is, an interaction process) that was established by another application, the widget may not be the top-level active window after Activatewindow () is invoked, but its title bar becomes highlighted, To tell the user that there has been some change in the state of the window widget. This, please the reader friend in the use of attention.
If you want to know whether a window is an active window, you can call the Isactivewindow () method.
Earlier we talked about the "resource set file" in the QT core mechanism. Now let's take a look at how to write or configure a resource set file in Qt Creator.
Using the mouse to double-click the resource set file in Qt Creator opens the resource Set File editor, which allows you to add or delete files, nodes, and so on in the resource set editor, which is easy to operate and at a glance. As shown in Figure 9-19.
Figure 9-19 Resource Set File Editor
Let's take a look at the contents of the Stylesheeteditor.h file.