QCombobox finData 方法总是 returns -1 with numpy array
QCombobox finData method always returns -1 with numpy array
当使用 numpy 数组添加项目时,我在尝试获取组合框中某些数据的索引时遇到问题,而如果我使用列表,则结果是预期的。
from PySide2 import QtWidgets
import numpy as np
app = QtWidgets.QApplication()
heights_list = [0.52, 1, 2, 3, 4, 12.57, 14.97]
heights_array = np.array([0.52, 1, 2, 3, 4, 12.57, 14.97])
combo_list = QtWidgets.QComboBox()
for height in heights_list:
combo_list.addItem(f"{height:.2f} m", height)
combo_array = QtWidgets.QComboBox()
for height in heights_array:
combo_array.addItem(f"{height:.2f} m", height)
print(combo_list.findData(14.97)) # Print 6
print(combo_array.findData(14.97)) # Print -1
存储指针的 findData()
method uses the model's match()
method, and the match method uses the QVariant
s for comparison. For its part PySide2(also PyQt5) to have compatibility does PyObject
conversions (which is the representation of a python object in C/C++) to basic types such as int, float, etc. but it does not know how to convert a numpy object and then it only stores the pointer, and when comparing QVariant
它比较内存地址,但作为 2 个不同的对象,它总是 return 它们不是同一个对象。
当使用 numpy 数组添加项目时,我在尝试获取组合框中某些数据的索引时遇到问题,而如果我使用列表,则结果是预期的。
from PySide2 import QtWidgets
import numpy as np
app = QtWidgets.QApplication()
heights_list = [0.52, 1, 2, 3, 4, 12.57, 14.97]
heights_array = np.array([0.52, 1, 2, 3, 4, 12.57, 14.97])
combo_list = QtWidgets.QComboBox()
for height in heights_list:
combo_list.addItem(f"{height:.2f} m", height)
combo_array = QtWidgets.QComboBox()
for height in heights_array:
combo_array.addItem(f"{height:.2f} m", height)
print(combo_list.findData(14.97)) # Print 6
print(combo_array.findData(14.97)) # Print -1
存储指针的 findData()
method uses the model's match()
method, and the match method uses the QVariant
s for comparison. For its part PySide2(also PyQt5) to have compatibility does PyObject
conversions (which is the representation of a python object in C/C++) to basic types such as int, float, etc. but it does not know how to convert a numpy object and then it only stores the pointer, and when comparing QVariant
它比较内存地址,但作为 2 个不同的对象,它总是 return 它们不是同一个对象。