Friday, May 11, 2007

QTextBrowser

One possible aproach that I can see (but have not tested) is to create
a new class inherrited from QTextBrowser and overload the loadResource function in a way where you check for the names you are interested in and resolve them.

For example like this (again not tested)

Qt Code:
  1. QVariant MyBrowser::loadResource(int type, const QUrl &name)
  2. {
  3. if (type == QTextDocument::ImageResource
  4. && name.scheme() == QLatin1String("mypics")) {
  5. QImage correctImage;
  6. //lookup the correct QImage from a cache
  7. return QVariant::fromValue(correctImage);
  8. } else {
  9. return QTextBrowser::loadResource(type, name);
  10. }
  11. }
Then you should be able to show images when you call
Qt Code:
  1. myTextBrowser.append(QLatin1String("\"mypics://theCurrentPicture.png\" />"));

Disclaimer: Shot from the Hip, not tested...might be completely the wrong track


------------------------------------------------------------------------
from PyQt4 import QtCore,QtGui
import urllib, os, md5

class PBrowser(QtGui.QTextBrowser):
def loadResource(self, type, name):
url=unicode(name.toString())
ret=QtCore.QVariant()
if url.startswith('http://'):
dn=os.path.expanduser('~/.bartleblog/cache/')
if not os.path.isdir(dn):
os.mkdir(dn)
m=md5.new()
m.update(url)
fn=os.path.join(dn,m.hexdigest())
if not os.path.isfile(fn):
urllib.urlretrieve(url, fn)
ret=QtGui.QTextBrowser.loadResource(self, type, QtCore.QUrl(fn))
else:
ret=QtGui.QTextBrowser.loadResource(self, type, name)
return ret

No comments: