第20课 - Qt 中的标准对话框(QColorDialog和QInputDialog)

1、颜色对话框 

Qt中提供了预定义的颜色对话框QColorDialog类 

QColorDialog类用于提供指定颜色的对话框部件 

 

颜色对话框的使用方式 

Qt中的QColor类用来在程序中表示颜色的概念 

QColor类同时支持多种颜色表示方式

        - RGB: 以红,绿,蓝为基准的三色模型 

        - HSV: 以色调,饱和度,明度为基准的六角锥体模型 

        - CMYK: 以天蓝,品红,黄色,黑为基准的全彩印刷色彩模型 

 

QColorDialog中的实用函数 

             - QColorDialog: : getColor 

 

2、输入对话框 

Qt中提供了预定义的输入对话框QlnputDialog类 

QlnputDialog类用于需要临时进行数据输入的场合 

 

输入对话框的使用方式 

 

输入对话框的输入模式 

         QinputDialog::TextInput -输入文本字符串

         QinputDialog::IntInput -输入整形数 

         QinputDialog::DoubleInput    - 输入浮点数 

 

QlnputDialog中的实用函数 

         - QinputDialog: : getDouble 

         - QinputDialog: :getInt 

         - QinputDialog:: getItem 

         - Qinpu tDialog: : getText 

 

3、编程实验

颜色、输入对话框使用实例    20-1.pro 

Widget.h

#ifndef _WIDGET_H_
#define _WIDGET_H_

#include <QtGui/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

Widget.cpp

#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()));
}

void Widget::ColorDialogBtn_Clicked()
{
    QColorDialog dlg(this);

    dlg.setWindowTitle("Color Editor");
    dlg.setCurrentColor(QColor(100, 111, 222)); //自定义  可以直接dlg.setCurrentColor(QColor::blue);

    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();
    }
}

void Widget::InputDialogBtn_Clicked()
{
    QInputDialog dlg(this);

    dlg.setWindowTitle("Input Test");
    dlg.setLabelText("Please input an integer:");
    dlg.setInputMode(QInputDialog::TextInput);


    if( dlg.exec() == QInputDialog::Accepted )
    {
        qDebug() << dlg.textValue();
    }
}

Widget::~Widget()
{
    
}

main.cpp

#include <QtGui/QApplication>
#include "Widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;

    w.show();
    
    return a.exec();
}

4、小结 

QColorDialog类用于提供指定颜色的对话框部件 

QColor类用来在程序中表示颜色的概念 

QlnputDialog类用于需要临时进行数据输入的场合 

 

 

猜你喜欢

转载自blog.csdn.net/qq_39654127/article/details/81409527