Qt+vue开发桌面应用程序(二)Vue部分介绍

5 篇文章 0 订阅

上一章介绍了Qt+vue开发桌面应用程序(一)Qt部分介绍,本文介绍Vue部分

一、原理介绍

主要是引入Qt5里面qwebchannel.js文件,再定义交互类和Qt5交互

二、代码实现

  1. 在Vue项目中的public目录,创建js目录,把qwebchannel.js文件复制到该目录,如图:
    在这里插入图片描述
  2. 修改qwebchannel.js文件,导出QWebChannel
export var QWebChannel = function(transport, initCallback) {
	if (typeof transport !== "object" || typeof transport.send !== "function") {
		console.error("The QWebChannel expects a transport object with a send function and onmessage callback property." +
			" Given is: transport: " + typeof(transport) + ", transport.send: " + typeof(transport.send));
		return;
	}

在"var QWebChannel"前面增加export,导出QWebChannel。Qt5里面自带的没有导出。

  1. 在main.js中导入qwebchannel.js
import {
	QWebChannel
} from '../public/js/qwebchannel.js'

export var qtWebChannel = null;  //导出qtWebChannel,供其他页面调用
new QWebChannel(qt.webChannelTransport, (channel) => {
	// all published objects are available in channel.objects under
	// the identifier set in their attached WebChannel.id property
	qtWebChannel = channel.objects.core;
});

注意:channel.objects.core中的core是在Qt代码中注册的,

QWebChannel *channel = new QWebChannel(this);
channel->registerObject(QStringLiteral("core"), &mCore);
  1. 页面中使用qtWebChannel
<script>
	import {
		qtWebChannel
	} from '../main.js'

	export default {
		name: 'login',
		props: {
			msg: String
		},
		components: {

		},
		data() {
			return {
				form: {
					username: '',
					password: '',
					record: false
				}
			}
		},
		mounted: function() {
			this.form.record = localStorage.getItem('record') == 'true' ? true : false;
			if (this.form.record) {
				this.form.username = localStorage.getItem('username');
			}
			this.autoLogin();

			// 测试连接信息,接受命令处理结果
			setTimeout(()=>{
				qtWebChannel.operateResult.connect((response) => {
					// alert("response: " + response);
					let result = JSON.parse(response);
					if (result.code == "0") {
						if (result.func == 'checkLogin') {
							localStorage.setItem('record', this.form.record);
							localStorage.setItem('username', this.form.username);
							this.$store.commit('setData', {
								'access_token': this.form.username,
								'userInfo': this.form.username,
								'groups': {
									"首页": [],
									"业务菜单": ["3D模型", "画图展示", "业务3"],
									"系统设置": ["用户管理", "系统日志"]
								},
								'roles': {
									"首页": ["读"],
									"3D模型": ["读", "写"],
									"业务2": ["读", "写"],
									"业务3": ["读", "写"],
									"用户管理": ["读", "写"],
									"系统日志": ["读", "写"]
								}
							});
							this.$router.push('/home');
							this.form.password = '';
						}
					} else {
						this.$message({
							message: result.msg,
							type: 'warning'
						});
					}
				});
			}, 1000);
		},
		methods: {
			async login() {
				if (this.form.username == "" || this.form.password == "") {
					this.$message({
						message: '请输入用户名和密码',
						type: 'warning'
					});
				} else {
					let argc = {
						'username': this.form.username,
						'password': this.form.password
					};

					//qt+vue
					qtWebChannel.handleCmd('checkLogin', ['username', 'password'],
						[this.form.username, this.form.password]);

				}
			},
			selectFile() {
				qtWebChannel.handleCmd('selectFile', [], []);
			},
			selectDir() {
				qtWebChannel.handleCmd('selectDir', [], []);
			}
		}
	}
</script>

说明:

  • 在mounted里面接收Qt5处理结果,qtWebChannel.operateResult.connect((response) => {}),operateResult是在Qt里面定义的信号,用于发送处理结果
  • 要等main.js里面new QWebChannel完成后,才在mounted
    里面绑定信号,否则提示qtWebChannel里面找不到operateResult,所以这里用setTimeout,一秒延迟绑定
  • 通过qtWebChannel.handleCmd与Qt5交互,handleCmd是Qt5里面定义的函数

三、源码文件

源码地址:https://download.csdn.net/download/yyt593891927/12838793
默认用户名:admin
默认密码:admin

四、后记

Qt+vue开发桌面应用程序Qt部分和Vue部分都介绍完了,下一章介绍《Qt+vue开发桌面应用程序(三)程序打包》

  • 5
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁爸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值