views:

227

answers:

2

I am writing a small application with Qt 4.6 (64-bit Arch Linux, though that shouldn't matter) which lets the user edit a document using a QWebView with contentEditable turned on. However, for some reason embedding an image does not work. Here is a code snippet:

void LeafEditView::onInsertImage()
{
    // bring up a dialog, ask for an image
    QString imagePath = QFileDialog::getOpenFileName(this,tr("Open Image File"),"/",tr("Images (*.png *.xpm *.jpg)"));
    ui->leafEditor->page()->mainFrame()->documentElement().evaluateJavaScript("document.execCommand('insertImage',null,'"+imagePath+"');");
}

The test image does in fact exist and yet absolutely nothing happens. Bold / italics / underline all work fine via JavaScript, just not images. Thoughts?

+1  A: 

Check that QWebSettings::AutoLoadImages is enabled.

You could also try: document.execCommand('insertImage',false,'"+imagePath+"');

Try using relative vs absolute paths to the image.

Last but not least, poke around this sample application -- they are using a similar method of Javascript execCommand(), they do some things in a slightly different way such as using QUrl::fromLocalFile.

Best of luck!

swanson
I had tried a number of those things. I poked around, then asked here, and continued poking around. It increases the odds of an answer :)
gatlin
QUrl::fromLocalFile worked for me. Thanks!
Jesse Aldridge
A: 

It turns out that WebKit has a policy of not loading resources from the local filesystem without some massaging. In my code, I have a WebKit view which I'm using to edit leaves in a notebook. The following one-liner solved my issue:

 ui->leafEditor->page()->mainFrame()->setHtml("<html><head></head><body></body></html>",QUrl("file:///"));

From what I gleaned by lurking around the WebKit mailing list archives, in order to load files from the local filesystem one must set the URI to be file:, and this does the job.

gatlin