CurrentIndex of ComboBox from QML to C++ code.

huangapple go评论76阅读模式
英文:

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

&quot;Could not convert argument 0 at&quot;
...
&quot;Passing incompatible arguments to C++ functions from JavaScript is dangerous and deprecated.&quot;
&quot;This will throw a JavaScript TypeError in future releases of Qt!&quot;

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&lt;MyObject&gt; 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 &lt;QObject&gt;
#include &lt;QDebug&gt;

class ComboBoxHandler: public QObject {
    Q_OBJECT
public slots:
    void onItemChanged(int newValue) {
        qDebug() &lt;&lt; &quot;Value: &quot; &lt;&lt; newValue;
    }
};

Register it with the engine in your main.cpp

qmlRegisterType&lt;ComboBoxHandler&gt;(&quot;ComboBoxHandler&quot;, 1, 0, &quot;ComboBoxHandler&quot;);

Use it in your QML file:

ComboBoxHandler {
    id: handler
}

ComboBox {
     textRole: &quot;text&quot;
     valueRole: &quot;value&quot;

     // When an item is selected, update the backend.
     onActivated: handler.onItemChanged(currentIndex)

     model: [
         { value: Qt.NoModifier, text: qsTr(&quot;No modifier&quot;) },
         { value: Qt.ShiftModifier, text: qsTr(&quot;Shift&quot;) },
         { value: Qt.ControlModifier, text: qsTr(&quot;Control&quot;) }
     ]
}

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++

huangapple
  • 本文由 发表于 2023年5月10日 18:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定