PDA

View Full Version : QWebview not showing images



manojmka
7th January 2008, 05:05
Hi!

I am trying QT 4.4 tecnology preview for webkit to have full html functionality in my application. It does not display images!! Any help why?

Regards,
Manoj

moowy
7th January 2008, 05:47
I'm sorry that I don't have an answer for you, but maybe it doesn't work, because it still is a technology preview ?

manojmka
7th January 2008, 06:00
but why is it so that it shows the images in online page but not offline? i.e. If I show the page "http://www.yahoo.com" it shows perfectly but if I save the same page in html format along with its all images, it shows everything except images???

I am stuck here with this!! please help!!

jpn
7th January 2008, 06:28
How do the links look like? What is current working directory?

manojmka
7th January 2008, 06:53
Current working directory is the directory where I am executing my project from! There are very funny things happening:
(1) It shows the online page perfectly.
(2) It does not show the offline images.
(3) It shows the images from some server in offline pages but that too only when I load the page with setHtml() function, not with load() function!!

How do I get it done??

jpn
7th January 2008, 07:10
Could you answer the questions, please?

What does

qDebug() << QDir::currentPath();
output?
In saved HTML page, what do missing images have as img-elements src attribute?

<img src="..."/>

manojmka
7th January 2008, 07:32
Hi!

The current Path returns:
C:/HTMLViewer

The image source is:
"img1.jpg"

I am passing html/test.htm as path for url and img1.jpg is stored in html folder.

BTW: Image is displayed if I give the src as: ftp::///C:/HTMLViewer/html/img1.jpg but I don't want to specify absolute paths here.

MarkoSan
7th January 2008, 07:46
Did you compile Qt with .jpg support?

jpn
7th January 2008, 07:48
So it was about current working directory after all. src="image.png" means that the image is searched from the current directory. I don't know if there is a possibility to set search paths or something, but presumably the images are shown once you launch the application from C:\HTMLViewer\html or you change it to src="html/image.png".

manojmka
7th January 2008, 08:52
Did you compile Qt with .jpg support?

I compiled QT with GIF support and I think that it is always compiled with jpg and mng support by default. I also told you that it is displaying images if I give the online location of images or the full absolute location with FTP.

manojmka
7th January 2008, 08:53
So it was about current working directory after all. src="image.png" means that the image is searched from the current directory. I don't know if there is a possibility to set search paths or something, but presumably the images are shown once you launch the application from C:\HTMLViewer\html or you change it to src="html/image.png".

No, it does not work that way even.

jpn
7th January 2008, 08:59
No, it does not work that way even.
So, exactly what did you try? It might prove to be useful to be as detailed as possible. "My car does not work" is not very useful information to a car mechanic. Does it print any warnings to debug output (provided that you have QT += console)?

manojmka
7th January 2008, 09:13
Well, I think I tried to write in detail but may be you can tell me what details are you expecting?

I have already stated that:
(1) It shows the online page perfectly.
(2) It does not show the offline images.
(3) It shows the images from some server in offline pages but that too only when I load the page with setHtml() function, not with load() function!!
(4) It also show the images from disk if I give them as: <img src=FTP:///C:/HTMLViewer/html/img1.jpg">

jpn
7th January 2008, 09:49
Start with enabling console so you can see the debug output, which might provide useful information.

Notice, that there is a significant difference when index.html contains src="image.png" and you do:

viewer->setHtml("html/index.html");
and launch the app in C:/HTMLViewer/

C:/HTMLViewer/> app.exe
versus when you do

viewer->setHtml("index.html");
and launch the app in C:/HTMLViewer/html

C:/HTMLViewer/html> ..\app.exe

Now that I take a look at QWebView docs, it seems that setHtml() takes an optional baseUrl parameter. Did you pass anything as baseUrl? All the relative paths are clearly relative to this baseUrl.

manojmka
7th January 2008, 10:09
Hi!

Thank you for telling me the benifits of console view. I started the same but did not get any information regarding paths in html file in fact, I did not get any information on the console view on loading the page itself.

I tried running the application both the ways, you describe but no luck.

I also tried to add the baseURL in setHtml function and that also did not produce any results. BTW, baseURL is only available in setHtml() function and that is not usable if I want to load a html page from some url. I will have to use load() method for this purpose.

manojmka
8th January 2008, 05:25
I beg the help of some trolltech guys here. I am really stuck at this!! I don't believe that a tool like webkit is not able to show up images from local disk.

jpn
8th January 2008, 06:37
What about <img src="file:///html/img1.jpg">?

manojmka
9th January 2008, 06:30
we have to provide the whole absolute file path along with file protocol i.e.
file:///c:/HTMLViewer/html/img1.jpg

manojmka
10th January 2008, 07:06
I am eagerly waiting for some of trolltech guys to answer this! Am I making some mistake here or it is a bug to be fixed?? Can I rely on the final release and wait for the same?

jacek
10th January 2008, 13:16
I am eagerly waiting for some of trolltech guys to answer this!
It's hard to find Trolltech employees here.

ModeZt
10th January 2008, 13:29
Coud u please post a chunk of your code loading a web-page and a chunk of html code.

1) make an index.html
<html><body><img src="img.png"></body></html>
2) put index.html and img.png into the same directory.
3) try to load the index.html

manojmka
11th January 2008, 07:29
Can you please tell me how to reach them to get the answer for this?

jacek
11th January 2008, 12:17
Can you please tell me how to reach them to get the answer for this?
You could try the qt4-preview-feedback list.

http://trolltech.com/company/newsroom/announcements/press.2007-12-17.6192777923

diogolr
25th July 2008, 16:53
Can someone solve this problem? I have the same problem...

If I load the html code with Firefox, for example, the images appears to be ok.

manojmka
26th July 2008, 11:53
Hi,

How are you describing the image path in html files? Is it relative? You need to use the full path somewhere, either directly in html files or full path to html files in your application.

Regards,
Manoj

diogolr
27th July 2008, 04:43
Thanks Manoj...:)

The following code works for me :cool::


QDir dir;

QString path = dir.absolutePath();

QString img_path = "\"%1/%2\"";
img_path = img_path.arg( path ).arg( "imgs/image_name.png" );

sorin
9th April 2009, 20:01
Manok

I had the same issues with my QWebView setHtml function.
It did load the html file but not the .png image that is located inside the subfolder /images under the current application directory:

<img style="border: 1px solid" alt="berlin" src="./images/berlin.png">

On my Unix box the current directory in src attribute above is shown as a dot "." not as a column ":"

Inside my Qt application that is using QWebView to see the html file I have this call to load the HTML in this way.

1. In order to load this image I had to create a new resource file qwebview.qrc
and put this file in the same folder as my application files.

In this qwebview.qrc file I reference my image file with the same realtive path specified above

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>images/berlin.png</file>
</qresource>
</RCC>

2. I add the resource qwebview.qrc file into the .pro file if my application under RESOURCES key as below:

CONFIG += qt debug
QT += webkit
SOURCES += main.cpp
RESOURCES += qwebview.qrc

3. I used my QWebView object and call setHTML with a QUrl that is pointing to qrc resource file find in the current directory as below.

MyWebView->setHtml(file.readAll(), QUrl("qrc:/"));

4. I got the images/berlin.png image file loaded in my HTML page but is true that having a qrc (RESOURCE file) inside my project would compile the .png file inside the application binary executable preserving the same original relative path as described in the < src attribute > and in the .qrc file. Maybe this tight connection from the compilation stage till the end make this woring.

5. I asked QT support people about addResource option to add an external file image inside the QWebView when an HTML file is loaded and they told me that the only way now to do it in Qt 4.5.0 is:


<<The only way to be able handle what is not handled by QWebPage is to utilize the unsupportedContent() signal, this is emitted when it is unable to handle a link that the user is trying to have loaded. Here you can place what you wanted in that frame. If you look at the browser demo then you can see this in operation in the webview.cpp file. Look at the WebPage::handleUnsupportedContent() function to see how to do this>>

I looked into that code is quite spooky.

haldrik
16th July 2012, 23:22
Hi, you must use the absolute path for the image in the html code:

I solved that, including html file loading at this way:



<img style="width: 300px; height: 84px;" alt="Image" src="%1" align="left">

The arg %1 in the src parameter is the key.


QString fileName = QDir::currentPath() + "/webs/page.html";
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, trUtf8("Error Opening File"),
trUtf8("Can't Open '%1'").arg(fileName));
} else {
// First, load the content file to a stream
QTextStream stream(&file);
QString htmlString;
// download all contents to a QString
htmlString = stream.readAll();
// re-download the html contents but, modifiying the parameter %1.
QString htmlWebView = htmlString.arg("file://" + QDir::currentPath() + "/webs/image.png");
WebView->setHtml(htmlWebView);
// finally you could use qDebug to show the result of the code
qDebug() << htmlWebView;
}