qt_demoe/other/echartgauge/widget.cpp

90 lines
2.5 KiB
C++
Raw Normal View History

2020-01-04 06:46:39 +00:00
#include "widget.h"
#include "ui_widget.h"
2021-11-17 07:33:19 +00:00
#include "qurl.h"
#ifdef webkit
#include <QtWebKit>
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include <QtWebKitWidgets>
#endif
#elif webengine
#include <QtWebEngineWidgets>
#endif
2020-01-04 06:46:39 +00:00
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
this->initForm();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initForm()
{
2022-05-27 07:40:19 +00:00
//初始化浏览器控件属性,一个项目中只需要设置一次就行
2020-01-04 06:46:39 +00:00
#ifdef webkit
QWebSettings *webSetting = QWebSettings::globalSettings();
webSetting->setAttribute(QWebSettings::JavascriptEnabled, true);
webSetting->setAttribute(QWebSettings::PluginsEnabled, true);
webSetting->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
2021-11-17 07:33:19 +00:00
#elif webengine
QWebEngineSettings *webSetting = QWebEngineProfile::defaultProfile()->settings();
2020-01-04 06:46:39 +00:00
webSetting->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
webSetting->setAttribute(QWebEngineSettings::PluginsEnabled, true);
webSetting->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
#endif
2022-05-27 07:40:19 +00:00
//实例化多个浏览器控件,设置背景透明
2020-01-04 06:46:39 +00:00
#ifdef webkit
2022-05-27 07:40:19 +00:00
for (int i = 0; i < 4; ++i) {
QWebView *webView = new QWebView;
webView->setStyleSheet(QString("background:%1;").arg("rgba(255,0,0,0)"));
webViews << webView;
}
2021-11-17 07:33:19 +00:00
#elif webengine
2022-05-27 07:40:19 +00:00
for (int i = 0; i < 4; ++i) {
QWebEngineView *webView = new QWebEngineView;
webView->page()->setBackgroundColor(Qt::transparent);
webViews << webView;
}
2020-01-04 06:46:39 +00:00
#endif
2022-05-27 07:40:19 +00:00
//添加到布局
2021-11-17 07:33:19 +00:00
#if (defined webkit) || (defined webengine)
2022-05-27 07:40:19 +00:00
ui->gridLayout->addWidget(webViews.at(0), 0, 0);
ui->gridLayout->addWidget(webViews.at(1), 0, 1);
ui->gridLayout->addWidget(webViews.at(2), 1, 0);
ui->gridLayout->addWidget(webViews.at(3), 1, 1);
#endif
2020-01-04 06:46:39 +00:00
2022-05-27 07:40:19 +00:00
//加载html文件
QUrl url("file:///" + qApp->applicationDirPath() + "/gauge.html");
#ifdef webkit
foreach (QWebView *webView, webViews) {
webView->load(url);
}
#elif webengine
foreach (QWebEngineView *webView, webViews) {
webView->load(url);
}
2021-11-17 07:33:19 +00:00
#endif
2020-01-04 06:46:39 +00:00
}
void Widget::on_horizontalSlider_valueChanged(int value)
{
2022-05-27 07:40:19 +00:00
//执行js函数
2020-01-04 06:53:35 +00:00
QString js = QString("setGaugeValue(%1)").arg(value);
2020-01-04 06:46:39 +00:00
#ifdef webkit
2022-05-27 07:40:19 +00:00
foreach (QWebView *webView, webViews) {
webView->page()->mainFrame()->evaluateJavaScript(js);
}
2021-11-17 07:33:19 +00:00
#elif webengine
2022-05-27 07:40:19 +00:00
foreach (QWebEngineView *webView, webViews) {
webView->page()->runJavaScript(js);
}
2020-01-04 06:46:39 +00:00
#endif
}