PDA

View Full Version : Issues with QWebSettings::iconForURL()



sfcheng77
31st January 2011, 06:17
I am running to an issue with QWebSettings::iconForURL. Let's assume an icon of a certain web page has been cached in the icon database in a previous session. In a fresh new session, the first call to QWebSettings::iconForURL will always return null. I have to wait for some time and then make a second call. The second call will return a valid icon.

Here is the steps to produce the problem,

1. Cache the icon by using QWebView to navigate to the target website first. Then close the application.

2. Restart the application, the first call to QWebSettings::iconForURL for the same URL will return null.

3. Wait some time, make a second call to iconForURL, it will return a valid icon this time.

4. If the two calls are made continuously without delay, both times will return null.

So, to summarize, if an icon has been cached in a previous session, then the first call will return null. We have to wait some time and make a second call to get a valid icon. I believe a QWebView navigation also counts as a first time call. So, if you perform a navigation first and then make a call iconForURL in the same session, it will always succeed.

The following code serves as a test case:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWebSettings::setIconDatabasePath(".");

//run this for only once and then comment it out.
QWebView view;
view.load(QUrl("http://www.cnn.com/"));
view.show();
app.exec();

QIcon icon=QWebSettings::iconForUrl(QUrl("http://www.cnn.com/"));
QPushButton button;
button.setIcon(icon);
button.show();
app.exec();
icon=QWebSettings::iconForUrl(QUrl("http://www.cnn.com/"));
button.setIcon(icon);
button.show();
return app.exec();
}

Please cache the icon with QWebView when you run it for the first time. After that, comment out the code block related to QWebView. Then run it again. You will see that the first button has no icon while the second button has the cnn icon.