QXmlStreamReader的characterOffset方法如何工作?

发布于 2025-01-09 02:33:53 字数 10766 浏览 0 评论 0原文

如果 xml 文件长于几行,characterOffset 方法将停止正常工作。随着xml文件中信息量的增加,characterOffset方法更加偏离标签的位置。

代码:

QFile file("../file-multi-hello.xml");
file.open(QIODevice::ReadOnly);

QXmlStreamReader xml;
xml.setDevice(&file);
qint64 startPositionElem = -1;
qint64 endPositionElem = -1;
while(!xml.atEnd()) {
    qint64 offset = xml.characterOffset();
    xml.readNext();
    if (xml.isStartElement() && xml.name().toString() == "t") {
        startPositionElem = offset;
    }
    if (xml.isEndElement() && xml.name().toString() == "t") {
        endPositionElem = xml.characterOffset();
    }
}

file.seek(startPositionElem);
qDebug() << file.read(endPositionElem - startPositionElem);

file-multi-hello.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="4807E431" w14:textId="62BDDD69" w:rsidR="00E14DC7" w:rsidRDefault="00E14DC7">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="5029BEAE" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="6C84EB68" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="4247D896" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="594CCA3B" w14:textId="61C8A4A1" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>–</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="28E3E3B5" w14:textId="77777777" w:rsidR="009027BF" w:rsidRPr="00E14DC7" w:rsidRDefault="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
        </w:p>
        <w:sectPr w:rsidR="009027BF" w:rsidRPr="00E14DC7">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

控制台输出:

输出:“世界

上例中的 xml 文件已格式化。不格式化输出到控制台

输出:“r>世界

如何使characterOffset方法正常工作?显示在控制台中

输出:“世界”

PS 对于一个小的 xml 文件,一切正常

file-hello.xml

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="4807E431" w14:textId="71C9FAEC" w:rsidR="00E14DC7" w:rsidRPr="00E14DC7" w:rsidRDefault="00E14DC7">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00E14DC7" w:rsidRPr="00E14DC7">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

控制台输出:

输出:“你好世界

Qt版本5.15.2 编译器 MinGW 64 位

The characterOffset method stops working properly if the xml file is longer than a few lines. With an increase in the amount of information in the xml file, the characterOffset method deviates from the position of the tag even more.

Code:

QFile file("../file-multi-hello.xml");
file.open(QIODevice::ReadOnly);

QXmlStreamReader xml;
xml.setDevice(&file);
qint64 startPositionElem = -1;
qint64 endPositionElem = -1;
while(!xml.atEnd()) {
    qint64 offset = xml.characterOffset();
    xml.readNext();
    if (xml.isStartElement() && xml.name().toString() == "t") {
        startPositionElem = offset;
    }
    if (xml.isEndElement() && xml.name().toString() == "t") {
        endPositionElem = xml.characterOffset();
    }
}

file.seek(startPositionElem);
qDebug() << file.read(endPositionElem - startPositionElem);

file-multi-hello.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="4807E431" w14:textId="62BDDD69" w:rsidR="00E14DC7" w:rsidRDefault="00E14DC7">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="5029BEAE" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="6C84EB68" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="4247D896" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="594CCA3B" w14:textId="61C8A4A1" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>–</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="28E3E3B5" w14:textId="77777777" w:rsidR="009027BF" w:rsidRPr="00E14DC7" w:rsidRDefault="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
        </w:p>
        <w:sectPr w:rsidR="009027BF" w:rsidRPr="00E14DC7">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

console output:

output: " <w:t>world</w:"

The xml file in the example above is formatted. Outputs to the console without formatting

output: "r><w:t>world</w:"

How can I make the characterOffset method work fine? To appear in the console

output: "<w:t>world</w:>"

P.S. For a small xml file everything works fine

file-hello.xml

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="4807E431" w14:textId="71C9FAEC" w:rsidR="00E14DC7" w:rsidRPr="00E14DC7" w:rsidRDefault="00E14DC7">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00E14DC7" w:rsidRPr="00E14DC7">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

console output:

output: "<w:t>Hello world</w:t>"

Qt version 5.15.2
compiler MinGW 64bit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2025-01-16 02:33:53

第 72 行有一个多字节字符:U+2013
该字符在 UTF-8 中使用 3 个字节。

函数 characterOffset 将其计为单个字符。
但是,当您使用 seekread 读取文件时,您会使用字节数。

在您的示例中,最后一个元素的字符偏移量为 4940,但它从第 4942 字节开始。

为了使其工作,您需要使用正确处理多字节字符的函数。一种解决方案是使用 QString:

file.seek(0);
const QByteArray data = file.readAll();
const QString str = QString::fromUtf8(data);
qDebug() << str.mid(startPositionElem, endPositionElem - startPositionElem);

另一种是使用 QTextStream:

QTextStream stream(&file);
stream.seek(0);
stream.read(startPositionElem);
qDebug() << stream.read(endPositionElem - startPositionElem);

请注意,在任何情况下,您都不能直接查找多个字符,只能查找多个字节。
这意味着您必须从文件的开头读取,以便执行 unicode 解析的函数/类可以读取和检测多字节字符。

Line 72 you have a multibyte character: U+2013
This character uses 3 bytes in UTF-8.

The function characterOffset counts it as a single character.
However, when you read the file using seek and read you work with the numbers of bytes.

In you exemple the last element has a character offset of 4940, however it starts at the 4942th byte.

To make it work you need to use functions that handle properly multibyte characters. A solution is to use QString:

file.seek(0);
const QByteArray data = file.readAll();
const QString str = QString::fromUtf8(data);
qDebug() << str.mid(startPositionElem, endPositionElem - startPositionElem);

Another is to use QTextStream:

QTextStream stream(&file);
stream.seek(0);
stream.read(startPositionElem);
qDebug() << stream.read(endPositionElem - startPositionElem);

Note that in any case you cannot seek directly to a number of characters, only to a number of bytes.
That means that you will have to read from the start of the file so that the function/class that do the unicode parsing can read and detect multibyte characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文