对于Qt而言,在实际的开发过程中, 1)开发者可能知道所要使用的类 ---- >帮助手册 —>索引 -->直接输入类名进行查找 2)开发者可能不知道所要使用的类,只知道开发需求文档 ----> 帮助 手册,按下图操作:
头文件:#include <QTime>
模块:在项目的pro文件中,查看是否包含模块:QT += core
对于QTime对象,主要包括时 分 秒 毫秒,数字时间,一个主要的功能就是,用户可以自己设置一个时间(时 分 秒 ),然后可以给这个时间来设置毫秒数,从而达到时间可以正常运行。 创建时间的对象的方式有两种:构造函数----- 指定一个时间 通过静态成员函数—currentTime() — 获得当前操作系统的本地时间 功能: 1)直接获取操作系统的时间
[static] QTime QTime::currentTime() //获取当前的系统时间,就是此时的时间,不会自动往下走
2)可以创建一个QTime对象 ,然后再指定该对象时,分,秒
QTime(int h, int m, int s = 0, int ms = 0)
QTime time(0,0,0); //可以做为秒表的功能
再通过增加毫秒的时间,增加到1000,秒针就会自动加1
QTime + QTimer
第一步:通过currentTime() ----->得到QTime对象
第二步:将QTime时间更新UI(QLabel)-----> tostring()
QString QTime::toString(const QString &format) const
—将时间对象转换成字符串,并且在转换字符串时,可以指定字符串显示的格式
字符串 | 含义 |
---|---|
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
H | the hour without a leading zero (0 to 23, even with AM/PM display) |
HH | the hour with a leading zero (00 to 23, even with AM/PM display) |
m | the minute without a leading zero (0 to 59) |
mm | the minute with a leading zero (00 to 59) |
s | the whole second, without any leading zero (0 to 59) |
ss | the whole second, with a leading zero where applicable (00 to 59) |
zzz | the fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999). |
使用如下: “hh:mm:ss
” 或者 “hh-mm-ss
”
1)QString QTime::toString(QStringView format) const
功能说明:将一个时间对象以指定格式转换成字符串
指定格式:“hh:mm:ss” 或者 “hh-mm-ss”
2)[static] QTime QTime::currentTime()
功能:获取系统当前的时间
3)
boolsetHMS(int h, int m, int s, int ms = 0)//给时间对象设置时分秒
inthour() const
intminute() constintmsec() const
QTimeaddMSecs(int ms) const// 在给定的时间基础上,增加指定的毫秒QTimeaddSecs(int s) const// 在给定的时间基础上,增加指定的秒
功能:获取系统当前的日期 创建对象的方法: 1)构造函数:QDate(int y, int m, int d) 2)静态函数:currentDate() 功能接口函数: QString QDate::toString(const QString &format) const — 将日期转换成字符串 指定格式:
字符串 | 含义 |
---|---|
d | the day as number without a leading zero (1 to 31) |
dd | the day as number with a leading zero (01 to 31) |
ddd | the abbreviated localized day name (e.g. ‘Mon’ to ‘Sun’). Uses the system locale to localize the name, i.e. QLocale::system(). |
dddd | the long localized day name (e.g. ‘Monday’ to ‘Sunday’). Uses the system locale to localize the name, i.e. QLocale::system() |
M | the month as number without a leading zero (1 to 12) |
MM | the month as number with a leading zero (01 to 12) |
MMM | the abbreviated localized month name (e.g. ‘Jan’ to ‘Dec’). Uses the system locale to localize the name, i.e. QLocale::system(). |
MMMM | the long localized month name (e.g. ‘January’ to ‘December’). Uses the system locale to localize the name, i.e. QLocale::system(). |
yy | the year as two digit number (00 to 99) |
yyyy | the year as four digit number. If the year is negative, a minus sign is prepended in addition. |
比如:"yyyy/MM/dd"
ui->dateLabel->setText(QDate::currentDate().toString("yyyy-MM-dd"));
qDebug() << QDate::currentDate().year(); // 获得当前的年份
qDebug() << QDate::currentDate().month(); // 获得当前的月数
qDebug() << QDate::currentDate().dayOfWeek(); //这周的第几天
对于对话框的功能,在GUI图形界面开发过程,使用是非常多,那么Qt也提供了丰富的对话框类。 The QDialog class is the base class of dialog windows,QDialog 是所有对话框的基类,对话框的框架类图如下:
该类主要来获得一个颜色(The QColorDialog class provides a dialog widget for specifying colors)
通过该类的静态成员函数(The static getColor() function shows the dialog),打开的对话框来选择
一个颜色。
头文件:#include<QColorDialog>
[static] QColor QColorDialog::getColor(const QColor &initial = Qt::white,
QWidget *parent = nullptr, const QString &title = QString(),
QColorDialog::ColorDialogOptions options = ColorDialogOptions())
功能:得到一个颜色(QColor) 参数说明:
const QColor &initial = Qt::white
---- 默认一个初始化的颜色,一般情况下,默认即可QWidget *parent = nullptr
---- 这个参数说明,跟对话框(QColorDialog)之间关系,
该对话框的父部件,设置为nullptr,则说明对话框没有父部件,对话框跟界面是没有关系
两个都是独立存在,如果想要设置的话,通常设置为 this(通常为界面类对象)const QString &title = QString()
---- 对话框的标题QColorDialog::ColorDialogOptions options = ColorDialogOptions()
对话框的颜色选项,类似对话框皮肤的设置返回值: QColor ----- RGB
参数 | 含义 |
---|---|
int | green() const |
int | blue() const |
int | red() const |
案例:设置一个控件的背景色
void ColorWindow::on_colorSetButton_clicked()
{
//[1]打开颜色对话框,并选择一个颜色
QColor color = QColorDialog::getColor();
//[2] 将颜色值分别获取它的rgb
int red = color.red();
int green = color.green();
int blue = color.blue();
//[3] 将rgb设置给控件
ui->testLabel->setStyleSheet(QString("background-color: rgb(%1, %2, %3);").arg(red)
.arg(green)
.arg(blue));
}
让用户来选择一个字体(The QFontDialog class provides a dialog widget for selecting a font)
头文件:#include<QFontDialog>
函数原型:
[static] QFont QFontDialog::getFont(bool *ok, const QFont &initial,
QWidget *parent = nullptr, const QString &title = QString(),
QFontDialog::FontDialogOptions options = FontDialogOptions())
功能:得到一个字体对象(QFont) 参数说明:
bool *ok
----> 对获得字体结果,成功返回true,否则,返回 falseconst QFont &initial
----> 初始化字体QWidget *parent = nullptr
----> 字体对话框的父部件,一般可以传this,或者不传const QString &title = QString()
----> 对话框标题QFontDialog::FontDialogOptions options = FontDialogOptions()
— 可选项,默认即可bool ok;
QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
if (ok) {
// font is set to the font the user selected
} else {
// the user canceled the dialog; font is set to the initial
// value, in this case Times, 12.
}
案例:给一个控件设置字体
void FontDialogWin::on_fontSetButton_clicked()
{
//[1] 打开对话框
bool ok;
QFont font = QFontDialog::getFont(&ok,this);
if(ok)
{
qDebug() << "ok = " << ok << font.toString();
//[2] 如果获得成功,则将font设置给指定的控件
ui->label->setFont(font);
}else {
qDebug() << " get font failed";
}
}
打开文件对话框,可以让用户选择一个文件或者目录(路径),并不能打开文件中的内容,有了文件的路径,就可以对文件进行操作(打开 读 写 关闭等操作)
(The QFileDialog class provides a dialog that allow users to select files or directories)
头文件:#include <QFileDialog>
静态函数的原型:
参数 | 含义 |
---|---|
QString | getExistingDirectory(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), QFileDialog::Options options = ShowDirsOnly) |
QString | getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options()) |
函数功能:得到一个文件的路径 参数说明:
它经常结合QFile来进行读或者写文件
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open File"),/*tr()---国标化标准*/
"/home", //绝对路径
tr("Images (*.png *.xpm *.jpg)"));
如果要设置过滤器,类型之间使用";;",比如:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
具体用法:
void FileDialogWin::on_browseButton_clicked()
{
//[1]打开文件对话框
QString filePath = QFileDialog::getOpenFileName(this,"打开文件",
"D:/Qt/qt_gz2057_workspace/DAY4/cpp_demo6_qfiledialog",
"Text files (*.cpp *.h)");
//[2] 将文件路径更新UI
ui->fileEdit->setText(filePath);
//[3]根据文件路径打开此文件
}
指定的路径都是以Linux路径的标准来执行
该类可以用来读写文件的数据(The QFile class provides an interface for reading from and writing to files
)
头文件:#include <QFile>
QFile操作文件流程:
QFile(const QString &name)
QFile()和void setFileName(const QString &name)
bool open(QIODevice::OpenMode mode) override
QByteArray QIODevice::readAll() ----> 一次性读取文件中所有的数据,数据以
QByteArray(字节数组)
具体的使用方法:
void FileDialogWin::on_browseButton_clicked()
{
//[1]打开文件对话框
QString filePath = QFileDialog::getOpenFileName(this,"打开文件",
"D:/Qt/qt_gz2057_workspace/DAY4/cpp_demo6_qfiledialog",
"Text files (*.cpp *.h)");
//[2] 将文件路径更新UI
ui->fileEdit->setText(filePath);
//[3]根据文件路径打开此文件
QFile file(filePath);
//[4] 打开文件 --- open
if(file.open(QIODevice::ReadOnly))
{
QByteArray array = file.readAll();
qDebug() << "ARRAY= " << array;
}
file.close();
}
void SaveFileWin::on_saveButton_clicked()
{
//[1] 打开另存为对话框,并手动设置文件的名字
QString filename = QFileDialog::getSaveFileName(this);
//[2] 创建文件和打开文件
QFile file(filename);
if(file.open(QIODevice::WriteOnly))
{
//[3] 获得UI控件上的内容,再将它写入到文件中
QString data = ui->textEdit->toPlainText();
//QString ----> QByteArray ---- QString::toUtf8() const : QByteArray
file.write(data.toUtf8());
}
file.close();
}