• Qt学习20 Qt 中的标准对话框(中)


    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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 输入对话框的输入模式
      • 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 // WIDGET_H
    
    • 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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    小结

    • QColorDialog类用于提供指定颜色的对话框部件
    • QColor类用在程序中表示颜色的概念
    • QInputDialog类用于需要临时进行数据输入的场合
  • 相关阅读:
    港联证券:综合施策提振信心 资本市场新一轮深化改革拉开帷幕
    c++函数模板与类模板
    深入理解隔离性(MVCC,快照,undo log,Read View)
    基于Java+SpringBoot+Vue前后端分离失物招领平台设计和实现
    晶振与晶体
    【从零开始学习 SystemVerilog】3.8、SystemVerilog 控制流—— Tasks(任务)
    k8s 命令提示
    感觉 C++ 很简单,但为何这么多劝退的?
    【左神算法笔记】Class1:异或交换,时间复杂度计算
    开发应用智能猫砂盆实战案例
  • 原文地址:https://blog.csdn.net/weixin_40743639/article/details/125567720