英文:
CurrentIndex of ComboBox from QML to C++ code
问题
I would like to get the currentIndex
of a ComboBox
in QML.
如何获取它?
From C++, I tried to have a Q_INVOKABLE
function to call from QML code when onCurrentIndexChanged
but it seems I can only pass QObject*
as parameters in these functions in QML. So, it seems I can not pass just a int
, or I get the error
我尝试从C++中创建一个Q_INVOKABLE
函数,以在onCurrentIndexChanged
时从QML代码中调用,但似乎我只能在这些函数中传递QObject*
作为参数。因此,似乎我无法仅传递一个int
,否则我会收到以下错误:
"Could not convert argument 0 at" ... "Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated." "This will throw a JavaScript TypeError in future releases of Qt!"
I tried to look in internet, but I did not find anything straighforward. I can not believe there is no trivial way to get the currentIndex
from the C++ code. Maybe I am using QML in a wrong way.
我尝试在互联网上查找,但我没有找到明显的方法。我无法相信没有从C++代码中获取currentIndex
的简单方法。也许我在错误的方式中使用QML。
A link to some example would also be appreciate.
The ComboBox model is defined in C++ and it is a QList<MyObject>
but MyObject
is NOT a QObject
.
一个示例链接也将不胜感激。
ComboBox模型在C++中定义,它是一个QList<MyObject>
,但MyObject
不是 QObject
。
英文:
I would like to get the currentIndex
of a ComboBox
in QML.
How can I get that?
From C++, I tried to have a Q_INVOKABLE
function to call from QML code when onCurrentIndexChanged
but it seems I can only pass QObject*
as parameters in these functions in QML. So, it seems I can not pass just a int
, or I get the error
"Could not convert argument 0 at"
...
"Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated."
"This will throw a JavaScript TypeError in future releases of Qt!"
I tried to look in internet, but I did not find anything straighforward. I can not believe there is no trivial way to get the currentIndex
from the C++ code. Maybe I am using QML in a wrong way.
A link to some example would also be appreciate.
The ComboBox model is defined in C++ and it is a QList<MyObject>
but MyObject
is NOT a QObject
.
答案1
得分: 1
有很多解决这个问题的方法。其中一种是注册一个处理程序类,并在更改当前索引时调用一个槽。
示例处理程序类:
#pragma once
#include <QObject>
#include <QDebug>
class ComboBoxHandler: public QObject {
Q_OBJECT
public slots:
void onItemChanged(int newValue) {
qDebug() << "Value: " << newValue;
}
};
在主.cpp中将其注册到引擎:
qmlRegisterType<ComboBoxHandler>("ComboBoxHandler", 1, 0, "ComboBoxHandler");
在QML文件中使用:
ComboBoxHandler {
id: handler
}
ComboBox {
textRole: "text"
valueRole: "value"
// 当选择项目时,更新后端。
onActivated: handler.onItemChanged(currentIndex)
model: [
{ value: Qt.NoModifier, text: qsTr("No modifier") },
{ value: Qt.ShiftModifier, text: qsTr("Shift") },
{ value: Qt.ControlModifier, text: qsTr("Control") }
]
}
这在一定程度上也是QML帮助中提供的示例代码:ComboBox文档。
有关更多见解和其他解决方案,请从这里开始:将QML与C++集成。
英文:
There are many ways of solving this. One is to register a handler class and call a slot every time you change the current index.
Example handler class:
#pragma once
#include <QObject>
#include <QDebug>
class ComboBoxHandler: public QObject {
Q_OBJECT
public slots:
void onItemChanged(int newValue) {
qDebug() << "Value: " << newValue;
}
};
Register it with the engine in your main.cpp
qmlRegisterType<ComboBoxHandler>("ComboBoxHandler", 1, 0, "ComboBoxHandler");
Use it in your QML file:
ComboBoxHandler {
id: handler
}
ComboBox {
textRole: "text"
valueRole: "value"
// When an item is selected, update the backend.
onActivated: handler.onItemChanged(currentIndex)
model: [
{ value: Qt.NoModifier, text: qsTr("No modifier") },
{ value: Qt.ShiftModifier, text: qsTr("Shift") },
{ value: Qt.ControlModifier, text: qsTr("Control") }
]
}
This is also partly the example code provided in the QML help: ComboBox docu
For more insiged and other solutions, start here: Integrate QML with C++
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论