PDA

View Full Version : Image renders using setHtml method in QTextBrowser but not in QTextEdit



amitkc
31st December 2010, 13:08
Hi all,

I'm getting inconsistent results with a very short piece of test code in which html text & a small png file renders in a QTextBrowser but not in a QTextEdit. I've tried to boil the issue down to the minimum in5669 a test application which behaves as follows:

- when compiled to use a QTextBrowser I get an image (barrel.png) followed by the text fragment "After."

- when compiled to use a QTextEdit I get an image placehoder followed by the text fragment.

If I deliberately supply either version with a non-existent image I get the QTextEdit behaviour (placeholder).

The real life application would have a user drag and drop images onto the chosen widget and display along with notes edited in or dropped from text files.

My reading of the docs has QTextBrowser as an enhanced QTextEdit, with most of the additions being aimed at hyperlinks & navigation. A simple rendering task ought to be common to both?

Here is the code arranged in separate code blocks to reflect what's on my machine :

Header first



#ifndef TESTDIALOG_H
#define TESTDIALOG_H

#include <QWidget>

class QTextBrowser;
class QTextEdit;


// comment this out to switch between options with text edit/browser
//
#define TEXTEDIT

class TestDialog : public QWidget
{
Q_OBJECT
public:
explicit TestDialog(QWidget *parent = 0);

void test1();


signals:

public slots:

private:

#ifdef TEXTEDIT
QTextEdit *editor;
#else
QTextBrowser *editor;
#endif

};

#endif // TESTDIALOG_H



Implementation





#include "testdialog.h"

#include <QtGui/QTextBrowser>
#include <QtGui/QVBoxLayout>
#include <QtGui/QTextFrame>

#include <QtCore/QtDebug>


TestDialog::TestDialog(QWidget *parent) :
QWidget(parent)
{

#ifdef TEXTEDIT
editor = new QTextEdit(this);
qDebug() << "With QTextEdit";
#else
editor = new QTextBrowser(this);
qDebug() << "With QTextBrowser";
#endif

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(editor);
this->setLayout(layout);

}



void TestDialog::test1()
{

QString html;
html += "<body>";
html += "<p>";
html += "<img src = \"file:barrel.png\">";
html += "<p><i>After</i>";
html += "</body>";

editor->setHtml(html);

}




main




#include <QtGui/QApplication>


#include "testdialog.h"



int main(int argc, char * argv[])
{

QApplication app(argc, argv);

TestDialog * td = new TestDialog;
td->show();
td->test1();


return app.exec();
}



I've had the same behaviour on Linux & Mac using Qt 4.7.0 & 4.6.3.

Can anyone see a problem with my code?

Do others get the same result?

Appreciate any pointers - I'm getting nowhere with my attempts to follow using source & debug at the moment. Zip file contains image and qmake pro file along with source quoted above.

Regards,
Amit