我尝试创建一个简单的 QT 应用程序,其中前端是 HTML 和本机后端。我使用来自 QtWebView (QML) 和 QWebChannel 示例的代码来构建一个小型演示。
如果我启动应用程序的发布版本,一切都会按预期工作,但您无法真正调试发布版本。如果我开始调试构建,应用程序将启动,然后它会停止,就好像遇到了调试断点(我没有设置断点),并且在我按下恢复后,应用程序将永远挂起。
我目前正在使用 Windows 7 x86 机器进行开发。QT 版本是 5.5。
我将演示应用程序上传到我的谷歌驱动器:https ://drive.google.com/file/d/0Byc0Zui_4XuxaUc5MmJlWlo0M28/view?usp=sharing
任何想法为什么调试版本不起作用?
编辑:这是我的代码,根据要求。
测试套件专业版
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
主文件
#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
}
}
索引.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>
脚本.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;
});
}