-1

私は Qt を初めて使用し、Web ビューアーをアプリに追加しようとしています。

3 つのウィンドウと 3 つのボタンを持つナビゲーション バーを備えたアプリがあります。ボタンをクリックすると、関連するウィンドウにスワイプします。

このボタンの 1 つを使用して、アプリを終了せずに Web ビューアーを開きます。

アプリはモバイル (Android、iOS、Windows Phone) とも互換性がある必要があります。

QtWebengine を検索して見つけましたが、実際には役に立ちません...

私は Qt Creator 3.4.2、Qt 5.5.0 を使用しており、Qt Designer を使用しています (重要かどうかはわかりません...)

C++ と QML でのコーディング。

ありがとう。

編集: Webview doc について読みましたが、まだ混乱しています... Webkit Webview と WbeEngine Webview があることがわかりました。Webkit は非推奨になっているので、WebEngine を使用したいと考えています。そこで、WebEngine Webview を使用する MiniBrowser Example を試してみましたが、必要なプラットフォームで動作します。しかし、ボタンをクリックして起動する方法がわかりません...

私はこれを試しました:

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

メインウィンドウ.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slot_test()));
}

void MainWindow::slot_test()
{
    this->webview();
}

MainWindow::~MainWindow()
{
    delete ui;
}

webview.cpp

#include <QtCore/QUrl>

#include <QQmlApplicationEngine>

#include <QtCore/QCommandLineOption>
#include <QtCore/QCommandLineParser>
#include <QGuiApplication>
#include <QStyleHints>
#include <QScreen>

#include <QtQml/QQmlContext>
#include <mainwindow.h> //

#ifdef QT_WEBVIEW_WEBENGINE_BACKEND
#include <QtWebEngine>
#endif /* QT_WEBVIEW_WEBENGINE_BACKEND */

void MainWindow::webview()
{
#ifdef Q_OS_OSX
    // On OS X, correct WebView / QtQuick compositing and stacking requires running
    // Qt in layer-backed mode, which again resuires rendering on the Gui thread.
    qWarning("Setting QT_MAC_WANTS_LAYER=1 and QSG_RENDER_LOOP=basic");
    qputenv("QT_MAC_WANTS_LAYER", "1");
    qputenv("QSG_RENDER_LOOP", "basic");
#endif /* Q_OS_OSX */

#ifdef QT_WEBVIEW_WEBENGINE_BACKEND
    QtWebEngine::initialize();
#endif /* QT_WEBVIEW_WEBENGINE_BACKEND */

    QQmlApplicationEngine engine;

    engine.load(QUrl(QStringLiteral("qrc:/base/main.qml")));

}

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtWebView 1.0
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2

ApplicationWindow {
    visible: true
    x: 500
    y: 500
    width: 500
    height: 500
    title: webView.title
    statusBar: StatusBar {
        id: statusBar
        visible: webView.loading && Qt.platform.os !== "ios"
        RowLayout {
            anchors.fill: parent
            Label { text: webView.loadProgress == 100 ? qsTr("Done") : qsTr("Loading: ") + webView.loadProgress + "%" }
        }
    }

    WebView {
        id: webView
        anchors.fill: parent
        url: "https://www.google.fr"
    }
}

(webview.cpp は MiniBrowser の例の簡易版です)

(デスクトップ バージョンまたは Android で) 起動してプッシュ ボタンをクリックすると、Webview が別のウィンドウで開き、すぐに閉じます。これを解決する方法がわかりません...

4

1 に答える 1

0

QtWebKit を使用できますが、非推奨です: http://doc.qt.io/qt-5/qtwebkit-index.html

QtWebEngine を使用できますが、必要なすべてのプラットフォームをサポートしているわけではありません。

于 2015-09-23T15:48:27.870 に答える