我尝试创建一个简单的QT应用程序,其中前端是HTML和本机后端。我使用QtWebView(QML)和QWebChannel示例中的代码构建了一个小型演示。
如果我启动应用程序的发布版本,一切都按预期工作,除了,您无法真正调试版本构建。如果我启动调试版本,应用程序启动,然后停止,好像调试断点被点击(我没有设置断点),在我按下恢复后,应用程序永远挂起。
我目前正在使用Windows 7 x86机器进行开发。 QT版本是5.5。
我将演示应用程序上传到我的google驱动器:https://drive.google.com/file/d/0Byc0Zui_4XuxaUc5MmJlWlo0M28/view?usp=sharing
为什么调试版本无法正常工作?
编辑:根据要求,这是我的代码。
TestSuite.pro
TEMPLATE = app
QT += qml quick webengine webchannel websockets
CONFIG += c++11
SOURCES += main.cpp \
shared/websocketclientwrapper.cpp \
shared/websockettransport.cpp \
Log.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
shared/websocketclientwrapper.h \
shared/websockettransport.h \
Log.h
的main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtWebEngine/QtWebEngine>
#include <QWebSocketServer>
#include <QWebChannel>
// From the webchannel example
#include "shared/websocketclientwrapper.h"
#include "shared/websockettransport.h"
// Simple class with 1 method "void logMessage(const QString &msg);" which writes a string to qDebug()
#include"Log.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtWebEngine::initialize();
//setup WebSocket server
QWebSocketServer server(QStringLiteral("QWebChannelServer"), QWebSocketServer::NonSecureMode);
if (!server.listen(QHostAddress::LocalHost, 12344)) {
qFatal("Failed to open web socket server.");
return 1;
}
//wrap WebSocket clients in QWebChannelAbstractTransport objects
//see:qtwebchannel/examples/webchannel/standalone
WebSocketClientWrapper clientWrapper(&server);
//setup the channel and connect to WebSocket clients
QWebChannel channel;
// Register Objects
Log l;
channel.registerObject("Log", &l);
// Start Application
QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected,
&channel, &QWebChannel::connectTo);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.0
import QtQuick.Window 2.2
import QtWebEngine 1.1
Window {
width: 1280
height: 720
visible: true
WebEngineView {
id: webview
url: "qrc:/index.html"
anchors.fill: parent
}
}
的index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>m2suite</title>
<script type="text/javascript" src="qtwebchannel/qwebchannel.js"></script>
<script type="text/javascript" src="script/main.js"></script>
</head>
<body>
<a onclick="Log.logMessage('test')">Log.sendMessage</a><br>
</body>
</html>
的script.js
var Log;
var socket = new WebSocket("ws://127.0.0.1:12344/");
socket.onopen=function(){
var c = new QWebChannel(socket, function(channel) {
//connection to server succeeded, objectsavailablevia:
Log = channel.objects.Log;
});
}