Skip to content

Commit

Permalink
Remap setSectionResizeMode to Qt 4
Browse files Browse the repository at this point in the history
  • Loading branch information
mottosso committed Aug 27, 2016
1 parent 9ba1022 commit 34181c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
46 changes: 0 additions & 46 deletions CAVEATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,49 +216,3 @@ Traceback (most recent call last):
...
TypeError: ...
```


#### QtWidgets.QHeaderView.setResizeMode

`setResizeMode` was [renamed](http://doc.qt.io/qt-5/qheaderview.html#setSectionResizeMode) `setSectionResizeMode` in Qt 5.

```python
# PySide2
>>> from Qt import QtWidgets
>>> app = QtWidgets.QApplication(sys.argv)
>>> view = QtWidgets.QTreeWidget()
>>> header = view.header()
>>> header.setResizeMode(QtWidgets.QHeaderView.Fixed)
Traceback (most recent call last):
...
AttributeError: 'PySide2.QtWidgets.QHeaderView' object has no attribute 'setResizeMode'
```

```python
# PySide
>>> from Qt import QtWidgets
>>> app = QtWidgets.QApplication(sys.argv)
>>> view = QtWidgets.QTreeWidget()
>>> header = view.header()
>>> header.setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
Traceback (most recent call last):
...
AttributeError: 'PySide.QtGui.QHeaderView' object has no attribute 'setSectionResizeMode'
```

##### Workaround

Use a conditional.

```python
# PySide2
>>> from Qt import QtWidgets, __binding__
>>> app = QtWidgets.QApplication(sys.argv)
>>> view = QtWidgets.QTreeWidget()
>>> header = view.header()
>>> if __binding__ in ("PyQt4", "PySide"):
... header.setResizeMode(QtWidgets.QHeaderView.Fixed)
... else:
... header.setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
```

4 changes: 4 additions & 0 deletions Qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def _pyqt4():
PyQt4.QtCore.Property = PyQt4.QtCore.pyqtProperty
PyQt4.QtCore.QItemSelection = PyQt4.QtGui.QItemSelection
PyQt4.QtCore.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
PyQt4.QtWidgets.QHeaderView.setSectionResizeMode = \
PyQt4.QtWidgets.QHeaderView.setResizeMode

try:
from PyQt4 import QtWebKit
Expand Down Expand Up @@ -115,6 +117,8 @@ def _pyside():
PySide.QtCore.QStringListModel = PySide.QtGui.QStringListModel
PySide.QtCore.QItemSelection = PySide.QtGui.QItemSelection
PySide.QtCore.QItemSelectionModel = PySide.QtGui.QItemSelectionModel
PySide.QtWidgets.QHeaderView.setSectionResizeMode = \
PySide.QtWidgets.QHeaderView.setResizeMode

try:
from PySide import QtWebKit
Expand Down
21 changes: 21 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ def test_vendoring():
) == 0


def test_qheaderview_setsectionresizemode():
"""setResizeMode was renamed setSectionResizeMode in Qt 5"""

with pyside():
from PySide import QtGui

# Qt.py doesn't replace an existing member.
assert not hasattr(QtGui.QHeaderView, "setSectionResizeMode")

import sys
from Qt import QtWidgets
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QTreeView()
qheaderview = widget.header()

# This does not raise an AttributeError
qheaderview.setSectionResizeMode(qheaderview.Fixed)

app.quit()


if PYTHON == 2:
def test_sip_api_already_set():
"""Raise ImportError if sip API v1 was already set (Python 2.x only)"""
Expand Down

0 comments on commit 34181c1

Please sign in to comment.