[PyQt] Problem with QVariant float arguments

Baz Walter bazwal at gmail.com
Mon Oct 15 13:47:31 BST 2018


Hello

There seems to be a problem when passing python floats above a certain 
size to Qt methods which take a QVariant argument. Could there be some 
kind of truncation taking place? My test set up: python 3.7.0, qt 
5.11.2, sip 4.19.13, pyqt 5.11.3, gcc 8.2.1 20180831.

Here is a test script:

from PyQt5 import QtCore, QtGui, QtWidgets

a = QtWidgets.QApplication(['test'])

x0 = 1.001
x1 = 1.002
x2 = 1523015317.001
x3 = 1523015317.002

m = QtGui.QStandardItemModel(); r = QtCore.Qt.UserRole
i = QtGui.QStandardItem('0'); i.setData(x0, r); m.appendRow(i)
i = QtGui.QStandardItem('1'); i.setData(x1, r); m.appendRow(i)
i = QtGui.QStandardItem('2'); i.setData(x2, r); m.appendRow(i)
i = QtGui.QStandardItem('3'); i.setData(x3, r); m.appendRow(i)

f = QtCore.Qt.MatchExactly # QVariant matching

assert m.match(m.index(0, 0), r, x0, 1, f)[0].row() == 0
assert m.match(m.index(0, 0), r, x1, 1, f)[0].row() == 1
assert m.match(m.index(0, 0), r, x2, 1, f)[0].row() == 2
assert m.match(m.index(0, 0), r, x3, 1, f)[0].row() == 3

Traceback (most recent call last):
   File "test_qvariant_float.py", line 23, in <module>
     assert m.match(m.index(0, 0), r, x3, 1, f)[0].row() == 3
AssertionError

And the same behaviour is seen with other equivalent Qt APIs:

c = QtWidgets.QComboBox(); c.setModel(m)

assert c.findData(x0) == 0
assert c.findData(x1) == 1
assert c.findData(x2) == 2
assert c.findData(x3) == 3

Traceback (most recent call last):
   File "test_qvariant_float.py", line 31, in <module>
     assert c.findData(x3) == 3
AssertionError


More information about the PyQt mailing list