Case study of QT personalized forms without borders, transparency, mobility, and

Source: Internet
Author: User

This article from http://blog.csdn.net/yiyaaixuexi/article/details/6362865

There is also a good blog: http://devbean.blog.51cto.com/448512/d-8

Many of my friends asked me how to do the transparent effect. Why do my borderless forms cannot be moved? I was tired of answering them one by one. I just wrote them and shared them. I only used the code to speak.

Main. cppint main (INT argc, char * argv [])
{
Qapplication: setstyle ("cleanlooks ");
Qapplication A (argc, argv );
Login W;
W. setwindowtitle ("clientlogin ");
W. setwindowopacity (1 );
W. setwindowflags (QT: framelesswindowhint );
W. setattribute (QT: wa_translucentbackground );
W. Show ();
W. Move (1, 200,100 );
Return a.exe C ();
}

The key statement is

W. setwindowopacity (1 );
W. setwindowflags (QT: framelesswindowhint );
W. setattribute (QT: wa_translucentbackground );

 

If you do not know what these statements mean, Press F1 or directly view the help documentation ...... The settings for the form without borders should be written in the main, so that all derived subwindows, qdialog, and qwidget can be inherited, well planned and managed to facilitate unified beautification design. Take a chat window in the project as an example. First, use psto create a background image of the Form. Note that it is stored in PNG format, which is the key to transparency. No ps. You can find some PNG resource images. My PNG transparent background image is:

 

 

Add it to your resource package and set it as the background of the form. It's my project, and the scenario settings are actually the background image of the new project ~~ You can preview the transparent borderless form, but there is another important problem that the window cannot be moved. This is also caused by no borders ...... I will not elaborate on the specific reasons. It is very clear to search for them. I will only talk about the solution. In each subwindow, add: void yourwindow: mousepressevent (qmouseevent * event) {This-> windowpos = This-> pos (); this-> mousepos = event-> globalpos (); this-> DPOs = mousepos-windowpos;} void yourwindow: mousemoveevent (qmouseevent * event) {This-> move (Event-> globalpos ()-This-> DPOs);} void yourwindow: changeevent (qevent * E) {qmainwindow: changeevent (E ); switch (e-> type () {Case qevent: languagechange: UI-> retrans Lateui (this); break; default: break;} This is a big success. Run it to see the effect. The green forest is a desktop, which can be ignored. Welcome to use QT to make a nice window program http://blog.163.com/lei_yuming/blog/static/4455865620118239916277/

| Font size subscription

Many people first wanted to create a beautiful window when using QT. Although they searched for a lot of information on the Internet, they still felt that they had no choice but to give up many times. Then I thought that QT was just like that. Why should I do it? Let's go with him. In fact, through QT, we can make a very beautiful window in a very short time. Next we will work with you to create a beautiful login window.

First, let's make a final conclusion. This is a beta version of the logon interface in the early days of cloud word. If you like cloud word, you can pay attention to it (here is the portal ):


To create the preceding window, we create a GUI project whose base class is qwidget. After setting the window size, place the controls in the corresponding positions, as shown in:

The widget size is set based on the actual image size. In this dialog box, "Registration" and "help" are made of buttons.

After the controls are arranged, we create a resource file and add the prepared images to the resource file. Then we can start to write the window style.

QPushButton{    border-image: url(:/Image/Login/close1.png); }  QPushButton:hover{    border-image: url(:/Image/Login/close2.png); }  QPushButton:pressed{         border-image: url(:/Image/Login/close3.png); } 

I used a qlabel control and filled the entire window as the background. Then I wrote the following sentence in the qlabel style table: "border-image: URL (: /image/login/login_bg.png); ", which immediately becomes the following.

Next, I started to process the four buttons. Because the code format is the same, a style script is listed as follows:

The "hover" and "pressed" in the script are two pseudo states of the button.
Finally, process "Registration" and "help ":

QPushButton{     background-color: rgba(0,0,0,0);    border: 0px;    color: steelblue;    border-image:"";}QPushButton:hover    color: orange;}

Now a beautiful login box is ready. You can't wait to press F5, a very tangled login box. That's because you have missed something, that is, write this-> setwindowflags (QT: framelesswindowhint | QT: windowstaysontophint) in your window class.

Then you will find that your window cannot be moved, and all kinds of details are to be continued.

PS: in fact, the main purpose of a good interface is patience. Many times we need to tangle with each pixel. A pixel difference will bring us a different feeling.

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.