test_patch_qheaderview.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from __future__ import absolute_import
  2. import sys
  3. import pytest
  4. from qtpy import PYSIDE, PYSIDE2, PYQT4
  5. from qtpy.QtWidgets import QApplication
  6. from qtpy.QtWidgets import QHeaderView
  7. from qtpy.QtCore import Qt
  8. from qtpy.QtCore import QAbstractListModel
  9. PY3 = sys.version[0] == "3"
  10. def get_qapp(icon_path=None):
  11. qapp = QApplication.instance()
  12. if qapp is None:
  13. qapp = QApplication([''])
  14. return qapp
  15. @pytest.mark.skipif(PY3 or PYSIDE2, reason="It fails on Python 3 and PySide2")
  16. def test_patched_qheaderview():
  17. """
  18. This will test whether QHeaderView has the new methods introduced in Qt5.
  19. It will then create an instance of QHeaderView and test that no exceptions
  20. are raised and that some basic behaviour works.
  21. """
  22. assert QHeaderView.sectionsClickable is not None
  23. assert QHeaderView.sectionsMovable is not None
  24. assert QHeaderView.sectionResizeMode is not None
  25. assert QHeaderView.setSectionsClickable is not None
  26. assert QHeaderView.setSectionsMovable is not None
  27. assert QHeaderView.setSectionResizeMode is not None
  28. # setup a model and add it to a headerview
  29. qapp = get_qapp()
  30. headerview = QHeaderView(Qt.Horizontal)
  31. class Model(QAbstractListModel):
  32. pass
  33. model = Model()
  34. headerview.setModel(model)
  35. assert headerview.count() == 1
  36. # test it
  37. assert isinstance(headerview.sectionsClickable(), bool)
  38. assert isinstance(headerview.sectionsMovable(), bool)
  39. if PYSIDE:
  40. assert isinstance(headerview.sectionResizeMode(0),
  41. QHeaderView.ResizeMode)
  42. else:
  43. assert isinstance(headerview.sectionResizeMode(0), int)
  44. headerview.setSectionsClickable(True)
  45. assert headerview.sectionsClickable() == True
  46. headerview.setSectionsClickable(False)
  47. assert headerview.sectionsClickable() == False
  48. headerview.setSectionsMovable(True)
  49. assert headerview.sectionsMovable() == True
  50. headerview.setSectionsMovable(False)
  51. assert headerview.sectionsMovable() == False
  52. headerview.setSectionResizeMode(QHeaderView.Interactive)
  53. assert headerview.sectionResizeMode(0) == QHeaderView.Interactive
  54. headerview.setSectionResizeMode(QHeaderView.Fixed)
  55. assert headerview.sectionResizeMode(0) == QHeaderView.Fixed
  56. headerview.setSectionResizeMode(QHeaderView.Stretch)
  57. assert headerview.sectionResizeMode(0) == QHeaderView.Stretch
  58. headerview.setSectionResizeMode(QHeaderView.ResizeToContents)
  59. assert headerview.sectionResizeMode(0) == QHeaderView.ResizeToContents
  60. headerview.setSectionResizeMode(0, QHeaderView.Interactive)
  61. assert headerview.sectionResizeMode(0) == QHeaderView.Interactive
  62. headerview.setSectionResizeMode(0, QHeaderView.Fixed)
  63. assert headerview.sectionResizeMode(0) == QHeaderView.Fixed
  64. headerview.setSectionResizeMode(0, QHeaderView.Stretch)
  65. assert headerview.sectionResizeMode(0) == QHeaderView.Stretch
  66. headerview.setSectionResizeMode(0, QHeaderView.ResizeToContents)
  67. assert headerview.sectionResizeMode(0) == QHeaderView.ResizeToContents
  68. # test that the old methods in Qt4 raise exceptions
  69. if PYQT4 or PYSIDE:
  70. with pytest.warns(UserWarning):
  71. headerview.isClickable()
  72. with pytest.warns(UserWarning):
  73. headerview.isMovable()
  74. with pytest.warns(UserWarning):
  75. headerview.resizeMode(0)
  76. with pytest.warns(UserWarning):
  77. headerview.setClickable(True)
  78. with pytest.warns(UserWarning):
  79. headerview.setMovable(True)
  80. with pytest.warns(UserWarning):
  81. headerview.setResizeMode(0, QHeaderView.Interactive)