qt Window Skin Change

Source: Internet
Author: User

1. Related articles

Qt Resource System
Use of the QT MOC,UIC,RCC command


2. Overview
Two years of graduation, has been using the Qt interface Library to develop the program, using VS08, 10, 13 and other development tools, and installed QT plug-in, recently in the client to do a skin-changing function, so on the QT qrc do a bit of research, I am a pragmatic faction (may be a bit virtual), relative to the document. At the beginning of this article I gave two blog, these two blog for I understand QRC this thing has a lot of help, next I will briefly analyze my understanding.

First of all, the next qrc is the QT, but not the VS, it is easy to prove that the VS engineers do not recognize the QRC file, if you need to use QRC file, then we need to manually create a txt, and then change the suffix and add to the VS project. QRC files added to vs project if we're going to open it, Qt comes with QT resource editor,1 shown. And we use QRC also have many ways, this article will mention in two ways (do not know the third kind of not), one is to use the QRC file with RCC compiled into Qrc_xxx.cpp file, this file with a static C + + compression binary array containing all the pictures and other resources of the data The other is to compile the QRC file into an RCC binary file (rcc-binary r.qrc-o R.RCC) with RCC.

Figure 1

3. Skin-changing comprehension
Pave the QRC of things, I first said that my understanding of the skin, may be biased, but it is personal opinion, Daniel do not spray.
QT program skin, personal understanding can be modified from these two points: rgb+png, I do not seem to understand, in fact, quite good understanding, then I will say my own skin changing process
RGB Replacement
The so-called RGB replacement is a simple color replacement, which may be a skin change that most of us will use. In fact, the principle is very simple, is to put all the stylesheet description is taken out, and then put into a qss text file, the file is added to the QRC file, the content of QRC will eventually be compiled into binary data for our program to use, the way to compile the previous article mentioned in the two ways.
Load the QSs file and use the following to the global code:

1 QFile file (filename);2 if(!File.Open (qfile::readonly))3 {4     return;5 }6 7Qtextstreaminch(&file);8 9 inch. Setcodec ("UTF-8");TenQString QSS =inch. ReadAll (); One  AQapp->setstylesheet (QSS);
View Code

The above parameter of this method is the QSs file in the QRC resource path, you can use this method at any time to do RGB skin.

About RGB skinning, I also gave a suggestion when I wrote the code.
1, the use of setstylesheet in the process can not be combined with logical code
2, each GUI class only uses 1 times setstylesheet, all the settings are placed in the constructor function as far as possible, facilitates later searches
3. For each widget that may need to be replaced, it needs to be named with the Setobjectname method, and the name cannot be duplicated with other control names
4, when using the Setstylesheet method, note the format, the recommended format is: class#objectname{}
Of course, after you know the QSS syntax, you'll find that the fourth way is just a simpler class selector, and if you're familiar with QSS syntax, you can use a more advanced approach, 2, which is an example of a QSS file.

Figure 2


Speaking of QSS's grammar, I give a qss pit: When the class attribute is found to change the need to reset QSS,QT QSS blog post also has a corresponding explanation, although this blog is my turn, but it has to join my own understanding.
About QSS style Reset I wrote a common method.

 1  void  resetstylesheet (qwidget *< Span style= "color: #000000;" > widget)  2  { 3  QString sheetstr = Widget->stylesheet ();  4  widget->setstylesheet ( ); 5  widget->setstylesheet (SHEETSTR);  6 } 

Picture resource substitution
Picture resource substitution I only give my own test conclusion, the more theoretical knowledge can refer to my first article
Mode 1: Hit the QRC into the DLL, this is relatively simple when using VS, just need to add QRC to the VS project.
Method 2: Compile the QRC into an RCC file using the RCC command: Rcc-binary r.qrc-o R.RCC

4, regardless of which way to use, a few four methods are very useful
Q_init_resource (filename);//Load a QRC file
Q_cleanup_resource (filename);//clean up a QRC file, use the VS mode to load the QRC file, you can use this method to clean up
Qresource::registerresource ("FILENAME.RCC");//Register an RCC file, which is compiled with QRC files, with the same results as Q_init_resource
Qresource::unregisterresource ("FILENAME.RCC");//anti-registration of an RCC file, with the same results as Q_cleanup_resource

qt Window Skin Change

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.