我在 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/