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;
}
相关推荐
xcyxiner14 小时前
DicomViewer (后台线程处理文件)4
qt
xcyxiner21 小时前
DicomViewer (添加模型类)3
qt
xcyxiner2 天前
DicomViewer (目录调整) 2
qt
xcyxiner2 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
倔强的石头_2 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab2 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence3 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神3 天前
三、用户与权限管理
数据库·mysql
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript