htmlc++qtpython-sphinxqtextbrowser

Sphinx Integration in Qt


I would like to integrate Sphinx documentation functionality to help with my Qt project. However, when including the HTML files for Sphinx, the formatting appears differently and no file links work. For example:

QFile file("/home/user1/project/Sphinx/build/html/intro.html");
if (!file.open(QIODevice::Readonly))
    qDebug() << "Didn't open file";
QTextStream in(&file);
ui->textBrowser->setText(in.readAll());

Error: QTextBrowser: No document for _sources/intro.txt

This will cause the textBrowser to open the correct file, but will not end up displaying the page with the correct HTML coding, and will not follow the links even though those HTML files are contained in the same path (as I have copied the entire Sphinx project into the Qt project).

Is there some way to package the entire Sphinx project so that inclusion of multiple files is unnecessary or is the multiple file inclusion the way to go and I'm just handling it incorrectly?


Solution

  • Instead of reading all text and setting it with setText() you must use the setSource() method and pass it to the QUrl using the QUr::fromLocalFile() method.

    main.cpp

    #include <QtWidgets>
    
    class Widget: public QWidget
    {
        Q_OBJECT
    public:
        Widget(QWidget *parent=nullptr):
            QWidget(parent),
            m_text_browser(new QTextBrowser)
        {
            m_lineedit = new QLineEdit;
            auto button = new QPushButton("Load");
    
            auto lay = new QVBoxLayout{this};
            auto hlay = new QHBoxLayout;
            lay->addLayout(hlay);
            hlay->addWidget(m_lineedit);
            hlay->addWidget(button);
            lay->addWidget(m_text_browser);
    
            connect(button, &QPushButton::clicked, this, &Widget::on_clicked);
        }
    private slots:
        void on_clicked(){
            QString fileName = QFileDialog::getOpenFileName(this,
                                                            tr("Open Image"),
                                                            QDir::homePath(),
                                                            tr("HTML Files (*.html)"));
            m_lineedit->setText(fileName);
            m_text_browser->setSource(QUrl::fromLocalFile(fileName));
        }
    private:
        QTextBrowser *m_text_browser;
        QLineEdit *m_lineedit;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.showMaximized();
        return a.exec();
    }
    
    #include "main.moc"