1、在qt中实现通信类html2qt
html2qt.h
#ifndef HTML2QT_H
#define HTML2QT_H
#ifndef VIDEO_HEAD_H
#include <video_head.h>
#endif
#include <QSound>
#include <iostream>
#include <QSoundEffect>
#include <QMediaPlayer>
using namespace std;
class Html2Qt : public QObject
{
Q_OBJECT
public:
explicit Html2Qt(QObject *parent = 0);
string _str;
QString str;
QSoundEffect effect;
public slots:
void audioPlay(const QString &r_text);
};
#endif
html2qt.cpp
#include "html2qt.h"
using namespace std;
Html2Qt::Html2Qt(QObject *parent) : QObject(parent){
_str = "";
str = "";
}
void Html2Qt::audioPlay(const QString &r_text){
effect.stop();
_str = r_text.toStdString();
cout << _str << endl;
str = QString::fromStdString("/audio_wav/I" + _str + ".wav");
effect.setSource(QUrl::fromLocalFile(str));
effect.play();
}
2、QWidget类中对html2qt的运用
QWidget.cpp
view = new QWebEngineView(this);
html2Qt = new Html2Qt();
channel = new QWebChannel(this);
channel->registerObject(QStringLiteral("content"), html2Qt);
view->page()->setWebChannel(channel);
3、在html文件中加载qwebchannel.js
(qwebchannel.js下载地址:https://download.csdn.net/download/lu_android/10578832)
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../tv/css/main.css" />
<link rel="stylesheet" type="text/css" href="../tv/css/video.css" />
<script src="../tv/js//qwebchannel.js" type="text/javascript" charset="utf-8"></script>
</head>
4、在js文件中实现音频调用函数
var content;
var obj1 = new QWebChannel(qt.webChannelTransport, function(channel) {
// make dialog object accessible globally
content = channel.objects.content;
});
function radioPlay(statue){
content.audioPlay(statue);
}