简体   繁体   English

如何重置QML WebView?

[英]How to reset QML WebView?

Looks. 看起来 Here is the simplest code: https://www.dropbox.com/s/ctdcgrk0eqgq3a3/webview_test.tar.gz 这是最简单的代码: https : //www.dropbox.com/s/ctdcgrk0eqgq3a3/webview_test.tar.gz

When you start this app you will see white window. 当您启动此应用时,您会看到白色的窗口。 Click on window and you will see browser with Twitter. 单击窗口,您将看到带有Twitter的浏览器。 Click again and browser will be hide and destroyed. 再次单击,浏览器将被隐藏并销毁。

So, please login to Twitter, than destroy browser by clicking first window and then create it again by clicking on first window. 因此,请登录到Twitter,然后单击第一个窗口销毁浏览器,然后单击第一个窗口再次创建浏览器。 You will see that you left signed in. But I need that browser every time opens without signed user (even if user checked "Remember me" checkbox). 您将看到您保持登录状态。但是,每次打开时都需要该浏览器,而无需登录用户(即使用户选中了“记住我”复选框)。 How to do this? 这个怎么做? How to reset browser to the initial state? 如何将浏览器重置为初始状态?

Here is main parts of project: 这是项目的主要部分:

main.cpp main.cpp

#include <QApplication>
#include <QQuickView>

#include "container.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Container c;
    c.start();

    return a.exec();
}

container.cpp container.cpp

#include "container.h"

#include <QDebug>
#include <QQuickItem>
#include <QQmlContext>

Container::Container(QObject *parent) :
    QObject(parent)
{
    mainFile = new QQuickView();

    browserExists = false;
}

Container::~Container()
{
    delete mainFile;
}

void Container::start()
{
    mainFile->setSource(QUrl("qrc:/main.qml"));

    connect(mainFile->rootObject(), SIGNAL(click()), this, SLOT(mainFileClicked()));

    mainFile->show();
}

void Container::mainFileClicked()
{
    if(!browserExists)
    {
        browser = new QQuickView();
        browser->setSource(QUrl("qrc:/browser.qml"));
        browser->rootContext()->setContextProperty("address", "http://twitter.com");

        browser->show();

        browserExists = true;
    }
    else
    {
        browser->hide();
        delete browser;

        browserExists = false;
    }
}

main.qml main.qml

import QtQuick 2.0

Rectangle {
    width: 320
    height: 240

    signal click();

    MouseArea {
        anchors.fill: parent

        onClicked:
        {
            click()
        }
    }
}

browser.qml browser.qml

import QtQuick 2.0
import QtWebKit 3.0
import QtWebKit.experimental 1.0

Item {
    width: 640
    height: 480

    signal closeWindow();

    WebView
    {
        anchors.fill: parent
        url: address
        experimental.preferences.privateBrowsingEnabled: true
    }
}

I think, that this answer is not necessary now, but I have the same problem with embeded QML WebView in my Sailfish application. 我认为,现在不需要此答案了,但是我在Sailfish应用程序中嵌入QML WebView时遇到了同样的问题。 Private browsing still doesn't work, so I have found dirty hack: remove cache from fs. 私人浏览仍然无法正常工作,因此我发现了肮脏的骇客:从fs中删除缓存。 In sailfish it stores in ~/.local/share/app-name/.QtWebKit. 在旗鱼中,它存储在〜/ .local / share / app-name / .QtWebKit中。 So, this dirty solution gives me possibility to use one webview with fresh state each time. 因此,这种肮脏的解决方案使我有可能每次都使用一个具有新状态的Webview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM