QtXlsx适配qt6源码修改

    本文记录了在QtXlsx(https://github.com/dbzhang800/QtXlsxWriter)2020-03-19时间版本基础上为适配qt6进行源码修改。

    在Deepin20的Qt6环境下编译通过。

    主要修改在src目录下的源码,examples目录下也有一点。

一、src部分源码修改

(一)适配QT6修改

1、迭代器使用变化

    QMap::const_iterator迭代器不支持-操作。

    代码中应明确使用的是const_iterator还是iterator,避免混淆错误。

(1)xlsxworksheet.cpp

//原代码

    int firstRow = cellTable.constBegin().key();
    int lastRow = (cellTable.constEnd() - 1).key();

//新代码

    int firstRow = cellTable.firstKey();
    int lastRow  = cellTable.lastKey();

//原代码

    for (QMap>>::const_iterator it = cellTable.begin();
         it != cellTable.end(); ++it) {
        Q_ASSERT(!it.value().isEmpty());
        if (firstColumn == -1 || it.value().constBegin().key() < firstColumn)
            firstColumn = it.value().constBegin().key();
        if (lastColumn == -1 || (it.value().constEnd() - 1).key() > lastColumn)
            lastColumn = (it.value().constEnd() - 1).key();
    }

//新代码

    for (QMap>>::const_iterator it = cellTable.constBegin();
         it != cellTable.constEnd(); ++it)
    {
        Q_ASSERT(!it.value().isEmpty());
        if (firstColumn == -1 || it.value().firstKey() < firstColumn)
            firstColumn = it.value().firstKey();
        if (lastColumn == -1 || (it.value().lastKey() > lastColumn))
            lastColumn  = it.value().lastKey();
    }

2、不再支持使用QStringRef类,用QStringView替代

(1)xlsxdocpropscore.cpp

//原代码

           const QStringRef nsUri = reader.namespaceUri();

           const QStringRef name = reader.name();

//新代码

            const QStringView nsUri = reader.namespaceUri();

            const QStringView name = reader.name();

(2)xlsxworkbook.cpp

//原代码

    const QStringRef &stateString = attributes.value(QLatin1String("state"));

//新代码

    const QStringView &stateString = attributes.value(QLatin1String("state"));

(3)xlsxchart.cpp

//原代码

bool ChartPrivate::loadXmlXxxChart(QXmlStreamReader &reader)
{
    QStringRef name = reader.name();

//新代码

bool ChartPrivate::loadXmlXxxChart(QXmlStreamReader &reader)
{
    QStringView name = reader.name();

//原代码

bool ChartPrivate::loadXmlSer(QXmlStreamReader &reader)
            ......
            QStringRef name = reader.name();

//新代码

bool ChartPrivate::loadXmlSer(QXmlStreamReader &reader)
            ......
            QStringView name = reader.name();

//原代码

bool ChartPrivate::loadXmlAxis(QXmlStreamReader &reader)
            ......
                QStringRef pos = attrs.value(QLatin1String("val"));

//新代码

bool ChartPrivate::loadXmlAxis(QXmlStreamReader &reader)
            ......
                QStringView pos = attrs.value(QLatin1String("val"));

3、QDataStream对象初始化参数应为QIODeviceBase::OpenMode,修改QIODevice为QIODeviceBase

(1)xlsxformat.cpp中有4处

//原代码

QByteArray Format::fontKey() const
            ......
        QDataStream stream(&key, QIODevice::WriteOnly);

//新代码

QByteArray Format::fontKey() const
            ......
        QDataStream stream(&key, QIODeviceBase::WriteOnly);

//原代码

QByteArray Format::borderKey() const
            ......
        QDataStream stream(&key, QIODevice::WriteOnly);

//新代码

QByteArray Format::borderKey() const
            ......
        QDataStream stream(&key, QIODeviceBase::WriteOnly);

//原代码

QByteArray Format::fillKey() const
            ......
        QDataStream stream(&key, QIODevice::WriteOnly);

//新代码

QByteArray Format::fillKey() const
            ......
        QDataStream stream(&key, QIODeviceBase::WriteOnly);

//原代码

QByteArray Format::formatKey() const
            ......
        QDataStream stream(&key, QIODevice::WriteOnly);

//新代码

QByteArray Format::formatKey() const
            ......
        QDataStream stream(&key, QIODeviceBase::WriteOnly);

4、自定义类注册使用修改

    宏函数qRegisterMetaType()支持自定义类是否已注册的判断,取消不必要的判断

    不再支持宏qRegisterMetaTypeStreamOperators()

    已经删除QMetaType::registerDebugStreamOperator()

(1)xlsxstyles.cpp

//原代码

     if (QMetaType::type("XlsxColor") == QMetaType::UnknownType) {
        qRegisterMetaType("XlsxColor");
        qRegisterMetaTypeStreamOperators("XlsxColor");
#if QT_VERSION >= 0x050200
        QMetaType::registerDebugStreamOperator();
#endif
    }

//新代码

        qRegisterMetaType("XlsxColor");
#if QT_VERSION < 0x060000
        QMetaType::registerDebugStreamOperator();
#endif

5、QVariant()初始化方式的修改

(1)xlsxrichstring.cpp

//原代码

    return QVariant(qMetaTypeId(), this);

//新代码

    return QVariant::fromValue(this);

(2)xlsxcolor.cpp

//原代码

    return QVariant(qMetaTypeId(), this);

//新代码

    return QVariant::fromValue(this);

6、QString::asprintf替代sprintf

(1)xlsxcolor.cpp

//原代码

color.sprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());

//新代码

color=QString::asprintf("%02X%02X%02X%02X", c.alpha(), c.red(), c.green(), c.blue());

(二)代码错误修改

1、xlsxformat.cpp

//原代码

if (hasProperty(FormatPrivate::P_Protection_Hidden || FormatPrivate::P_Protection_Locked)) {

//新代码

if (hasProperty(FormatPrivate::P_Protection_Hidden) || hasProperty(FormatPrivate::P_Protection_Locked)) {

(三)警告修改

1、类别名不能用class前置引用

xlsxutility_p.h中QStringList是类的别名,此处注释其前置引用

//原代码

class QStringList;

//新代码

//class QStringList;

2、0改为nullptr

(1)xlsxcell.h

//原代码

    Cell(const QVariant &data = QVariant(), CellType type = NumberType,
         const Format &format = Format(), Worksheet *parent = 0);

//新代码

    Cell(const QVariant &data = QVariant(), CellType type = NumberType,
         const Format &format = Format(), Worksheet *parent = nullptr);

(2)xlsxconditionalformatting.h

//原代码

    bool loadFromXml(QXmlStreamReader &reader, Styles *styles = 0);

//新代码

    bool loadFromXml(QXmlStreamReader &reader, Styles *styles = nullptr);

(3)xlsxdocument.h

//原代码

    explicit Document(QObject *parent = 0);
    Document(const QString &xlsxName, QObject *parent = 0);
    Document(QIODevice *device, QObject *parent = 0);

//新代码

    explicit Document(QObject *parent = nullptr);
    Document(const QString &xlsxName, QObject *parent = nullptr);
    Document(QIODevice *device, QObject *parent = nullptr);

(4)xlsxchart.h

//原代码

    void addSeries(const CellRange &range, AbstractSheet *sheet = 0);

//新代码

    void addSeries(const CellRange &range, AbstractSheet *sheet = nullptr);

3、函数标记为可覆盖override

(1)xlsxchart.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(2)xlsxchartsheet.h

//原代码

    Chartsheet *copy(const QString &distName, int distId) const;
    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    Chartsheet *copy(const QString &distName, int distId) const override;
    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(3)xlsxworksheet.h

//原代码

    Worksheet *copy(const QString &distName, int distId) const;
    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    Worksheet *copy(const QString &distName, int distId) const override;
    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(4)xlsxworkbook.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(5)xlsxdocpropscore_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(6)xlsxsharedstrings_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(7)xlsxstyles_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(8)xlsxtheme_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    QByteArray saveToXmlData() const;
    bool loadFromXmlData(const QByteArray &data);
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    QByteArray saveToXmlData() const override;
    bool loadFromXmlData(const QByteArray &data) override;
    bool loadFromXmlFile(QIODevice *device) override;

   

(9)xlsxdrawing_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(10)xlsxdrawinganchor_p.h

//原代码

    bool loadFromXml(QXmlStreamReader &reader);
    void saveToXml(QXmlStreamWriter &writer) const;

//新代码

    bool loadFromXml(QXmlStreamReader &reader) override;
    void saveToXml(QXmlStreamWriter &writer) const override;

(11)xlsxcontenttypes_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(12)xlsxdocpropsapp_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    bool loadFromXmlFile(QIODevice *device) override;

(13)xlsxsimpleooxmlfile_p.h

//原代码

    void saveToXmlFile(QIODevice *device) const;
    QByteArray saveToXmlData() const;
    bool loadFromXmlData(const QByteArray &data);
    bool loadFromXmlFile(QIODevice *device);

//新代码

    void saveToXmlFile(QIODevice *device) const override;
    QByteArray saveToXmlData() const override;
    bool loadFromXmlData(const QByteArray &data) override;
    bool loadFromXmlFile(QIODevice *device) override;

二、examples部分源码修改

(一)适配QT6修改

1、qrand()、qsrand()已弃用,使用QRandomGenerator类替代

(1)xlsx/conditionalformatting/main.cpp

//原代码

            xlsx.write(row, col, qrand() % 100);

//新代码

            xlsx.write(row, col, QRandomGenerator::system()->generate() % 100);

(2)xlsx/demo/main.cpp

//原代码

    qsrand(QDateTime::currentMSecsSinceEpoch());
    for (int row = 2; row < 31; ++row) {
        for (int col = 1; col <= 10; ++col)
            xlsx.write(row, col, qrand() % 100);

//新代码

    for (int row = 2; row < 31; ++row) {
        for (int col = 1; col <= 10; ++col)
            xlsx.write(row, col, QRandomGenerator::system()->generate() % 100);

你可能感兴趣的:(Qt,qt,excel)