如何从 QCombobox 中获取选定的 VALUE?
在 Qt 中,我可以使用以下命令获取 QComboBox
的选定文本
combobox->currentText()
方法。 如何获取所选值
?
我寻求帮助,但找不到我期望找到的方法 currentData()
。我只能找到 combobox->currentIndex()
除了 combobox->itemData(combobox->currentIndex())
之外,还有更聪明的方法吗?
更新:从 Qt 5 开始不再需要此操作。添加了 currentData()
方法 http://doc.qt.io/qt-5/qcombobox.html#currentData-prop
In Qt, I can get the selected text
of a QComboBox
by using thecombobox->currentText()
method.
How can I get the selected value
?
I searched for help but I couldn't find a method currentData()
which I expected to find. I could only find combobox->currentIndex()
Is there a smarter way to do it other than combobox->itemData(combobox->currentIndex())
?
Update: This is no longer necessary as of Qt 5. A currentData()
method has been added http://doc.qt.io/qt-5/qcombobox.html#currentData-prop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我有同样的问题,
我已经解决了
I had same issue
I have solved by
这是我在 QT 4.7 中的 OK 代码:
This is my OK code in QT 4.7:
我很惊讶没有激活信号并且有同样的问题。我通过创建 QComboBox 的子类解决了这个问题。我认为最好避免直接访问对象并调用其函数,因为这意味着更紧密的耦合并且违背了 Qt 的理念。这是我制作的适合我的课程。
以及实施
I'm astonished that there isn't an activated signal and have the same problem. I solved it by making a subclass of QComboBox. I think it's better to avoid having to directly access the object and call its functions because that means more tight coupling and goes against Qt's philosophy. So here's the class I made that works for me.
And the implementation
这个问题很老了,但也许有人需要一个实际的答案。
在 QGIS 3.4 中,您可以使用
currentData()
方法从QComboBox
获取值。示例:
comboBox.currentData()
链接: https://doc.qt.io/qt-5/qcombobox.html#currentData-prop
The question is old, but maybe, somebody need an actual answer.
In the QGIS 3.4 you can get the value from the
QComboBox
with the methodcurrentData()
.Example:
comboBox.currentData()
Link: https://doc.qt.io/qt-5/qcombobox.html#currentData-prop
我这样做了
,您将看到名为 _dirs 的 QStringList 的结构类似于一个数组,您可以通过索引访问其成员,最多可达 _dirs.count() 返回的值
I did this
You will see with this that the QStringList named _dirs is structured like an array whose members you can access via an index up to the value returned by _dirs.count()
我遇到了这个问题并
解决了这个问题。
I had the issue and
solved this.
如果您正在开发 QGIS 插件,那么只需
if you are developing QGIS plugins then simply
我知道我已经迟到了,但对于那些仍然有这个问题的人来说,它可以很容易地解决。
我使用 Qt 5.3 并且运行良好。无需创建函数或其他所有内容。
它有效!
希望有帮助!
I know I'm very late but for those who still have that problem, it can be solved easily.
I use Qt 5.3 and it works fine. No need to create a function or all that.
and it works !
Hope it helps !
我确认最简单的方法是这样做:
I confirm the easiest way is to do this:
如果您想获取 QComboBox 的当前数据,似乎您需要执行
combobox->itemData(combobox->currentIndex())
。如果您使用自己的从 QComboBox 派生的类,则可以添加
currentData()
函数。It seems you need to do
combobox->itemData(combobox->currentIndex())
if you want to get the current data of the QComboBox.If you are using your own class derived from QComboBox, you can add a
currentData()
function.这个可以获取当前索引的文本:
This one can get the text of current index:
您可以为所有项目设置QVariant数据,然后您可以在需要时获取该值。
对于这种情况有一个示例代码:
顺便说一下,我想我误解了你的问题。我认为你获取数据的方式足够聪明吗?
you can set QVariant data for all items, then you can get the value when you need it.
there is an example code for this situation:
by the way, i think i misunderstood your question. i think the way you get data is smart enough?
自从提出这个问题以来,成员函数 QComboBox::currentData 已被添加,请参阅此 commit
The member function QComboBox::currentData has been added since this question was asked, see this commit