Qt 数据类型介绍

Qt 提供了一些常见的数据类型来处理各种数据,包括字符串、日期时间、颜色等。下面是一些常用的 Qt 数据类型及其使用例程:

1.QString:用于处理字符串的类。

复制代码
#include <QString>
#include <QDebug>

int main() {
    QString str = "Hello, Qt!";
    qDebug() << "String Example:";
    qDebug() << str;

    return 0;
}

2.QDateTime:用于处理日期和时间的类。

复制代码
#include <QDateTime>
#include <QDebug>

int main() {
    QDateTime currentDateTime = QDateTime::currentDateTime();
    qDebug() << "Date and Time Example:";
    qDebug() << "Current Date and Time:" << currentDateTime.toString();

    return 0;
}

3.QColor:用于表示颜色的类。

复制代码
#include <QColor>
#include <QDebug>

int main() {
    QColor color(Qt::red);
    qDebug() << "Color Example:";
    qDebug() << "Color Name:" << color.name();
    qDebug() << "Red Component:" << color.red();
    qDebug() << "Green Component:" << color.green();
    qDebug() << "Blue Component:" << color.blue();

    return 0;
}

4.QPoint:用于表示二维平面上的点的类。

复制代码
#include <QPoint>
#include <QDebug>

int main() {
    QPoint point(10, 20);
    qDebug() << "Point Example:";
    qDebug() << "X Coordinate:" << point.x();
    qDebug() << "Y Coordinate:" << point.y();

    return 0;
}

5.QSize:用于表示二维平面上的尺寸的类。

复制代码
#include <QSize>
#include <QDebug>

int main() {
    QSize size(100, 200);
    qDebug() << "Size Example:";
    qDebug() << "Width:" << size.width();
    qDebug() << "Height:" << size.height();

    return 0;
}

6.QRect :用于表示二维平面上的矩形的类。QVariant:用于封装各种 Qt 数据类型的通用类。

复制代码
#include <QVariant>
#include <QDebug>

int main() {
    QVariant var = 10;
    qDebug() << "Variant Example:";
    qDebug() << "Variant Type:" << var.typeName();
    qDebug() << "Variant Value:" << var.toInt();

    return 0;
}

7.QUrl:用于处理统一资源定位符 (URL) 的类。

复制代码
#include <QUrl>
#include <QDebug>

int main() {
    QUrl url("https://www.example.com");
    qDebug() << "URL Example:";
    qDebug() << "URL Scheme:" << url.scheme();
    qDebug() << "Host:" << url.host();
    qDebug() << "Path:" << url.path();

    return 0;
}

8.QByteArray:用于处理字节数组的类。

复制代码
#include <QByteArray>
#include <QDebug>

int main() {
    QByteArray byteArray("Hello, Qt!");
    qDebug() << "ByteArray Example:";
    qDebug() << "ByteArray Size:" << byteArray.size();
    qDebug() << "ByteArray Content:" << byteArray;

    return 0;
}

9.QListWidgetItem:用于在列表框中显示的列表项类。

复制代码
#include <QListWidget>
#include <QDebug>

int main() {
    QListWidget listWidget;
    QListWidgetItem *item1 = new QListWidgetItem("Item 1");
    QListWidgetItem *item2 = new QListWidgetItem("Item 2");
    listWidget.addItem(item1);
    listWidget.addItem(item2);

    qDebug() << "ListWidget Example:";
    for (int i = 0; i < listWidget.count(); ++i) {
        qDebug() << "Item:" << listWidget.item(i)->text();
    }

    return 0;
}

10.QVariant:用于封装各种 Qt 数据类型的通用类。

复制代码
#include <QVariant>
#include <QDebug>

int main() {
    QVariant var = 10;
    qDebug() << "Variant Example:";
    qDebug() << "Variant Type:" << var.typeName();
    qDebug() << "Variant Value:" << var.toInt();

    return 0;
}
相关推荐
YJlio1 小时前
Active Directory 工具学习笔记(10.8):AdInsight——保存与导出(证据留存、共享与二次分析)
数据库·笔记·学习
suoyue_zhan1 小时前
GBase的管理监控平台GEM实践指南
数据库
生骨大头菜1 小时前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务
绝不收费—免费看不了了联系我1 小时前
Fastapi的单进程响应问题 和 解决方法
开发语言·后端·python·fastapi
消失的旧时光-19432 小时前
深入理解 Java 线程池(二):ThreadPoolExecutor 执行流程 + 运行状态 + ctl 原理全解析
java·开发语言
咖啡续命又一天2 小时前
Trae CN IDE 中 Python 开发的具体流程和配置总结
开发语言·ide·python·ai编程
哈哈老师啊2 小时前
Springboot学生综合测评系统hxtne(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
4311媒体网2 小时前
帝国cms调用文章内容 二开基本操作
java·开发语言·php
小小8程序员2 小时前
Redis-10
数据库·redis·缓存
liuzhilongDBA2 小时前
从collation mismatch异常到其原理
数据库·version·glibc·postgres·collation