From e80719ed2714779cd4afac099916be069742fe77 Mon Sep 17 00:00:00 2001 From: Pierre Rossi <pierre.rossi@digia.com> Date: Tue, 11 Jun 2013 15:16:50 +0200 Subject: [PATCH] Use QUrl::fromUserInput in the QtQuick view --- example/example.pro | 3 +++ example/quickwindow.cpp | 22 ++++++++++++++++++++++ example/quickwindow.qml | 2 +- lib/qquickwebcontentsview.cpp | 5 +---- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/example/example.pro b/example/example.pro index 7b98e1bf2..393ec6a4d 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 92cabb9ea..5b0ed1d9b 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 b424d959c..9c90e7241 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 1d7357cd0..469b030fd 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); -- GitLab