从qml端发送坐标点到c++端。使用上下文把c++的对象暴露到qml端,然后直接调用c++的函数

news/2025/2/6 9:35:36 标签: qml

在这里插入图片描述
在这里插入图片描述

coordinatereceiver.h

#include <QObject>
#include <QPointF>
#include <QVector>
#include <QDebug>
// 定义全局变量来存储坐标点
//QVector<QPointF> globalCoordinates;

class CoordinateReceiver : public QObject
{
    Q_OBJECT
public:
    explicit CoordinateReceiver(QObject *parent = nullptr) : QObject(parent) {}

public slots:
    // 用于接收 QML 发送的坐标点列表
    void receiveCoordinates(QVector<QPointF> coordinates1) {

        for (const auto &point : coordinates1) {
            qDebug() << "Coordinate:" << point;
        }
    }
};

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "coordinatereceiver.h"

int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    CoordinateReceiver handler;
    engine.rootContext()->setContextProperty("CoordinateReceiver", &handler);

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}


import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: "Send Coordinates to C++"

    Button {
        id: sendButton
        text: "Send Coordinates"
        anchors.centerIn: parent
        onClicked: {
            var coordinates = []
            for (var i = 0; i < 30; ++i) {
                // 这里简单生成一些示例坐标点,你可以根据实际需求修改
                coordinates.push(Qt.point(i * 10, i * 20))
            }
            // 发送坐标点到 C++ 端
            CoordinateReceiver.receiveCoordinates(coordinates)
        }
    }
}


http://www.niftyadmin.cn/n/5842831.html

相关文章

react关于手搓antd pro面包屑的经验(写的不好请见谅)

我们先上代码&#xff0c;代码里面都有注释&#xff0c;我是单独写了一个组件&#xff0c;方便使用&#xff0c;在其他页面引入就行了 还使用了官方的Breadcrumb组件 import React, { useEffect, useState } from react; import { Breadcrumb, Button } from antd; import { …

双亲委派(jvm)

1.双亲委派 在 Java 中&#xff0c;双薪委派通常是指双亲委派模型&#xff0c;它是 Java 类加载器的一种工作模式&#xff0c;用于确保类加载的安全性和一致性。以下是其相关介绍&#xff1a; 定义与作用 定义&#xff1a;双亲委派模型要求除了顶层的启动类加载器外&#xf…

SAP HCM 读取特定0014信息类型(特定月份)数据

导读 0014信息类型:0014是HCM的周期性维护数据&#xff0c;也就是说默认我维护的周期时间很长&#xff0c;在一段时间内不需要维护&#xff0c;减少维护的工作量&#xff0c;今天遇到一个朋友问的问题&#xff0c;0014信息类型能读取特定月份的数据&#xff0c;例如我需要维护…

ES6 变量解构赋值总结

1. 数组的解构赋值 1.1 基本用法 // 基本数组解构 const [a, b, c] [1, 2, 3]; console.log(a); // 1 console.log(b); // 2 console.log(c); // 3// 跳过某些值 const [x, , y] [1, 2, 3]; console.log(x); // 1 console.log(y); // 3// 解构剩余元素 const [first, ...re…

在本地快速部署deepseek

第一步&#xff0c;找到网站&#xff0c;下载&#xff1a; 首先找到Ollama &#xff0c; 根据自己的电脑下载对应的版本 。 我个人用的是Windows 我就先尝试用Windows版本了 &#xff0c;文件不是很大&#xff0c;下载也比较的快 第二部就是安装了 &#xff1a; 安装完成后提示…

12.外观模式(Facade Pattern)

定义 外观模式&#xff08;Facade Pattern&#xff09; 是一种结构型设计模式&#xff0c;它通过为复杂的子系统提供一个统一的接口&#xff0c;使得子系统的使用更加简化。外观模式通常隐藏了复杂的内部子系统&#xff0c;使得客户端可以通过一个简单的接口与这些子系统进行交…

11.享元模式 (Flyweight)

定义 Flyweight 模式&#xff08;享元模式&#xff09; 是一种结构型设计模式&#xff0c;它旨在通过共享对象来有效支持大量细粒度对象的复用。该模式主要通过共享细节来减少内存使用&#xff0c;提升性能&#xff0c;尤其在需要大量对象时非常有效。 基本思想&#xff1a; …

Security(四)授权模块详解:注解授权

Security&#xff08;四&#xff09;授权模块详解&#xff1a;注解授权 前言 &#xff1a;项目中常用的授权方式就是注解授权&#xff0c;这里之讲解注解授权。 文章目录 Security&#xff08;四&#xff09;授权模块详解&#xff1a;注解授权前言 &#xff1a;项目中常用的授…