简体   繁体   English

读取包含文本和图像的xml文件,并将其显示在QtextEdit中

[英]Read xml file which contain text and image and display it in a QtextEdit

i'm trying to develop at application window that contain a widget and two buttons Open and Save. 我正在尝试在包含小部件和两个按钮“打开”和“保存”的应用程序窗口中进行开发。 As an input I have an xml file which contains text plus an image. 作为输入,我有一个xml文件,其中包含文本和图像。 I want to display both text and image in the same widget, apply modification and save it finally. 我想在同一个小部件中同时显示文本和图像,应用修改并最终保存它。 My xml file look like this: 我的xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Stage>
<Item id = "1" Name = "Ensure span is erected">
    <image id = "1" src = "im.png"/>
</Item>
</Stage> 

What kind of widget can display both text and image and what to do to display them? 什么样的小部件可以同时显示文本和图像,以及如何显示它们?

I could display only image in a Qlabel 我只能在Qlabel中显示图像

QPixmap logo;
QByteArray ba;
QFile file("img.txt");
if(file.open(QIODevice::ReadOnly)) {
ba = file.readAll();
}
logo.loadFromData(QByteArray::fromBase64(ba));
ui->label->setPixmap(logo.scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatioByExpanding));

Any idea please! 任何想法,请! Your help is highly appreciated. 非常感谢您的帮助。

Here is a small example. 这是一个小例子。 I ignore ids in my case. 在我的情况下,我会忽略ID。 Convert your xml to html: 将您的xml转换为html:

QString MainWindow::HtmlFromXml(const QString &xmlFileName)
{
    QFile file(xmlFileName);
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Cant open file: " << file.errorString();
        return "";
    }

    QDomDocument htmlDoc;
    QDomElement htmlRoot = htmlDoc.createElement("html");

    QDomDocument xmlDoc;
    xmlDoc.setContent(&file);
    QDomElement xmlRoot = xmlDoc.documentElement();
    QDomElement xmlItem = xmlRoot.firstChild().toElement();

    while(!xmlItem.isNull())
    {
        //read xml
        int itemId = xmlItem.attribute("id", "0").toInt();
        QString itemName = xmlItem.attribute("Name", "");

        QDomElement xmlImg = xmlItem.firstChild().toElement();
        QString imgSrc;
        int imgId = 0;
        if (!xmlImg.isNull()) {
            imgSrc = xmlImg.attribute("src", "");
            imgId = xmlImg.attribute("id", "0").toInt();
        }

        //create html
        QDomElement htmlItem = htmlDoc.createElement("p");

        QDomElement htmlImg = htmlDoc.createElement("img");
        htmlImg.setAttribute("src", imgSrc);

        QDomElement htmlText = htmlDoc.createElement("p");
        QDomText textName = htmlDoc.createTextNode(itemName);
        htmlText.appendChild(textName);

        htmlItem.appendChild(htmlImg);
        htmlItem.appendChild(htmlText);
        htmlRoot.appendChild(htmlItem);

        //next
        xmlItem = xmlItem.nextSibling().toElement();
    }

    htmlDoc.appendChild(htmlRoot);

    return htmlDoc.toString();
}

Then set html you got to QTextEdit 然后将html设置为QTextEdit

QString strHtml = HtmlFromXml(fileName);
ui->textEdit->setHtml(strHtml);

Now you can edit in QTextEdit . 现在,您可以在QTextEdit进行编辑。 Before saving you need to get html from QTextEdit 保存之前,您需要从QTextEdit获取html。

QString strHtml = ui->textEdit->toHtml();
QString strXml = XmlFromHtml(strHtml);

And convert this html into xml 并将此html转换为xml

QString MainWindow::XmlFromHtml(const QString &strHtml)
{
    QDomDocument xmlDoc;
    QDomElement xmlRoot = xmlDoc.createElement("Stage");

    QDomDocument htmlDoc;
    htmlDoc.setContent(strHtml);
    QDomElement htmlRoot = htmlDoc.documentElement();
    QDomElement htmlHead = htmlRoot.firstChild().toElement();
    QDomElement htmlBody = htmlHead.nextSibling().toElement();

    int dummyId = 1;
    QDomElement htmlItem = htmlBody.firstChild().toElement();
    while (!htmlItem.isNull())
    {
        //<p><img/></p><p>text</p>
        QDomElement htmlImg = htmlItem.firstChild().toElement();
        QString imgSrc = htmlImg.attribute("src", "");

        htmlItem = htmlItem.nextSibling().toElement(); //move to <p> with text
        QDomText itemText = htmlItem.firstChild().toText();

        //create xml elements
        QDomElement xmlItem = xmlDoc.createElement("Item");
        xmlItem.setAttribute("id", dummyId);
        xmlItem.setAttribute("Name", itemText.data());

        QDomElement xmlImg = xmlDoc.createElement("image");
        xmlImg.setAttribute("src", imgSrc);
        xmlImg.setAttribute("id", dummyId);

        xmlItem.appendChild(xmlImg);
        xmlRoot.appendChild(xmlItem);

        //next
        htmlItem = htmlItem.nextSibling().toElement();
        dummyId++;
    }

    xmlDoc.appendChild(xmlRoot);
    return xmlDoc.toString();
}

And then save into text file. 然后保存到文本文件中。

Note: QTextEdit allow you to edit text, but have some troubles with images. 注意: QTextEdit允许您编辑文本,但是图像有些麻烦。 You can remove or copy/paste images inside QTextEdit but you can not paste images from outside clipboard. 您可以在QTextEdit内部删除或复制/粘贴图像,但是不能从剪贴板外部粘贴图像。 It's because QTextEdit operates with <img/> text tag not with image. 这是因为QTextEdit使用<img/>文本标签而不是图像进行操作。 To edit your images inside QTextEdit you need to edit text html of your QTextEdit instance. 要在QTextEdit编辑图像,您需要编辑QTextEdit实例的文本html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM