QT Learning Pathway (qcolordialog): QT Standard dialog box

Source: Internet
Author: User
Tags documentation getcolor

Continue to talk about Qt's standard dialog box, this time talking about Qcolordialog. This is the Color selection dialog box provided by QT.

Using Qcolordialog is also very simple, QT provides the GetColor () function, similar to the Qfiledialog GetOpenFileName (), can directly obtain the selected color. Let's use the previous qaction to test this function:

QColor color = QColorDialog::getColor(Qt::white, this);
QString msg = QString("r: %1, g: %2, b: %3").arg(QString::number(color.red()), QString::number(color.green()), QString::number(color.blue()));
QMessageBox::information(NULL, "Selected color", msg);

Don't forget to include Qcolordialog Oh! This code is small, but it's not very small.

The first line Qcolordialog::getcolor () calls the Qcolordialog static function GetColor (). This function has two parameters, the first is the Qcolor type, the color that is selected by default when the dialog box is opened, and the second is its parent.

The second line is longer and involves the use of qstring. If I remember correctly, these usages have not been mentioned, the spirit of "useful to say" principle, although these and qcolordialog irrelevant, here is an explanation. QString ("R:%1, g:%2, B:%3") created a QString object. We used parameterized strings, which are those of%1. In the Java properties file, character parameters are implemented with {0}, {1}. In fact, these are all placeholders, that is, the following will be replaced with other strings of these values. The substitution of placeholders requires the qstring arg () function. This function returns its caller, so you can use a chained invocation to type. It replaces the placeholder in order. Then there is the Qstring::number () function, which is also a static function of qstring, which is to replace the int and double equivalent with the qstring type. Here is the Qcolor R, G, b Three values out of the output. Regarding the Qstring class, we will explain in detail later.

The third line is simpler, using a message dialog box to output the string just stitched up.

Now it's time to run this test program. It looks simple, doesn't it?

Qcolordialog There are other functions that can be used.

Qcolordialog::setcustomcolor () You can set the user custom color. This function has two values, the first is the index of the custom color, the second is the RGB value of the custom color, the type is QRGB, you can consult the API document to see the use of this class, the following only gives a simple use of the hair:

QColorDialog::setCustomColor(0, QRgb(0x0000FF));

GetColor () also has an overloaded function with the following signature:

QColorDialog::( const QColor & initial, QWidget * parent, const QString & title, ColorDialogOptions options = 0 )

The first argument, like the previous one, is the default selected color when the dialog box is opened initial;

The second argument, parent, sets up the parents of the dialog box;

Third argument title, set the title of the dialog box;

The fourth parameter options, is the qcolordialog::colordialogoptions type, you can set some properties of the dialog box, such as whether to display alpha values, and so on, the specific properties please consult the API documentation. In particular, these values can be manipulated using or.

Qcolordialog relatively simple, API documentation is also very detailed, you encounter problems can be consulted on the document Oh!

Source: http://devbean.blog.51cto.com/448512/214164

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.