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;
}
相关推荐
波波烤鸭4 分钟前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
会开花的二叉树6 分钟前
继承与组合:C++面向对象的核心
java·开发语言·c++
长河2 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
Cyan_RA92 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
l1t4 小时前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
再见晴天*_*5 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08276 小时前
Qt程序单独运行报错问题
开发语言·qt
MarkHard1238 小时前
如何利用redis使用一个滑动窗口限流
数据库·redis·缓存
hdsoft_huge8 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘9 小时前
39.网络流入门
开发语言·网络·c++·算法