使用QFile和QDataStream(或QTextStream)序列化一个类举例

1.概要

使用QFile和QDataStream(或QTextStream)序列化一个类举例

2.代码

1.工程结构

2.工程文件

cmake_minimum_required(VERSION 3.14)

project(untitled4 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)

add_executable(untitled4
  main.cpp
  mypersistentclass.h mypersistentclass.cpp
)
target_link_libraries(untitled4 Qt${QT_VERSION_MAJOR}::Core)

include(GNUInstallDirs)
install(TARGETS untitled4
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

3.MyPersistentClass头文件

#ifndef MYPERSISTENTCLASS_H
#define MYPERSISTENTCLASS_H

#include <QObject>
#include <QString>

class MyPersistentClass : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName)
    Q_PROPERTY(int age READ age WRITE setAge)

public:
    explicit MyPersistentClass(QObject *parent = nullptr);

    QString name() const;
    void setName(const QString &name);

    int age() const;
    void setAge(int age);

    void saveSettings() const;
    void loadSettings();

private:
    QString m_name;
    int m_age;
};

#endif // MYPERSISTENTCLASS_H

4.MyPersistentClass源文件

#include "MyPersistentClass.h"
#include <QSettings>
#include <QDebug>

MyPersistentClass::MyPersistentClass(QObject *parent)
    : QObject(parent), m_name("DefaultName"), m_age(0) {}

QString MyPersistentClass::name() const
{
    return m_name;
}

void MyPersistentClass::setName(const QString &name)
{
    m_name = name;
}

int MyPersistentClass::age() const
{
    return m_age;
}

void MyPersistentClass::setAge(int age)
{
    m_age = age;
}

void MyPersistentClass::saveSettings() const
{
    QSettings settings("MyCompany", "MyApplication");
    settings.setValue("name", m_name);
    settings.setValue("age", m_age);
}

void MyPersistentClass::loadSettings()
{
    QSettings settings("MyCompany", "MyApplication");
    m_name = settings.value("name", "DefaultName").toString();
    m_age = settings.value("age", 0).toInt();
}

5.主函数

3.运行结果

11:14:01: Debugging D:\projects\qts\untitled4\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug\untitled4.exe ...
Loaded settings: Name = "NewName" , Age = 30
Saved settings: Name = "NewName" , Age = 50
Reloaded settings: Name = "NewName" , Age = 50

以下是一个使用QFile和QDataStream来结合文件映射进行编码bmp图的功能封装成函数的示例: ```cpp #include <QFile> #include <QDataStream> bool encodeBmpFile(const QString& fileName, const QImage& image) { QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) return false; int width = image.width(); int height = image.height(); int bitsPerPixel = image.depth(); int bytesPerLine = (width * bitsPerPixel + 31) / 32 * 4; int dataSize = bytesPerLine * height; int fileSize = dataSize + 54; QByteArray data; data.resize(fileSize); // BMP header data[0] = 'B'; data[1] = 'M'; data[2] = fileSize & 0xff; data[3] = (fileSize >> 8) & 0xff; data[4] = (fileSize >> 16) & 0xff; data[5] = (fileSize >> 24) & 0xff; data[10] = 54; data[14] = 40; data[18] = width & 0xff; data[19] = (width >> 8) & 0xff; data[20] = (width >> 16) & 0xff; data[21] = (width >> 24) & 0xff; data[22] = height & 0xff; data[23] = (height >> 8) & 0xff; data[24] = (height >> 16) & 0xff; data[25] = (height >> 24) & 0xff; data[26] = 1; data[28] = bitsPerPixel; // BMP data uchar* dest = reinterpret_cast<uchar*>(data.data() + 54); const uchar* src = image.constBits(); for (int y = 0; y < height; ++y) { memcpy(dest, src, bytesPerLine); dest += bytesPerLine; src += image.bytesPerLine(); } // Write data to file QDataStream stream(&file); stream.writeRawData(data.constData(), fileSize); return true; } ``` 该函数接受两个参数:要写入的文件名和要编码的图像对象。函数首先打开文件并计算文件大小,然后创建一个字节数组来存储BMP文件头和图像数据。接下来,该函数设置BMP文件头和将图像数据复制到字节数组中。最后,将数据写入文件并返回成功标志。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值