qml 加载C++组件,

在同一个项目中加载c++组件

main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3
import org.test 1.0

ApplicationWindow {
    visible: true
    width: 400
    height: 400

    Log{
        id: log
    }

    StackLayout {
        id: stackLayout
        anchors.fill: parent

        Rectangle {
            width: 200
            height: 200
            color: "red"
        }

        Rectangle {
            width: 200
            height: 200
            color: "green"
        }

        Rectangle {
            width: 200
            height: 200
            color: "blue"
        }
    }

    Button {
        text: "Next"
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter

        onClicked: {
            log.add(2,4);
            console.log("这是一条控制台信息",stackLayout.currentIndex);
            stackLayout.currentIndex = (stackLayout.currentIndex + 1) % stackLayout.count
        }
    }


}



log.cpp
#ifndef LOG_H
#define LOG_H

#include <QQuickItem>
#include <QDebug>
#include <QObject>
#include <QQuickItem>

class Log : public QObject
{
public:
    Log();
    Q_OBJECT
    //Q_DISABLE_COPY(Log)


    Q_INVOKABLE int add(int a, int b){
        //return a + b;
        qDebug() << "zlb";
    }
};

#endif // LOG_H

log.cpp
#include "log.h"

Log::Log() {}

main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "log.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));

    qmlRegisterType<Log>("org.test", 1, 0, "Log");

    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreated,
        &app,
        [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!如果您想在C++中使用QML TextField,您可以按照以下步骤进行操作: 1. 首先,确保您已经设置好了正确的Qt环境并且已经安装了Qt Quick模块。 2. 创建一个新的QML文件,例如"textfield.qml",并在其中定义您的TextField组件: ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 TextField { id: textField placeholderText: "Enter text..." onTextChanged: { console.log("Text changed:", text) } } ``` 3. 在C++代码中加载和显示该QML文件。假设您已经有一个QQuickView对象,您可以使用以下代码加载并显示该QML文件: ```cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickView> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/textfield.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); } ``` 4. 在C++中与TextField进行交互。您可以通过访问QML中定义的TextField的属性和信号来与其交互。例如,您可以在C++中获取文本输入并进行处理: ```cpp QObject *item = engine.rootObjects().first(); QObject *textField = item->findChild<QObject*>("textField"); if (textField) { QString text = textField->property("text").toString(); qDebug() << "Text entered:" << text; } ``` 这样,您就可以在C++中与QML中的TextField进行交互了。请注意,您可能需要更多的代码来处理其他事件和逻辑。 希望这对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值