当前位置: 首页 > news >正文

网站定制建设公司启信宝企业查询官网

网站定制建设公司,启信宝企业查询官网,建立网站分为几阶段,让自己的电脑做网站的服务器某些 Qt API 要求你从抽象基类中重写某些方法,例如 QAbstractItemModel。 为了支持直接从 Rust 中创建这样的子类,CXX-Qt 提供了多种辅助工具。 某些基类可能需要特殊的构造参数。这可以通过使用自定义构造函数来实现。 访问基类方法 要在 Rust 中访…

某些 Qt API 要求你从抽象基类中重写某些方法,例如 QAbstractItemModel。

为了支持直接从 Rust 中创建这样的子类,CXX-Qt 提供了多种辅助工具。

某些基类可能需要特殊的构造参数。这可以通过使用自定义构造函数来实现。

访问基类方法

要在 Rust 中访问基类的方法,请使用 #[inherit] 宏。它可以放在 #[cxx_qt::bridge] 中的 extern “RustQt” 块中的函数前面。

extern "RustQt" {#[qobject]#[base = "QAbstractListModel"]#[qml_element]#[qproperty(State, state)]type CustomBaseClass = super::CustomBaseClassRust;
}// 为基类(QAbstractItemModel)的 C++ 函数创建 Rust 绑定
extern "RustQt" {/// 从基类继承的 beginInsertRows 方法#[inherit]#[cxx_name = "beginInsertRows"]unsafe fn begin_insert_rows(self: Pin<&mut CustomBaseClass>,parent: &QModelIndex,first: i32,last: i32,);/// 从基类继承的 endInsertRows 方法#[inherit]#[cxx_name = "endInsertRows"]unsafe fn end_insert_rows(self: Pin<&mut CustomBaseClass>);/// 从基类继承的 beginRemoveRows 方法#[inherit]#[cxx_name = "beginRemoveRows"]unsafe fn begin_remove_rows(self: Pin<&mut CustomBaseClass>,parent: &QModelIndex,first: i32,last: i32,);/// 从基类继承的 endRemoveRows 方法#[inherit]#[cxx_name = "endRemoveRows"]unsafe fn end_remove_rows(self: Pin<&mut CustomBaseClass>);/// 从基类继承的 beginResetModel 方法#[inherit]#[cxx_name = "beginResetModel"]unsafe fn begin_reset_model(self: Pin<&mut CustomBaseClass>);/// 从基类继承的 endResetModel 方法#[inherit]#[cxx_name = "endResetModel"]unsafe fn end_reset_model(self: Pin<&mut CustomBaseClass>);
}unsafe extern "RustQt" {/// 清除 QAbstractListModel 中的行#[qinvokable]pub fn clear(self: Pin<&mut CustomBaseClass>);
}impl qobject::CustomBaseClass {/// 清除 QAbstractListModel 中的行pub fn clear(mut self: Pin<&mut Self>) {unsafe {self.as_mut().begin_reset_model();self.as_mut().rust_mut().id = 0;self.as_mut().rust_mut().vector.clear();self.as_mut().end_reset_model();}}
}

完整示例

这段代码实现了一个 QAbstractListModel 子类。为此,Rust 中实现的 clear 方法需要调用基类中的 beginResetModel 及相关方法,这些方法通过使用 #[inherit] 来访问。有关特定子类化要求的更多详细信息,请参阅 Qt 文档。

类似于 CXX,extern “RustQt” 块中的方法可以使用 #[inherit] 属性标记,并且对可使用的类型有相同的限制。此外,self 类型必须是 self: Pin<&mut qobject::T> 或 self: &qobject::T,其中 qobject::T 必须引用在 #[cxx_qt::bridge] 中用 #[qobject] 标记的 QObject。

如果 Rust 名称应与 C++ 方法名称不同(例如,由于 snake_case 与 camelCase 的差异),请使用 #[cxx_name = “myFunctionName”] 或 #[rust_name = “my_function_name”] 属性。

#[inherit]` 也可以用于 `extern RustQt` 块中存在于基类上的信号。

重写基类方法

CXX-Qt 允许生成具有实现继承所需的 C++ 修饰符的可调用方法。这样,方法可以被重写、声明为 virtual 或 final。

C++ 关键字 CXX-Qt 属性
override #[cxx_override]
virtual #[cxx_virtual]
final #[cxx_final]
以下示例重写了从 QAbstractListModel 继承的 data 方法。

extern "RustQt" {#[qobject]#[base = "QAbstractListModel"]#[qml_element]#[qproperty(State, state)]type CustomBaseClass = super::CustomBaseClassRust;
}unsafe extern "RustQt" {#[qinvokable]#[cxx_override]fn data(self: &CustomBaseClass, index: &QModelIndex, role: i32) -> QVariant;
}impl qobject::CustomBaseClass {/// 检索给定索引和角色的数据pub fn data(&self, index: &QModelIndex, role: i32) -> QVariant {let role = qobject::Roles { repr: role };if let Some((id, value)) = self.vector.get(index.row() as usize) {return match role {qobject::Roles::Id => QVariant::from(id),qobject::Roles::Value => QVariant::from(value),_ => QVariant::default(),};}QVariant::default()}
}

完整示例

当使用 cxx_override 重写方法时,可以通过结合使用 #[inherit] 和 #[cxx_name] 属性来访问基类版本的方法。在这种情况下,基类版本的函数必须使用不同的名称,因为 Rust 不能在一个类型上拥有两个同名函数。

示例:

extern "RustQt" {#[qobject]#[base = "QAbstractListModel"]#[qml_element]#[qproperty(State, state)]type CustomBaseClass = super::CustomBaseClassRust;
}unsafe extern "RustQt" {/// 从基类继承的 canFetchMore 方法#[cxx_name = "canFetchMore"]#[inherit]fn base_can_fetch_more(self: &CustomBaseClass, parent: &QModelIndex) -> bool;/// 从基类继承的 index 方法#[inherit]fn index(self: &CustomBaseClass,row: i32,column: i32,parent: &QModelIndex,) -> QModelIndex;
}unsafe extern "RustQt" {/// 返回基类是否可以获取更多数据// 示例:重写 C++ 虚方法并调用基类实现。#[qinvokable]#[cxx_override]#[cxx_name = "canFetchMore"]fn can_fetch_more(self: &CustomBaseClass, parent: &QModelIndex) -> bool;
}impl qobject::CustomBaseClass {/// 返回基类是否可以获取更多数据// 示例:重写 C++ 虚方法并调用基类实现。pub fn can_fetch_more(&self, parent: &QModelIndex) -> bool {self.base_can_fetch_more(parent)}
}
http://www.dtcms.com/a/486320.html

相关文章:

  • html案例:制作一个图片水印生成器,防止复印件被滥用
  • 最新版谷歌浏览器集成知笺云阅读器控件介绍
  • 嘉定装饰装修网站企业网络营销青岛
  • break,continue练习题
  • 【Ubuntu 24.04.3 LTS(Noble Numbat)】移动硬盘数据提取操作手册
  • 网站开发需求分析与功能设计互联网线上推广是什么工作
  • 做网站前应该怎么处理微信推广文案范文
  • 35.渗透-.Kali Linux-工具-反弹shell生成器
  • 便携式水质监测仪——快速锁定水质污染
  • Redis String原理
  • 旅游网站功能流程图php wordpress教程
  • adminPage-vue3依赖LoadingWrap说明文档,表单页快速开发,使用思路及范例-汇总
  • 八股已死、场景当立(场景篇-JVM)
  • 【MySQL】主从复制
  • C4D域的常规修改层:功能详解与实用技巧
  • 网站后台管理系统模板仿西部数码网站
  • 外贸网站电子建设网站免费推广平台有哪些
  • 【汽车篇】AI深度学习在汽车轮胎X-ray缺陷检测应用方案
  • Jmeter循环控制器,IF控制器,正则表达式
  • 【qt学习】day1登录界面模仿
  • 一款优秀的桌面辅助软件
  • 2025-陇剑杯决赛-ezTraffic
  • 【Qt】1.安装QT
  • Spring AI 番外篇01:MCP Streamable HTTP 模式
  • 【GUI自动化测试】Python 自动化测试框架 pytest 全面指南:基础语法、核心特性(参数化 / Fixture)及项目实操
  • Vue3中组件间的数据传递【6】
  • nginx-1.16.1-2.p01.ky10.sw_64.rpm 安装教程(详细步骤,适用于Kylin V10/SW64架构)
  • 教育培训机构如何开发搭建自己的微信小程序?
  • 微软AutoGen:多智能体AI开发新利器
  • vscode的Verilog/SystemVerilog Tools(Andrew Nolte)插件简单配置