1 # The code below, as well as the associated test were adapted from
2 # qt-helpers, which was released under a 3-Clause BSD license:
4 # Copyright (c) 2015, Chris Beaumont and Thomas Robitaille
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the
18 # * Neither the name of the Glue project nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 def patch_qcombobox(QComboBox
):
37 In PySide, using Python objects as userData in QComboBox causes
38 Segmentation faults under certain conditions. Even in cases where it
39 doesn't, findData does not work correctly. Likewise, findData also does not
40 work correctly with Python objects when using PyQt4. On the other hand,
41 PyQt5 deals with this case correctly. We therefore patch QComboBox when
42 using PyQt4 and PySide to avoid issues.
45 from ..QtGui
import QIcon
46 from ..QtCore
import Qt
, QObject
48 class userDataWrapper():
50 This class is used to wrap any userData object. If we don't do this,
51 then certain types of objects can cause segmentation faults or issues
52 depending on whether/how __getitem__ is defined.
54 def __init__(self
, data
):
57 _addItem
= QComboBox
.addItem
59 def addItem(self
, *args
, **kwargs
):
60 if len(args
) == 3 or (not isinstance(args
[0], QIcon
)
62 args
, kwargs
['userData'] = args
[:-1], args
[-1]
63 if 'userData' in kwargs
:
64 kwargs
['userData'] = userDataWrapper(kwargs
['userData'])
65 _addItem(self
, *args
, **kwargs
)
67 _insertItem
= QComboBox
.insertItem
69 def insertItem(self
, *args
, **kwargs
):
70 if len(args
) == 4 or (not isinstance(args
[1], QIcon
)
72 args
, kwargs
['userData'] = args
[:-1], args
[-1]
73 if 'userData' in kwargs
:
74 kwargs
['userData'] = userDataWrapper(kwargs
['userData'])
75 _insertItem(self
, *args
, **kwargs
)
77 _setItemData
= QComboBox
.setItemData
79 def setItemData(self
, index
, value
, role
=Qt
.UserRole
):
80 value
= userDataWrapper(value
)
81 _setItemData(self
, index
, value
, role
=role
)
83 _itemData
= QComboBox
.itemData
85 def itemData(self
, index
, role
=Qt
.UserRole
):
86 userData
= _itemData(self
, index
, role
=role
)
87 if isinstance(userData
, userDataWrapper
):
88 userData
= userData
.data
91 def findData(self
, value
):
92 for i
in range(self
.count()):
93 if self
.itemData(i
) == value
:
97 QComboBox
.addItem
= addItem
98 QComboBox
.insertItem
= insertItem
99 QComboBox
.setItemData
= setItemData
100 QComboBox
.itemData
= itemData
101 QComboBox
.findData
= findData