c++ - 如何使用 QT 从 SVG 文件中获取值

标签 c++ qt svg

我需要一种方法来使用 XMLStreamReader 在 QT 中获取 svg 文件中的值。我该怎么做?

这是 SVG 文件中的值。

enter image description here

这是我已经尝试过的方法。如果我得到一些东西,我会尝试打印出这些值。顺便说一句,我是 QT 的新手。

xmlFile = new QFile("composition.svg");

if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text))
                qDebug() << ("Load XML File Problem");

xmlReader = new QXmlStreamReader(xmlFile);


//Parse the XML until we reach end of it
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
        // Read next element
        QXmlStreamReader::TokenType token = xmlReader->readNext();
        //If token is just StartDocument - go to next
        if(token == QXmlStreamReader::StartDocument) {
                continue;
        }
        //If token is StartElement - read it
        if(token == QXmlStreamReader::StartElement) {

                if(xmlReader->name() == "g") {
                        continue;
                }

                if(xmlReader->name() == "g") {
                    qDebug() << xmlReader->readElementText();
                    //fprintf(stderr, xmlReader->readElementText().toLatin1());
                }
        }
}

最佳答案

while(!xmlReader->atEnd() && !xmlReader->hasError()) {
        // Read next element
        QXmlStreamReader::TokenType token = xmlReader->readNext();
        //If token is just StartDocument - go to next
        if(token == QXmlStreamReader::StartDocument)
                continue;
        //If token is StartElement - read it
        if(token == QXmlStreamReader::StartElement)
        {
            if(xmlReader->name() == "g")
                   continue;
            if(xmlReader->name() == "polygon")
            {
                foreach(const QXmlStreamAttribute &attr, xmlReader->attributes())
                {
                    if (attr.name().toString() == QLatin1String("style"))
                    {
                        qDebug() << attr.value().toLatin1();
                    }
                }
            }
        }
}

我需要循环属性/节点来获取它的一些值。

关于c++ - 如何使用 QT 从 SVG 文件中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353502/

相关文章:

c++ - 由单个元素组成的参数包的折叠表达式在什么基础上转换为未加括号的表达式

c++ - 全局变量不好吗?

c# - 如何捕获 C++ 函数 printf() 的输出

C++ 迭代器访问下一个元素进行比较

html - 将现有的 SVG 或 HTML5 元素添加到 Box2D 世界

c++ - 自定义编译标志以启用特定功能

c++ - 如何在不阻塞 UI 的情况下在 QGraphicsScene 中移动大约 1000 个项目

python - 将参数传递给 QT Designer 构建的槽函数

xml - 如何降低 svg 滤镜中 alpha 层的不透明度?

android - 资源可用时出现 ResourcesNotFoundException(API 22 和 23 设备)