Qt学习20 Qt 中的标准对话框(中)
颜色对话框
- Qt中提供了预定义的颜色对话框QColorDialog类
- QColorDialog类用于提供指定颜色的对话框

QColorDialog dlg(this);
dlg.setWindowTitle("Color Editor");
dlg.setCurrentColor(Qt::red);
if (dlg.exec() == QColorDialog::Accepted) {
qDebug() << dlg.selectedColor();
}
- Qt中的QColor类用来在程序中表示颜色的概念
- QColor类同时支持多种颜色表示方式
- RGB:以红,绿,蓝为基准的三色模型
- HSV:以色调,饱和度,明度为基准的六角锥体模型
- CMYK:以天蓝,品红,黄色,黑为基准的全彩印刷色彩模型
输入对话框
- Qt中提供了预定义的输入对话框QInputDialog类
- QInputDialog类用于需要临时进行数据输入的场合

QInputDialog dlg(this);
dlg.setWindowTitle("Input ...");
dlg.setLabelText("Please enter a integer: ");
dlg.setInputMode(QInputDialog::IntInput);
if (dlg.exec() == QInputDialog::Accepted)
{
qDebug() << dlg.intValue();
}
- 输入对话框的输入模式
- QInputDialog::TextInput 输入文本字符串
- QInputDialog::IntInput 输入整形数
- QInputDialog::DoubleInput 输入浮点数
代码实验
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton ColorDialogBtn;
QPushButton InputDialogBtn;
private slots:
void ColorDialogBtn_Clicked();
void InputDialogBtn_Clicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
#include "Widget.h"
#include <QDebug>
#include <QColorDialog>
#include <QInputDialog>
Widget::Widget(QWidget *parent)
: QWidget(parent), ColorDialogBtn(this), InputDialogBtn(this)
{
ColorDialogBtn.setText("Color Dialog");
ColorDialogBtn.move(20, 20);
ColorDialogBtn.resize(160, 30);
InputDialogBtn.setText("Input Dialog");
InputDialogBtn.move(20, 70);
InputDialogBtn.resize(160, 30);
resize(200, 120);
setFixedSize(200, 120);
connect(&ColorDialogBtn, SIGNAL(clicked()), this, SLOT(ColorDialogBtn_Clicked()));
connect(&InputDialogBtn, SIGNAL(clicked()), this, SLOT(InputDialogBtn_Clicked()));
}
Widget::~Widget()
{
}
void Widget::ColorDialogBtn_Clicked()
{
QColorDialog dlg(this);
dlg.setWindowTitle("Color Editor");
dlg.setCurrentColor(Qt::red);
if (dlg.exec() == QColorDialog::Accepted) {
QColor color = dlg.selectedColor();
qDebug() << color;
qDebug() << color.red();
qDebug() << color.green();
qDebug() << color.blue();
qDebug() << color.hue();
qDebug() << color.saturation();
qDebug() << color.value();
}
QColor c = QColorDialog::getColor(Qt::red, this, "Color Edit");
qDebug() << c;
}
void Widget::InputDialogBtn_Clicked()
{
QInputDialog dlg(this);
dlg.setWindowTitle("Input Test");
dlg.setLabelText("Please input a string:");
dlg.setInputMode(QInputDialog::TextInput);
if (dlg.exec() == QInputDialog::Accepted) {
qDebug() << dlg.textValue();
}
qDebug() << QInputDialog::getDouble(this, "Input double", "Please input double:");
qDebug() << QInputDialog::getInt(this, "Input int", "Please input int:");
QStringList ql;
QStringList items;
items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter");
bool ok;
QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"),
tr("Season:"), items, 0, false, &ok);
qDebug() << item;
QString str = QInputDialog::getText(this, "getText", "Please input text:");
qDebug() << str;
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
小结
- QColorDialog类用于提供指定颜色的对话框部件
- QColor类用在程序中表示颜色的概念
- QInputDialog类用于需要临时进行数据输入的场合