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);
        tdContent->setPageSize(contentSize);
    
    
        QFile styleFile(m_modelStyle);
        QString style;
        if (styleFile.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream in(&styleFile);
            style = in.readAll();
        }
    
        QFile model(m_model);
        if (model.open(QFile::ReadOnly | 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 {
            addContent();
    
        }
    
        tdContent->setHtml(m_totalHtml);
        //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.

    Charlie.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.