diff --git a/example/example.pro b/example/example.pro index 7b98e1bf2ddcf0bec79dd2e967ab135c8c0763e9..393ec6a4dc9e50a87ef2bb81a6d439d2662c5fde 100644 --- a/example/example.pro +++ b/example/example.pro @@ -4,6 +4,8 @@ TARGET = example HEADERS = quickwindow.h widgetwindow.h SOURCES = quickwindow.cpp widgetwindow.cpp main.cpp +OTHER_FILES += quickwindow.qml + INCLUDEPATH += ../lib LIBPATH = $$getOutDir()/$$getConfigDir()/lib @@ -13,3 +15,4 @@ QMAKE_RPATHDIR += $$LIBPATH QT += widgets quick MOC_DIR=$$PWD + diff --git a/example/quickwindow.cpp b/example/quickwindow.cpp index 92cabb9eac10f8feffa7fc31e8a122e4f5fce06e..5b0ed1d9ba31663d5e3b9d147d3a8b26a4ab86eb 100644 --- a/example/quickwindow.cpp +++ b/example/quickwindow.cpp @@ -41,8 +41,30 @@ #include "quickwindow.h" +#include <QFileInfo> +#include <QObject> +#include <QQmlContext> +#include <QQmlEngine> +#include <QUrl> + +class Utils : public QObject { + Q_OBJECT +public: + Utils(QObject* parent = 0) : QObject(parent) { } + Q_INVOKABLE static QUrl fromUserInput(const QString& userInput) + { + QFileInfo fileInfo(userInput); + if (fileInfo.exists()) + return QUrl(fileInfo.absoluteFilePath()); + return QUrl::fromUserInput(userInput); + } +}; + +#include "quickwindow.moc" + QuickWindow::QuickWindow() { + engine()->rootContext()->setContextProperty("utils", new Utils(this)); setSource(QUrl("example/quickwindow.qml")); setResizeMode(QQuickView::SizeRootObjectToView); setTitle("QQuick Example"); diff --git a/example/quickwindow.qml b/example/quickwindow.qml index b424d959cd86911cd8bd0bdc34a9afda3cd1f58d..9c90e7241f38d37931f232db0a566a7a33ad7937 100644 --- a/example/quickwindow.qml +++ b/example/quickwindow.qml @@ -72,7 +72,7 @@ Item { selectByMouse: true onAccepted: { - webContentsView.url = text + webContentsView.url = utils.fromUserInput(text) } } } diff --git a/lib/qquickwebcontentsview.cpp b/lib/qquickwebcontentsview.cpp index 1d7357cd0084831c02b9066f22b75748cff0cd25..469b030fd18a3bdf9438876f9b5bf9c3ed3896cf 100644 --- a/lib/qquickwebcontentsview.cpp +++ b/lib/qquickwebcontentsview.cpp @@ -81,10 +81,7 @@ QUrl QQuickWebContentsView::url() const void QQuickWebContentsView::setUrl(const QUrl& url) { - QString urlString = url.toString(); - GURL gurl(urlString.toStdString()); - if (!gurl.has_scheme()) - gurl = GURL(std::string("http://") + urlString.toStdString()); + GURL gurl(url.toString().toStdString()); content::NavigationController::LoadURLParams params(gurl); params.transition_type = content::PageTransitionFromInt(content::PAGE_TRANSITION_TYPED | content::PAGE_TRANSITION_FROM_ADDRESS_BAR);