Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remap setSectionResizeMode to Qt 4 #112

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
```

89 changes: 89 additions & 0 deletions Qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def _pyqt4():
PyQt4.QtCore.QItemSelection = PyQt4.QtGui.QItemSelection
PyQt4.QtCore.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel

PyQt4.QtWidgets.QHeaderView = introduce_renamed_methods_qheaderview(
PyQt4.QtWidgets.QHeaderView)

try:
from PyQt4 import QtWebKit
PyQt4.QtWebKitWidgets = QtWebKit
Expand Down Expand Up @@ -116,6 +119,9 @@ def _pyside():
PySide.QtCore.QItemSelection = PySide.QtGui.QItemSelection
PySide.QtCore.QItemSelectionModel = PySide.QtGui.QItemSelectionModel

PySide.QtWidgets.QHeaderView = introduce_renamed_methods_qheaderview(
PySide.QtWidgets.QHeaderView)

try:
from PySide import QtWebKit
PySide.QtWebKitWidgets = QtWebKit
Expand Down Expand Up @@ -194,6 +200,89 @@ def _log(text, verbose):
sys.stdout.write(text)


def introduce_renamed_methods_qheaderview(QHeaderView):

_isClickable = QHeaderView.isClickable
def sectionsClickable(self):
"""
QHeaderView.sectionsClickable() -> bool
"""
return _isClickable(self)
QHeaderView.sectionsClickable = sectionsClickable
def isClickable(self):
raise Exception('isClickable is only available in Qt4. Use '
'sectionsClickable instead.')
QHeaderView.isClickable = isClickable


_isMovable = QHeaderView.isMovable
def sectionsMovable(self):
"""
QHeaderView.sectionsMovable() -> bool
"""
return _isMovable(self)
QHeaderView.sectionsMovable = sectionsMovable
def isMovable(self):
raise Exception('isMovable is only available in Qt4. Use '
'sectionsMovable instead.')
QHeaderView.isMovable = isMovable


_resizeMode = QHeaderView.resizeMode
def sectionResizeMode(self, logicalIndex):
"""
QHeaderView.sectionResizeMode(int) -> QHeaderView.ResizeMode
"""
return _resizeMode(self, logicalIndex)
QHeaderView.sectionResizeMode = sectionResizeMode
def resizeMode(self, logicalIndex):
raise Exception('resizeMode is only available in Qt4. Use '
'sectionResizeMode instead.')
QHeaderView.resizeMode = resizeMode

_setClickable = QHeaderView.setClickable
def setSectionsClickable(self, clickable):
"""
QHeaderView.setSectionsClickable(bool)
"""
return _setClickable(self, clickable)
QHeaderView.setSectionsClickable = setSectionsClickable
def setClickable(self, clickable):
raise Exception('setClickable is only available in Qt4. Use '
'setSectionsClickable instead.')
QHeaderView.setClickable = setClickable


_setMovable = QHeaderView.setMovable
def setSectionsMovable(self, movable):
"""
QHeaderView.setSectionsMovable(bool)
"""
return _setMovable(self, movable)
QHeaderView.setSectionsMovable = setSectionsMovable
def setMovable(self, movable):
raise Exception('setMovable is only available in Qt4. Use '
'setSectionsMovable instead.')
QHeaderView.setMovable = setMovable


_setResizeMode = QHeaderView.setResizeMode
def setSectionResizeMode(self, *args):
"""
QHeaderView.setSectionResizeMode(QHeaderView.ResizeMode)
QHeaderView.setSectionResizeMode(int, QHeaderView.ResizeMode)
"""
_setResizeMode(self, *args)
QHeaderView.setSectionResizeMode = setSectionResizeMode
def setResizeMode(self, *args):
raise Exception('setResizeMode is only available in Qt4. Use '
'setSectionResizeMode instead.')
QHeaderView.setResizeMode = setResizeMode





def _init():
"""Try loading each binding in turn

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