C++与QML交互(信号与槽通知QML,C++注册QML,QML结点映射C++类)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/83347647

运行截图如下:

源码如下:

data.h

#ifndef DATA_H
#define DATA_H

#include <QObject>

class Data : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText)
    Q_PROPERTY(int size READ size WRITE setSize)
public:
   Data(QObject *parent = 0);

   QString text() const;
   void setText(const QString name);
   int size() const;
   void setSize(const int age);

signals:
   void textChanged(QString str);

private:

   QString m_text;
   int m_size;
};

#endif // DATA_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>


class Data;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

protected:
    void getDataInfo();
    void textChange(const QString &str);

private:
    Ui::Widget *ui;

    Data *m_data;
};

#endif // WIDGET_H

data.cpp

#include "data.h"

Data::Data(QObject *parent) : QObject(parent)
{
    m_text = "";
    m_size = 0;
}

QString Data::text() const
{
    return m_text;
}

void Data::setText(const QString text)
{
    m_text = text;
    emit textChanged(m_text);
}

int Data::size() const
{
    return m_size;
}

void Data::setSize(const int size)
{
    m_size = size;
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "data.h"

#include <QQmlEngine>
#include <QQuickItem>
#include <QQmlContext>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //use after registartion
    qmlRegisterType<Data>("Data", 1, 0, "Data");
    ui->quickWidget->setSource(QUrl("qrc:/main.qml"));

    getDataInfo();

    connect(ui->lineEdit,static_cast<void(QLineEdit::*)(const QString&)>(&QLineEdit::textChanged),this,&Widget::textChange);
    connect(m_data,SIGNAL(textChanged(QString)),m_data,SIGNAL(qmltextChanged(QString)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::getDataInfo()
{
    QQuickItem *pRoot = ui->quickWidget->rootObject();
    Q_ASSERT(pRoot);
    qDebug() << pRoot->children();

    for(int i=0; i<pRoot->children().size();i++){
        QString className(pRoot->children()[i]->metaObject()->className());
        if(className.contains("Data")){
            m_data =  qobject_cast<Data *>(pRoot->children()[i]);
            break;
        }
    }

    ui->lineEdit->setText(m_data->text());
}

void Widget::textChange(const QString &str)
{
    m_data->setText(str);

}

main.qml

import QtQuick 2.0
import Data 1.0

Item {

    Text {
        id: helloText
        text: qsTr("Hello world")
        color: "blue"
        font.pixelSize: 50
        font.family: "Times New Roman"
        anchors.centerIn: parent

        MouseArea {
            id: mouseArea
            anchors.fill: parent
        }

        states: State {
            name: "down"
            when: mouseArea.pressed === true

            PropertyChanges {
                target: helloText
                rotation : 360
                color: "red"
            }
        }

        transitions: Transition {
            from: ""
            to: "down"
            reversible: true

            ParallelAnimation {
                NumberAnimation { properties: "rotation"; duration: 500; easing.type: Easing.InOutQuad }
                ColorAnimation { duration: 500 }
            }
        }
    }

    Data {
        id: data
        text: helloText.text
        size: helloText.font.pixelSize


        signal qmltextChanged(string text)

        onQmltextChanged: {
            helloText.text = text
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/83347647