当前位置:网站首页>Qt学习20 Qt 中的标准对话框(中)
Qt学习20 Qt 中的标准对话框(中)
2022-07-03 13:23:00 【一个小黑酱】
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 // WIDGET_H
#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;
}
#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类用于需要临时进行数据输入的场合
边栏推荐
- Record 405 questions about bank callback post request
- Golang — template
- MyCms 自媒体商城 v3.4.1 发布,使用手册更新
- 如何使用lxml判断网站公告是否更新
- Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
- Use and design of Muduo buffer class
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- Field problems in MySQL
- [技術發展-24]:現有物聯網通信技術特點
- [bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
猜你喜欢
太阳底下无新事,元宇宙能否更上层楼?
Go language unit test 3: go language uses gocovey library to do unit test
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
Bidirectional linked list (we only need to pay attention to insert and delete functions)
Libuv库 - 设计概述(中文版)
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
SQL Injection (AJAX/JSON/jQuery)
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
双向链表(我们只需要关注插入和删除函数)
Ocean CMS vulnerability - search php
随机推荐
Libuv Library - Design Overview (Chinese version)
GoLand 2021.1: rename the go project
Flutter dynamic | fair 2.5.0 new version features
PHP maze game
Leetcode-1175. Prime Arrangements
php 迷宫游戏
Flutter dynamic | fair 2.5.0 new version features
Students who do not understand the code can also send their own token, which is easy to learn BSC
Mastering the cypress command line options is the basis for truly mastering cypress
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
栈应用(平衡符)
Ocean CMS vulnerability - search php
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
Implementation of Muduo accept connection, disconnection and sending data
The network card fails to start after the cold migration of the server hard disk
常见的几种最优化方法Matlab原理和深度分析
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
项目协作的进度如何推进| 社区征文
KEIL5出现中文字体乱码的解决方法