【问题标题】:Get the value of QML Editbox from C++从 C++ 中获取 QML Editbox 的值
【发布时间】:2014-03-26 10:45:53
【问题描述】:

我在 QML 中创建了一个带有一些文本框的 QtQuick 应用程序。我想在我的 c++ 代码中使用这些文本框的值。那么如何从 C++ 代码中获取这些值呢?

【问题讨论】:

    标签: c++ qt qml qtquick2 qt-quick


    【解决方案1】:

    可能是这样的:

    QML 文件:

    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); 
        }
    
     }
    }
    

    ِ您可以将 qml 的信号连接到 cpp 中的某个插槽,例如:

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

    【讨论】:

      【解决方案2】:

      我相信您有一些用作数据容器的 C++ 类。您将 QML 用于 UI 目的。用户通过 QML UI 输入的任何数据都必须存储在 C++ 容器中。

      有两种方法可以实现它。

      1)在C++中创建和管理对象,并使用setContextProperty()方法将对象暴露给QML

      2) 在 C++ 中创建一个类,并使用qmlRegisterType 函数将该类注册为要在 QML 端使用的 QML 类型,然后从 QML 端本身管理对象t

      此示例包含一个 Customer 类,该类具有一个简单的 Employee 类和一个数据成员名称。此示例演示了上述两种方式。

      employee.h 头文件

      #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 文件

      #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 文件

      #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
           }
      }
      

      查看Employee 如何用作 QML 类型。您还可以创建 Employee 的 C++ 对象并使用 setContextProperty 将其设置为上下文属性并编辑该特定对象。选择权在你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-12
        • 1970-01-01
        • 1970-01-01
        • 2019-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多