Qcombobox犯错了索引

发布于 2025-02-04 19:54:44 字数 243 浏览 1 评论 0原文

我有这个组合。

 klients = qtw.QComboBox(self)
 klients.insertItem(5, "x")
 klients.insertItem(19, "y")

当我以后在按钮单击事件中编写此代码时:

print(klients.currentIndex())

它打印0或1而不是5或19。如何实现它打印5或19?

I have this combobox.

 klients = qtw.QComboBox(self)
 klients.insertItem(5, "x")
 klients.insertItem(19, "y")

when I later write this code in button clicked event:

print(klients.currentIndex())

It prints 0 or 1 instead of 5 or 19. How can I achieve it prints 5 or 19?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2025-02-11 19:54:44

qcombobox项目管理就像一个列表一样工作:创建新列表后,执行list.intert(5,“ x”)将在 first index上添加“ x”,这很明显,因为列表为空。

如果要具有自定义数据的内部引用,请使用userData additem()

klients.addItem("x", 5)
klients.addItem("y", 19)

然后用 currentdata()

data = klients.currentData()
# equivalent to
data = klients.itemData(klients.currentIndex())

QComboBox item management works just like a list: after creating a new list, doing list.insert(5, "x") will add "x" at the first index, which is quite obvious, since the list is empty.

If you want to have internal reference of custom data, then use the userData argument of addItem():

klients.addItem("x", 5)
klients.addItem("y", 19)

and then retrieve it with currentData():

data = klients.currentData()
# equivalent to
data = klients.itemData(klients.currentIndex())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文