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

Conversation

mottosso
Copy link
Owner

Goal

Enable Qt 4 bindings the use of setSectionResizeMode.

# PySide
import sys
from Qt import QtWidgets
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QTreeView()
qheaderview = widget.header()
qheaderview.setSectionResizeMode(qheaderview.Fixed)
widget.show()
app.exec_()

Rationale

Originally offered here but through misunderstandings it never saw the light of day.

This enables a PyQt5/PySide2 implementation to run on PyQt4/PySide, without altering the behavior of those bindings.

Implementation

A simple remapping. See the test and implementation for details!

@fredrikaverpil let me know what you think.

…somehow causes this to not work as one would expect.
@mottosso
Copy link
Owner Author

mottosso commented Aug 27, 2016

This works now, but there is a problem.

Importing PySide before Qt.py under Python 3 has side-effects which prevents the remapping from taking place. This may be due to some of the magic happening upon importing PySide and I'm expecting this to be outside of our control.

The exposing lines are here, remapping is here.

This makes monkey-patching classes a non-starter.

@mottosso
Copy link
Owner Author

Here's an alternative, to subclass rather than monkey-patch.

The problem in this case is that the function QTreeView.header() isn't returning our subclass, but rather the original class; as the call is to C++ and C++ knows nothing of what we've done externally.

A fact easily proven via:

id(qheaderview) != id(QtWidgets.QHeaderView)

This makes subclassing for filling in missing functionality in older bindings a non-starter as well.

Due to these findings, and unless anyone has an alternative implementation to share, this cannot be merged. :(

@mottosso
Copy link
Owner Author

Here's a third alternative from qtpy; also monkey-patching the class.

@mottosso
Copy link
Owner Author

Important lessons learned here are:

  1. We cannot replace members of QtWidgets et. al. with our own subclasses due to classes returned via internal functionality, such as QTreeView.header(), does not know nor care about this.
  2. We cannot monkey-patch a class because (1) it modifies the original binding and/or (2) it sometimes doesn't work; as seen here with Python 3.5-only (more).

Closing this, and will refer back here whenever any of these two solutions crop up in pull-requests.

@mottosso mottosso closed this Aug 29, 2016
@mottosso
Copy link
Owner Author

Due to the PR coming from a master branch, rather than a feature branch, I've force-pushed it back to it's original state and stored these changes for future reference in an alternative branch.

@mottosso mottosso mentioned this pull request Sep 5, 2016
This was referenced Sep 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant