In QT, if you use qstyle to customize cross-platform interface controls

Source: Internet
Author: User

We often encounter requirements for custom interface controls. If it is only on a platform, for example, your control only needs to be displayed on Windows, it would be nice to do it. It would be nice to hard code your look and feel. However, if the interface needs to display the Native Interface style on different platforms, such as Windows and Mac, do I need to write different code for each platform? In addition, if you want to modify the overall interface style on the qt platform, and your program also needs to be cross-platform, then you may need to derive the qstyle objects on the platform for each platform (qwindowsxpstyle, qvistastyle, and qmacstyle on MAC ). After the modification, you must apply the modification to other platforms. Is there a better solution?

 

The answer is yes.
First you can refer to this somewhat old article "http://doc.trolltech.com/qq/qq09-q-and-a.html#style

", Using proxy pattern. Create a proxy and define a style instance pointing to the current platform, so that you can easily make some customization. However, the process of creating a proxy may be cumbersome, and it also needs to be maintained based on the qstyle update later. Proxy is a widely used pattern in QT. If you are not familiar with the proxy pattern, you can go to the Wiki for a brief description. Of course, this method also has some problems. The biggest problem is that when calling the function of the proxy style, for example, myproxystyle: drawcontrol will call qwindowxpstyle: drawcontrol, but before drawcontrol returns, the latter will always only call the functions in qwindowxpstyle, so the Customize behavior in my proxy cannot be called. In fact, this problem has always existed, and it still exists. How can this problem be solved?

 

Solution 1: The proxy mode is still used,
For example, "Here

. Assume that the current requirement is to underline all shortcuts. The method is as follows:

First, create a porxystyle, but it is not a subclass of qstyle. Do you think this is wrong? How can I create a new style without deriving from qstyle? How much work is required? But please be patient. The proxystyle code is as follows:

Class proxystyle <br/>{< br/> Public: <br/> proxystyle (const qstring & basestyle) {style = qstylefactory: Create (basestyle );} <br/> int proxystylehint (qstyle: stylehint hint, const qstyleoption * option = 0, <br/> const qwidget * widget = 0, qstylehintreturn * returndata = 0) const <br/>{< br/> If (hint = qstyle: sh_underlineshortcut) <br/> return 1; <br/> return style-> stylehint (hint, option, widget, returndata); <br/>}< br/> PRIVATE: <br/> qstyle * style; <br/> };

Then we can implement the custom style as follows:

# Define addstylesubclass (basestyleclass, basestylename) <br/> class my # basestyleclass: Public basestyleclass, public proxystyle <br/>{< br/> public: <br/> my # basestyleclass (): basestyleclass (), proxystyle (basestylename) {}< br/> int stylehint (stylehint hint, const qstyleoption * option = 0, const qwidget * widget = 0, qstylehintreturn * returndata = 0) const <br/> {<br/> return proxystylehint (hint, option, widget, returndata ); <br/>}< br/> };

In this way, you can use the following macro to easily define sytle on different platforms. If the style in Windows 7 appears in QT next time, you only need to add a line of code to solve all the problems:

Addstylesubclass (qcleanlooksstyle, "cleanlooks"); <br/> addstylesubclass (qplastiquestyle, "Plastique"); <br/> addstylesubclass (qmotifstyle, "motif "); <br/> addstylesubclass (qwindowsstyle, "Windows"); <br/> addstylesubclass (qcdestyle, "CDE ");

 

Solution 2: "dynamic" inheritance

The method of dynamic inheritance is very simple, and you will see it. It mainly utilizes the features of templates. You can write your new style as follows:

Template <typename T> <br/> class myqstyle: Public T <br/>{< br/> Public: <br/> myqstyle (): T () {}< br/> ~ Myqstyle () {}< br/> virtual void drawitemtext (...) {... } <Br/> virtual int pixelmetric (...) {... } <Br/>... <Br/>}; <br/> qstyle * get_application_style (const qstring & style_name) <br/>{< br/> If (style_name = "Windows ") {<br/> return New myqstyle <qwindowsxpstyle>; <br/>} else if (style_name = "Mac") {<br/> return New myqstyle <qmacstyle>; <br/> }... <Br/> cerr; <br/>}

See it. Is it very convenient? Dynamic inheritance does not use proxy, so it reduces a lot of unnecessary code, but at the same time, dynamic inheritance also has its disadvantages: it must process different qwindowsxpstyle through some special cases of the template, compilation problems caused by different function names in qmacstyle. However, I still prefer the dynamic inheritance method.

 

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.