侧边栏壁纸
博主头像
fynn博主等级

我们应该有恒心,尤其是要有自信心,必须相信自己是有能力的,而且要不惜任何代价把这种能力发挥出来。

  • 累计撰写 50 篇文章
  • 累计创建 21 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Rust使用Qt开发图形化程序(一)安装环境

fynn
2023-09-29 / 0 评论 / 0 点赞 / 70 阅读 / 532 字 / 正在检测是否收录...

安装cxx-qt

安装Qt5 / Qt6

  • 最低需要安装 Qt 5.15
  • Q t5.15及以后的版本不支持离线安装
  • 在线安装地址下载地址

配置环境变量

  • 我这安装的msvc2015编译器,默认安装路径
  • 用户变量 Path中添加
C:\Qt\5.15.2\msvc2015_64\bin

项目配置

Cargo.toml 添加如下

[dependencies]
cxx = "1.0.107"
cxx-qt = "0.5.3"
cxx-qt-lib = "0.5.3"

[build-dependencies]
cxx-qt-build = "0.5.3"

编辑Rust代码

build.rs

use cxx_qt_build::CxxQtBuilder;

fn main() {
    CxxQtBuilder::new()
        // Link Qt's Network library
        // - Qt Core is always linked
        // - Qt Gui is linked by enabling the qt_gui Cargo feature (default).
        // - Qt Qml is linked by enabling the qt_qml Cargo feature (default).
        // - Qt Qml requires linking Qt Network on macOS
        .qt_module("Network")
        // Generate C++ from the `#[cxx_qt::bridge]` module
        .file("src/cxxqt_object.rs")
        // Generate C++ code from the .qrc file with the rcc tool
        // https://doc.qt.io/qt-6/resources.html
        .qrc("qml/qml.qrc")
        .setup_linker()
        .build();
}

cxxqt_object

  • src/cxxqt_object.rs
  • 此处qml_uri=“demo” 是qml文件中加入的 import demo 1.0
#[cxx_qt::bridge]
mod my_object {

    #[cxx_qt::qobject(qml_uri = "demo", qml_version = "1.0")]
    #[derive(Default)]
    pub struct Hello {}

    impl qobject::Hello {
        #[qinvokable]
        pub fn say_hello(&self) {
            println!("Hello world!")
        }
    }
}

main.rs

#![windows_subsystem = "windows"] //隐藏黑窗口
mod cxxqt_object;

use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl};

fn main() {
    // Create the application and engine
    let mut app = QGuiApplication::new();
    let mut engine = QQmlApplicationEngine::new();

    // Load the QML path into the engine
    if let Some(engine) = engine.as_mut() {
        engine.load(&QUrl::from("qrc:/main.qml"));
    }

    // Start the app
    if let Some(app) = app.as_mut() {
        app.exec();
    }
}

main.qml

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

// This must match the qml_uri and qml_version
// specified with the #[cxx_qt::qobject] macro in Rust.
import com.kdab.cxx_qt.demo 1.0

Window {
    height: 480
    title: qsTr("Hello World")
    visible: true
    width: 640

    MyObject {
        id: myObject
        number: 1
        string: "My String with my number: " + myObject.number
    }

    Column {
        anchors.fill: parent
        anchors.margins: 10
        spacing: 10

        Label {
            text: "Number: " + myObject.number
        }

        Label {
            text: "String: " + myObject.string
        }

        Button {
            text: "Increment Number"

            onClicked: myObject.incrementNumber()
        }

        Button {
            text: "Say Hi!"

            onClicked: myObject.sayHi(myObject.string, myObject.number)
        }
    }
}

qml.qrc

<RCC version="1.0">
    <qresource prefix="/">
        <file>main.qml</file>
    </qresource>
</RCC>

  • 如果遇到编译问题检查是否安装git
0

评论区