您的位置:首页 > 娱乐 > 八卦 > 自己开发一个app需要什么_西安网站推广哪家稳定_今日热点新闻事件摘抄50字_seo是什么的

自己开发一个app需要什么_西安网站推广哪家稳定_今日热点新闻事件摘抄50字_seo是什么的

2025/9/20 12:45:42 来源:https://blog.csdn.net/m0_69093426/article/details/143639622  浏览:    关键词:自己开发一个app需要什么_西安网站推广哪家稳定_今日热点新闻事件摘抄50字_seo是什么的
自己开发一个app需要什么_西安网站推广哪家稳定_今日热点新闻事件摘抄50字_seo是什么的

Qt坐标类时间类

  • 位置和尺寸
    • QPoint
    • QLine
      • 案例
    • QSize
    • QRect
  • 日期和时间
    • QDate
      • 日期对象格式化
        • 案例
    • QTime
      • 时间对象格式化
        • 案例
    • QDataTime
      • 案例

位置和尺寸

QPoint

QPoint类封装了我们常用用到的坐标点 (x, y)
在这里插入图片描述

QLine

QLine是一个直线类, 封装了两个坐标点 (两点确定一条直线)
在这里插入图片描述

案例

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QLine line(QPoint(100,200),QPoint(150,210));QLine newLine=line.translated(20,20);qDebug()<<"平移之前的坐标点"<<line;qDebug()<<"平移之后的坐标点"<<newLine;
}MainWindow::~MainWindow()
{delete ui;
}

在这里插入图片描述

QSize

在QT中QSize类用来形容长度和宽度

QRect

在Qt中使用 QRect类来描述一个矩形

// 构造函数
// 构造一个空对象
QRect::QRect();
// 基于左上角坐标, 和右下角坐标构造一个矩形对象
QRect::QRect(const QPoint &topLeft, const QPoint &bottomRight);
// 基于左上角坐标, 和 宽度, 高度构造一个矩形对象
QRect::QRect(const QPoint &topLeft, const QSize &size);
// 通过 左上角坐标(x, y), 和 矩形尺寸(width, height) 构造一个矩形对象
QRect::QRect(int x, int y, int width, int height);// 设置矩形的尺寸信息, 左上角坐标不变
void QRect::setSize(const QSize &size);
// 设置矩形左上角坐标为(x,y), 大小为(width, height)
void QRect::setRect(int x, int y, int width, int height);
// 设置矩形宽度
void QRect::setWidth(int width);
// 设置矩形高度
void QRect::setHeight(int height);// 返回值矩形左上角坐标
QPoint QRect::topLeft() const;
// 返回矩形右上角坐标
// 该坐标点值为: QPoint(left() + width() -1, top())
QPoint QRect::topRight() const;
// 返回矩形左下角坐标
// 该坐标点值为: QPoint(left(), top() + height() - 1)
QPoint QRect::bottomLeft() const;
// 返回矩形右下角坐标
// 该坐标点值为: QPoint(left() + width() -1, top() + height() - 1)
QPoint QRect::bottomRight() const;
// 返回矩形中心点坐标
QPoint QRect::center() const;// 返回矩形上边缘y轴坐标
int QRect::top() const;
int QRect::y() const;
// 返回值矩形下边缘y轴坐标
int QRect::bottom() const;
// 返回矩形左边缘 x轴坐标
int QRect::x() const;
int QRect::left() const;
// 返回矩形右边缘x轴坐标
int QRect::right() const;// 返回矩形的高度
int QRect::width() const;
// 返回矩形的宽度
int QRect::height() const;
// 返回矩形的尺寸信息
QSize QRect::size() const;

日期和时间

QDate

QDate类可以封装日期信息也可以通过这个类得到日期相关的信息, 包括:年, 月, 日

// 构造函数
QDate::QDate();
QDate::QDate(int y, int m, int d);// 公共成员函数
// 重新设置日期对象中的日期
bool QDate::setDate(int year, int month, int day);
// 给日期对象添加 ndays 天
QDate QDate::addDays(qint64 ndays) const;
// 给日期对象添加 nmonths 月
QDate QDate::addMonths(int nmonths) const;
// 给日期对象添加 nyears 月
QDate QDate::addYears(int nyears) const;// 得到日期对象中的年/月/日
int QDate::year() const;
int QDate::month() const;
int QDate::day() const;
void QDate::getDate(int *year, int *month, int *day) const;// 操作符重载 ==> 日期比较
bool QDate::operator!=(const QDate &d) const;
bool QDate::operator<(const QDate &d) const;
bool QDate::operator<=(const QDate &d) const;
bool QDate::operator==(const QDate &d) const;
bool QDate::operator>(const QDate &d) const;
bool QDate::operator>=(const QDate &d) const;// 静态函数 -> 得到本地的当前日期
[static] QDate QDate::currentDate();

日期对象格式化

// 日期对象格式化
/*d    - The day as a number without a leading zero (1 to 31)dd   - The day as a 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 a number without a leading zero (1 to 12)MM   - The month as a 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 a two digit number (00 to 99)yyyy - The year as a four digit number. If the year is negative, a minus sign is prepended, making five characters.
*/
QString QDate::toString(const QString &format) const;
案例
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//获取当前日期 --> 静态方法QDate d=QDate::currentDate();//第一种方式 直接访问qDebug()<<"year:"<<d.year()<<",month:"<<d.month()<<",day:"<<d.day();//第二种方式 ---> 2001-01-01QString str=d.toString("yyyy-MM-dd");QString str1=d.toString("yy-MMM-ddd");qDebug()<<"date str:"<<str;qDebug()<<"date str:"<<str1;
}MainWindow::~MainWindow()
{delete ui;
}

在这里插入图片描述

QTime

QTime类可以封装时间信息也可以通过这个类得到时间相关的信息, 包括:时, 分, 秒, 毫秒

时间对象格式化

// 时间格式化
/*-- 时 --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).-- 上午或者下午AP or A	==>	使用AM/PM(大写) 描述上下午, 中文系统显示汉字ap or a	==>	使用am/pm(小写) 描述上下午, 中文系统显示汉字
*/
QString QTime::toString(const QString &format) const;
// 阶段性计时
// 过时的API函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>> 不推荐
// 开始计时
void QTime::start();
// 计时结束
int QTime::elapsed() const;
// 重新计时
int QTime::restart();
// 推荐使用的API函数
// QElapsedTimer 类
void QElapsedTimer::start();
qint64 QElapsedTimer::restart();
qint64 QElapsedTimer::elapsed() const;
案例
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QDebug>
#include<QElapsedTimer>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QTime curtime=QTime::currentTime();//方式1qDebug()<<"hour:"<<curtime.hour()<<",minute:"<<curtime.minute()<<",second:"<<curtime.second()<<",millisecond:"<<curtime.msec();//方式2QString strtm=curtime.toString("hh:mm:ss.zzz");qDebug()<<"格式化的日期"<<strtm;//统计函数的运行时间
#if 0QTime tt;tt.start();randNumbers(100);int ms=tt.elapsed();qDebug()<<"函数执行所用的时间长度为:"<<ms<<"毫秒";
#elseQElapsedTimer tt;tt.start();randNumbers(100);int ms=tt.elapsed();qDebug()<<"函数执行所用的时间长度为:"<<ms<<"毫秒";
#endif
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::randNumbers(int count)
{srand(time(NULL));for(int i=0;i<count;++i){int num=rand()%10000;qDebug()<<num;}
}

QDataTime

QDateTime类可以封装日期和时间信息也可以通过这个类得到日期和时间相关的信息, 包括:年, 月, 日, 时, 分, 秒, 毫秒。其实这个类就是QDate 和 QTime 这两个类的结合体。

案例

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QDebug>
#include <QElapsedTimer>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//获取当前的日期和时间QDateTime dt=QDateTime::currentDateTime();QString strdt=dt.toString("yyyy/MM/dd hh:mm:ss ap");qDebug()<<"当前的日期和时间"<<strdt;
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::randNumbers(int count)
{srand(time(NULL));for(int i=0;i<count;++i){int num=rand()%10000;qDebug()<<num;}
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com