当前位置:网站首页>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类用于需要临时进行数据输入的场合
边栏推荐
- Flutter dynamic | fair 2.5.0 new version features
- 记录关于银行回调post请求405 问题
- CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
- Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
- Several common optimization methods matlab principle and depth analysis
- MySQL functions and related cases and exercises
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- Windos creates Cordova prompt because running scripts is prohibited on this system
猜你喜欢

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

The principle of human voice transformer

太阳底下无新事,元宇宙能否更上层楼?

RichView TRVStyle ListStyle 列表样式(项目符号编号)
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

Unity embeddedbrowser browser plug-in event communication
![[sort] bucket sort](/img/52/95514b5a70cea75821883e016d8adf.jpg)
[sort] bucket sort

Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market

Unity EmbeddedBrowser浏览器插件事件通讯
随机推荐
Libuv Library - Design Overview (Chinese version)
[sort] bucket sort
MySQL installation, uninstallation, initial password setting and general commands of Linux
Halcon combined with C # to detect surface defects -- Halcon routine autobahn
Software testing is so hard to find, only outsourcing offers, should I go?
JS 将伪数组转换成数组
MySQL 数据增删改查综合案例
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
SQL Injection (GET/Select)
Sequence table (implemented in C language)
php 迷宫游戏
Implementation of Muduo accept connection, disconnection and sending data
Use and design of Muduo buffer class
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
Go language unit test 4: go language uses gomonkey to test functions or methods
TensorBoard可视化处理案例简析
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Several common optimization methods matlab principle and depth analysis
The reasons why there are so many programming languages in programming internal skills
Heap structure and heap sort heapify