clion+msvc+qml demo

CMake设置-DCMAKE_PREFIX_PATH=C:\Qt\6.6.2\msvc2019_64

demo工程结构:

├───CMakeLists.txt
└───main.cpp
└───Main.qml
└───MyObject.cpp
└───MyObject.h
└───MyRectangle.qml

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(qmltest02 VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_executable(appqmltest02
    main.cpp
    MyObject.cpp
    MyObject.h
)

qt_add_qml_module(appqmltest02
    URI qmltest02
    VERSION 1.0
    QML_FILES Main.qml
    QML_FILES MyRectangle.qml
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appqmltest02 PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appqmltest02
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(appqmltest02
    PRIVATE Qt6::Quick
)

include(GNUInstallDirs)
install(TARGETS appqmltest02
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "MyObject.h"

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

    QQmlApplicationEngine engine;

    /* 将自定义 C++ 类型注册到 QML 中的函数*/
    // 参数: 导入的模块名(约等于头文件), 主版本号,次版本号,组件名(约等于类名)
    qmlRegisterType<MyObject>("MyObj", 1, 0, "MyObject");

    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.loadFromModule("qmltest02", "Main");

    return app.exec();
}

Main.qml

import QtQuick
import MyObj 1.0
import QtQuick.Controls

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

    MyObject {
        // 约等于组件对象名(class变量对象名)
        id: myobj
        str: "hahahah"
        value: 10

        // 约等于构造函数
        Component.onCompleted: {
            console.log(value, str)
        }
    }

    // 列布局
    Column {
        // 行布局
        Row {
            spacing: 2
            Rectangle {
                id: r1
                color: "red"
                height: 100
                width: 200
            }

            MyRectangle {
                x: r1.width + 20
            }

            Text {
                id: text01
                text: qsTr("text")
                font.pixelSize: 30
            }
        }

        Button {
            id: btn1
            width: 50
            height: 50
            text: "btn1"

            // 自定义属性并赋值
            property string tx: myobj.str
            onClicked: {
                console.log("btn1 被点击")
                text01.text = tx + myobj.value

                myobj.func();
            }
        }
    }
}

MyRectangle.qml

import QtQuick

Rectangle {
    id: r2
    property int aa: 1

    color: "blue"
    height: 100
    width: 200
}

MyObject.h

//
// Created by Dark on 24-4-28.
//

#pragma once

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT

public:
    explicit MyObject(QObject *parent = nullptr);

    int value() const;
    void setValue(int newValue);

    QString str() const;
    void setStr(const QString &newStr);

    // 声明Q_INVOKABLE, 可以让func给qml调用
    Q_INVOKABLE void func();

signals:
    void valueChanged();

    void strChanged();

private:
    int m_value;
    QString m_str;
    Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged FINAL)
    Q_PROPERTY(QString str READ str WRITE setStr NOTIFY strChanged FINAL)
};

MyObject.cpp

//
// Created by Dark on 24-4-28.
//

#include "MyObject.h"

#include <QDebug>

MyObject::MyObject(QObject *parent) : QObject(parent) {

}

int MyObject::value() const
{
    return m_value;
}

void MyObject::setValue(int newValue)
{
    if (m_value == newValue)
        return;
    m_value = newValue;
    emit valueChanged();
}

QString MyObject::str() const
{
    return m_str;
}

void MyObject::setStr(const QString &newStr)
{
    if (m_str == newStr)
        return;
    m_str = newStr;
    emit strChanged();
}

void MyObject::func()
{
    qDebug() << "MyObject::func";
    m_value += 10;
}

编译后,执行windeployqt把需要的dll拷贝到exe的目录,运行

运行结果:

posted @ 2024-06-15 23:03  DarkH  阅读(141)  评论(0编辑  收藏  举报