Hands-On GUI Programming with C++ and Qt5学习笔记 - 从 C++ JavaScript相互调用

cpp call js

在这里插入图片描述

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWebEngineView>
#include <QWebChannel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
	Q_OBJECT
public:
	explicit MainWindow(QWidget *parent = 0);
	~MainWindow();
	void paintEvent(QPaintEvent *event);
	//被js调用
	Q_INVOKABLE void doSomething();
private slots:
	void on_pushButton_clicked();

private:
	Ui::MainWindow *ui;
	QWebEngineView* webview;
};

#endif // MAINWINDOW_H

mainwindow.cpp

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

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	webview = new QWebEngineView(ui->verticalFrame);
	webview->load(QUrl("qrc:///html/test.html"));

	QWebChannel* channel = new QWebChannel(this);
	channel->registerObject("mainwindow", this);
	webview->page()->setWebChannel(channel);
}

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

void MainWindow::paintEvent(QPaintEvent *event)
{
   QMainWindow::paintEvent(event);

   webview->resize(ui->verticalFrame->size());
}

void MainWindow::doSomething()
{
	ui->label->setText("This text has been changed by javascript!");
}

void MainWindow::on_pushButton_clicked()
{
	webview->page()->runJavaScript("hello();"); //调用js
}

test.html

<!DOCTYPE html>
<html>
	<head>
		<title>Page Title</title>
		<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
		<script>
			var mainwindow;	
			function hello()
			{
				document.getElementById("myText").innerHTML = "Something happened!";
				alert("Good day sir, how are you?");
			}
			
			window.onload = function()
			{
				new QWebChannel(qt.webChannelTransport, function(channel)
				{
					mainwindow = channel.objects.mainwindow;
				});
			}
			
			function myFunction()
			{
				//调用c++函数
				mainwindow.doSomething();
			}
		</script>
	</head>
	<body>
		<p id="myText">Hello World!</p>
		<button onclick="myFunction()">Do Something</button>
	</body>
</html>

Hands-On High Performance Programming with Qt 5: Build cross-platform applications using concurrency, parallel programming, and memory management Author: Marek Krajewski Pub Date: 2019 ISBN: 978-1789531244 Pages: 384 Language: English Format: EPUB Size: 17 Mb Build efficient and fast Qt applications, target performance problems, and discover solutions to refine your code Achieving efficient code through performance tuning is one of the key challenges faced by many programmers. This book looks at Qt programming from a performance perspective. You’ll explore the performance problems encountered when using the Qt framework and means and ways to resolve them and optimize performance. The book highlights performance improvements and new features released in Qt 5.9, Qt 5.11, and 5.12 (LTE). You’ll master general computer performance best practices and tools, which can help you identify the reasons behind low performance, and the most common performance pitfalls experienced when using the Qt framework. In the following chapters, you’ll explore multithreading and asynchronous programming with C++ and Qt and learn the importance and efficient use of data structures. You’ll also get the opportunity to work through techniques such as memory management and design guidelines, which are essential to improve application performance. Comprehensive sections that cover all these concepts will prepare you for gaining hands-on experience of some of Qt’s most exciting application fields – the mobile and embedded development domains. By the end of this book, you’ll be ready to build Qt applications that are more efficient, concurrent, and performance-oriented in nature What you will learn Understand classic performance best practices Get to grips with modern hardware architecture and its performance impact Implement tools and procedures used in performance optimization Grasp Qt-specific work techniques for graphical user interface (GUI) and platform programming Make Transmission Control Protocol (TCP) and Hypertext Transfer Protocol (HTTP) performant and use the relevant Qt classes Discover the improvements Qt 5.9 (and the upcoming versions) holds in store Explore Qt’s graphic engine architecture, strengths, and weaknesses
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值