Pregunta

I have created a QtQuick application with some textboxes in QML. I want to use the value of those textboxes in my c++ code. So how can I fetch those values from C++ code?

¿Fue útil?

Solución

It can be like:

QML file:

Item{
id: root

signal textChanged(string msg)

TextInput
{
    id: inputText
    anchors.horizontalCenter: root.horizontalCenter
    anchors.verticalCenter: root.verticalCenter

    text : ""
    inputMethodHints: Qt.ImhNoPredictiveText
    selectByMouse: true

    onAccepted: { 
        inputText.focus = false; 
        Qt.inputMethod.hide(); 
        root.textChanged(inputText.text); 
    }

 }
}

ِYou can connect the signal of your qml to some slot in cpp like:

QObject::connect((QObject *)viewer.rootObject(), SIGNAL(textChanged(QString)), this, SLOT(someSlot(QString)));

Otros consejos

I believe you have some C++ class which is used as a data container. You are using the QML for UI purposes. Whatever data a user enters via QML UI must be stored in the C++ container.

There are 2 ways to achieve it.

1)Create and manage objects in C++ and expose objects to QML by using setContextProperty() method

2)Create a class in C++ and register that class as a QML type to be used in QML side using qmlRegisterType function and then manage objects from QML side itselft

This example contains a Customer class which has a simple class Employee and a single data member name. This example demonstrates both ways as described above.

employee.h header file

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <QObject>

class Employee : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
    explicit Employee(QObject *parent = 0);
    QString name();
    void setName(const QString &name);
signals:
    void nameChanged();
public slots:

private:
    QString m_name;

};

employee.cpp file

#include "employee.h"
#include <QDebug>
Employee::Employee(QObject *parent) :
    QObject(parent)
{
}

QString Employee::name()
{
    return m_name;
}

void Employee::setName(const QString &name)
{
    if(m_name!=name)
    {
        m_name=name;
        emit nameChanged();
        qDebug()<<"C++:Name changed->"<<m_name;
    }
}

main.cpp file

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <employee.h>
#include <QQmlEngine>
#include <QQmlContext>
#include <QtQml>
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    Employee emp;
    viewer.rootContext()->setContextProperty("employee",&emp);
    qmlRegisterType<Employee>("CPP.Mycomponents",1,0,"Employee");
    viewer.setMainQmlFile(QStringLiteral("qml/SO_GetValueOfQMLEditboxFromCpp/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

//main.qml

import QtQuick 2.0
import CPP.Mycomponents 1.0
Rectangle {
    width: 360
    height: 360
    Rectangle{
        id:rect1
        width:360
        height:50
        color:"transparent"
        border.color: "black"
     TextInput{
         id:contextPropertyInput
         anchors.left: parent.left
         anchors.leftMargin: 5
         width:350
         height:50
         color:"black"
         font.pixelSize: 16
         onTextChanged: employee.name = text

     }
    }
    Rectangle{
        width:360
        height:50
        color:"transparent"
        border.color: "black"
        anchors.top: rect1.bottom
        anchors.topMargin: 10
         TextInput{
             id:customQMLTypeInput
             anchors.left: parent.left
             anchors.leftMargin: 5
             width:360
             height:50
             color:"black"
             font.pixelSize: 16
         }
    }
     Employee{
         id:qmlEmp;
         name:customQMLTypeInput.text
     }
}

See how the Employee is used as a QML type. You can also create a C++ object of Employee and set it as a context property using setContextProperty and edit that paritcular object. Choice is yours.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top