c++ - 从 C++ 获取 QML Editbox 的值

标签 c++ qt qml qtquick2 qt-quick

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

最佳答案

它可以是这样的:

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

关于c++ - 从 C++ 获取 QML Editbox 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22658561/

相关文章:

c++ - 如何以交互方式调整 QGraphicsObject 的大小?

c++ - LNK2019 未解析的外部符号 SHGetFolderPathW

qt - 如何获得 QQuickItem 的变换矩阵?

c++ - 从 "const T"返回 "T::operator+(const T& rhs) const"是好的做法吗?

c++ - 如何对除 T 级以外的所有人隐藏数据

c++ - 来自带有时区和夏令时的字符串的 Qt QDateTime

c++ - QML/real 和 C++/float 之间的 Qt 类型错误

c++ - 调用QML处理程序,但使用 “undefined” c++信号参数

c++ - 绑定(bind).gyp : How to use "copies" section to copy files in multiple location

c++ - 一种在编译时从文件中读取数据并将其放入应用程序镜像文件中以初始化数组的方法