首先向大家说明一下相关的函数
static void QWebSettings::setIconDatabasePath(const
QString &location); // 指定肖像
数据库保存的本地路径
static QIcon QWebSettings::iconForUrl(const QUrl &url); // 返回关联url的肖像
QIcon QWebView::icon(); // 返回网页的肖像
//signal
void QWebView::iconChanged(); // 网页肖像改变的信号
下面请看几个不同的流程:
- // 下面的程序主要测试打开相应的网页的情况下 IconForUrl() 的异步工作模式
- // 在 iconChanged() 被触发的情况下,可以显示2次百度网站的肖像,输出是这样的
- /* icon changed
- valid icon
- valid icon
- */
- // 在 iconChanged() 未被触发的情况下,只有第二次能够正确显示百度肖像,输出是这样的
- /* null icon
- valid icon
- */
- // 得出的结论是这样的:这个异步模式必须有一个触发信号,
- // 要么是 iconChanged() ,要么是本地数据库读取成功(猜想的)
- #include <QApplication>
- #include <QtWebKit/QWebSettings>
- #include <QtWebKit/QWebView>
- #include <QPushButton>
- #include <qDebug>
- class Printer : public QObject
- {
- Q_OBJECT
- public slots:
- void iconchanged() {qDebug() << "icon changed";}
- void printIcon(const QIcon &icon)
- {
- if (icon.isNull()) {qDebug() << "null icon";}
- else {qDebug() << "valid icon";}
- }
- };
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWebSettings::setIconDatabasePath(".");
- QUrl douban("http://www.douban.com/");
- QUrl baidu("http://www.baidu.com/");
- //run this for only once and then comment it out.
- Printer printer;
- QWebView view;
- QObject::connect(&view, SIGNAL(iconChanged()), &printer, SLOT(iconchanged()));
- view.load(baidu);
- view.show();
- app.exec();
- QIcon icon = QWebSettings::iconForUrl(baidu);
- printer.printIcon(icon);
- QPushButton button;
- button.setIcon(icon);
- button.show();
- app.exec();
- icon = QWebSettings::iconForUrl(baidu);
- printer.printIcon(icon);
- button.setIcon(icon);
- button.show();
- return app.exec();
- }
- #include "main.moc"
- // 下面的程序主要测试未打开相应的网页的情况下 IconForUrl() 的异步工作模式
- // 第一次显示蓝色的圆形肖像,第二次不显示
- /* icon changed
- valid icon
- null icon
- */
- // 这里有2个疑问:不打开网页就读取不到网页的肖像?不是从本地数据缓冲读的吗?
- // 为什么第一个能返回一个默认的肖像而第二次返回的是NULL?
- #include <QApplication>
- #include <QtWebKit/QWebSettings>
- #include <QtWebKit/QWebView>
- #include <QPushButton>
- #include <qDebug>
- class Printer : public QObject
- {
- Q_OBJECT
- public slots:
- void iconchanged() {qDebug() << "icon changed";}
- void printIcon(const QIcon &icon)
- {
- if (icon.isNull()) {qDebug() << "null icon";}
- else {qDebug() << "valid icon";}
- }
- };
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWebSettings::setIconDatabasePath(".");
- QUrl douban("http://www.douban.com/");
- QUrl baidu("http://www.baidu.com/");
- //run this for only once and then comment it out.
- Printer printer;
- // QWebView view;
- // QObject::connect(&view, SIGNAL(iconChanged()), &printer, SLOT(iconchanged()));
- // view.load(baidu);
- // view.show();
- // app.exec();
- QIcon icon = QWebSettings::iconForUrl(baidu);
- printer.printIcon(icon);
- QPushButton button;
- button.setIcon(icon);
- button.show();
- app.exec();
- icon = QWebSettings::iconForUrl(baidu);
- printer.printIcon(icon);
- button.setIcon(icon);
- button.show();
- return app.exec();
- }
- #include "main.moc"
- // 下面的程序主要测试 QWebView::icon() 的有效性
- // 在 iconChanged() 被触发的情况下,可以显示2次百度网站的肖像,输出是这样的
- /* icon changed
- valid icon
- valid icon
- */
- // 在 iconChanged() 未被触发的情况下,只有第二次能够正确显示百度肖像,输出是这样的
- /* null icon
- valid icon
- */
- // 得出的结论是这样的:这个异步模式必须有一个触发信号,
- // 要么是 iconChanged() ,要么是本地数据库读取成功(猜想的)
- #include <QApplication>
- #include <QtWebKit/QWebSettings>
- #include <QtWebKit/QWebView>
- #include <QPushButton>
- #include <qDebug>
- class Printer : public QObject
- {
- Q_OBJECT
- public slots:
- void iconchanged() {qDebug() << "icon changed";}
- void printIcon(const QIcon &icon)
- {
- if (icon.isNull()) {qDebug() << "null icon";}
- else {qDebug() << "valid icon";}
- }
- };
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWebSettings::setIconDatabasePath(".");
- QUrl douban("http://www.douban.com/");
- QUrl baidu("http://www.baidu.com/");
- //run this for only once and then comment it out.
- Printer printer;
- QWebView view;
- QObject::connect(&view, SIGNAL(iconChanged()), &printer, SLOT(iconchanged()));
- view.load(baidu);
- view.show();
- app.exec();
- QIcon icon = view.icon();
- printer.printIcon(icon);
- QPushButton button;
- button.setIcon(view.icon());
- button.show();
- app.exec();
- icon = view.icon();
- printer.printIcon(icon);
- button.setIcon(view.icon());
- button.show();
- return app.exec();
- }
- #include "main.moc"
最大的疑问是:不打开相应的网页,你就无法从本地缓冲获取相应的网页肖像。我觉得这样很不合理。