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;
}
相关推荐
ccddsdsdfsdf3 小时前
DBeaver怎么链接mongoDB
数据库·mongodb
AI玫瑰助手3 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车3 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋3 小时前
C++14特性
开发语言·c++·c++14特性
丷丩3 小时前
Postgresql基础实践教程(十一)各种Join
数据库·postgresql·join
读书札记20223 小时前
Qt界面卡死问题探讨及解决方法
qt
星夜夏空994 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb
TheRouter4 小时前
AI Agent 记忆体系建设实战:短期、长期与工作记忆的工程实现
数据库·人工智能·oracle
Omics Pro5 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
JAVA社区5 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展