Qt programming 18: Use of the QT palette qpalette

Source: Internet
Author: User

The qpalette class has two enumeration types,

Enumeration

1. colorgroup

Corgroup refers to different States in 3 (when to set the color ):

1> active: gets the focus status.

2> inactive: status in which no focus is obtained.

3> Disable: unavailable.

Normally, the active status and inactive status show the same color. You can also set a different color as needed.

 

2. colorrole (set the color ).
Colorrole refers to the color topic, that is, the color classification of different parts in the form. For example, qpalette: Window refers to the background color and qpalette: windowtext refers to the foreground color.

Frequently used:

Qpalete: window, usually the background color of the window part;
 Qpalette: windowtext, usually the foreground color of the window that does not exist (qlabel can also be used );
 Qpalette: base indicates the background color of text input window components (such as qtextedit, qlinedit, and qplaintextedit ).
 Qpalette: Text, used together with qpalette: Base, refers to the foreground color of the text input window part;
 Qpalette: button, which indicates the background color of the Button widget;
Qpalette: buttontext, which refers to the foreground color of the Button widget.

Qpalette: highlight: used to set the background color when the text is highlighted

Qpalette: highlightedtext: used to set the color when the text is highlighted

Qpalette ::Link: hyperlink text color

Qpalette ::Linkvisted: The color of the hyperlink text after access

 

(Currently, it is rarely used, but can be used directly .)

Qpalette ::Light: Brighter than button

 Qpalette: midlight: brightness between light and button

Qpalette: Dark:

Qpalette ::Mid

Qpalette: Shadow

For example:

 
// Set to automatically fill in the background color UI-> label-> setautofillbackground (true); // set the background color to palette: Shadow UI-> label-> setbackgroundrole (qpalette :: shadow); UI-> label2-> setautofillbackground (true); UI-> label2-> setbackgroundrole (qpalette: Light)


Display Effect:

 

// Configure //----------------------------------------------------------------------------------------------------------------How to set the color:

Qpalette: setcolor () sets the color for a colorole.
Qpalette: setbrush ()Set the color for a colorole,You can set images.

 

Note: When setting the background color,Call setautofillbackground (true) first ).

 

Button use palette

// Use palette ---------- on the button to set the button font UI-> toolbt-> setfont (qfont ("", 20, qfont: bold )); UI-> toolbt-> setautoraise (true); UI-> toolbt-> setautofillbackground (true); qpalette PL = UI-> toolbt-> palette (); // set the button text color Pl. setcolor (qpalette: buttontext, qcolor (QT: Red); // use setbrush to set the image Pl. setbrush (qpalette: button, qbrush (qpixmap (":/New/resources/otherpage/tap_bg.png"); UI-> toolbt-> setpalette (PL );


Effect:

 

Qtextbrowser or qtextedit use a color palette (text edit box)

// Set part text attributes UI-> plaintextedit-> setfont (qfont ("", 20, qfont: bold )); // UI with effects-> textbrowser-> setfont (qfont ("", 20, qfont: bold); // no effect (qtextbrowser has the text editing function) qfont font = UI-> textbrowser-> font (); font. setcapitalization (qfont: alluppercase); UI-> textbrowser-> setfont (font); // effects // construct qpalette object qpalette P2; // set the background color of the text editing box p2.setcolor (qpalette: Base, qcolor (QT: Gray); // set the text color p2.setcolor (qpalette: text, qcolor (QT: Red); // sets the text background color (active) p2.setcolor (qpalette: active, qpalette: highlight, qcolor (QT:: White); // set the color of the text when the text editing box is highlighted (active) p2.setcolor (qpalette: active, qpalette: highlightedtext, qcolor (QT: Blue )); // to keep the text status consistent (nice looking), set the color p2.setcolor (qpalette: inactive, qpalette: highlight, qcolor (QT: Gray) when the focus is lost )); p2.setcolor (qpalette: inactive, qpalette: highlightedtext, qcolor (QT: Red); // apply focus to the control UI-> plaintextedit-> setpalette (P2 ); UI-> textbrowser-> setpalette (P2 );

Effect:



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.