Important: Please read the Qt Code of Conduct -

Need your opinion on resource of QTextDocument

  • Hello,

    I want use resource and addResource of QTextDocument to load a html model and stylesheet.

    Here, my currently code :

        QTextDocument *tdContent = new QTextDocument(this);
        QFile styleFile(m_modelStyle);
        QString style;
        if ( | QFile::Text))
            QTextStream in(&styleFile);
            style = in.readAll();
        QFile model(m_model);
        if ( | QFile::Text))
    /// what is the best way for you ? Use QTextStream or directly use resource of QTextDocument ?
            QTextStream in(&model);
            m_totalHtml = in.readAll();
            m_totalHtml = tdContent->resource(QTextDocument::HtmlResource, QUrl(m_model)).toString();
            tdContent->addResource(QTextDocument::StyleSheetResource, QUrl( "style.css" ), style);
            for (const auto i : m_dataModel)
                QStringList tuple = i.toStringList();
                m_totalHtml.replace("$"+tuple[0]+"$", tuple[1]);
        else {
        //tdContent->addResource(QTextDocument::HtmlResource, QUrl(m_model), m_totalHtml);

    If I comment tdContent->setHtml(m_totalHtml);and I uncomment the last line, it doesn't work.
    Do I make a mistake using addResource ?

    Thank you for help.


Log in to reply