QTableView sorting fails after setHorizontalHeader()

早过忘川 提交于 2019-12-13 02:55:50

问题


I am encountering a strange problem with PySide QTableViews after I have set a new QHeaderView instance via setHorizontalHeader(). Specifically it seems that the sorting ability of the view is broken/cannot be invoked anymore.

Questions


  • Why is the sorting broken after a new header view is set?
  • Why does monkeypatching methods on the header view instance only work after a setting a new header via setHorizontalHeader()? (See wrap_size_hint() in the demo code.)
  • When logging the children of the table view, why are the always 2 QHeaderView instances?
  • What are the initialize() methods on the QHeaderView actually doing? Could they be useful here in any way? The docs aren't quite that helpfull.

Demo code (gist)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division

import logging
import sys

from PySide import QtCore
from PySide import QtGui


# configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


def wrap_size_hint(func):
    """Monkeypatch virtual method to increase sizeHint.

    Notes:
        Strangely this does not work when when no custom header is set on the
        table view.
    """

    def wrapped(*args, **kwargs):
        """Just extend sizeHint to increase height."""
        size = func(*args, **kwargs)  # QSize
        height_increase = 60
        height = size.height() + height_increase
        size.setHeight(height)
        return size

    return wrapped


class CustomHeaderView(QtGui.QHeaderView):
    """CustomHeaderView"""


class TestWidget(QtGui.QWidget):

    def __init__(self, use_custom_headerview=True, *args, **kwargs):
        super(TestWidget, self).__init__(*args, **kwargs)

        self.logger = logging.getLogger(type(self).__name__)

        QtGui.QVBoxLayout(self)

        self.setup_mvc(use_custom_headerview)
        self.update_model()
        self.log_children(self.view)

    def setup_mvc(self, use_custom_headerview):
        layout = self.layout()

        # model
        header_labels = "One Two Three Four Five".split()
        self.model = QtGui.QStandardItemModel()
        self.model.setHorizontalHeaderLabels(header_labels)

        # proxymodel
        self.proxymodel = QtGui.QSortFilterProxyModel()
        self.proxymodel.setDynamicSortFilter(True)
        self.proxymodel.setSourceModel(self.model)

        # view
        self.view = QtGui.QTableView()
        if (use_custom_headerview is True):
            self.view.setHorizontalHeader(CustomHeaderView(QtCore.Qt.Horizontal))

        self.view.setSortingEnabled(True)
        self.view.setModel(self.proxymodel)

        # monkeypatch sizeHint to increase size
        headerview = self.view.horizontalHeader()
        headerview.sizeHint = wrap_size_hint(headerview.sizeHint)

        layout.addWidget(self.view)

    def update_model(self):

        for row in range(50):
            items = []

            for column in range(self.model.columnCount()):
                text = "{0} {1}".format(row, column)
                item = QtGui.QStandardItem()
                item.setEditable(True)
                item.setData(text, QtCore.Qt.DisplayRole)
                items.append(item)

            self.model.appendRow(items)

    def log_children(self, widget, tab=-1):
        tab += 1
        msg = "{0}{0}: {1}".format("--"*tab, type(widget).__name__, widget)
        self.logger.info(msg)
        for child in widget.children():
            self.log_children(child, tab)


if (__name__ == "__main__"):

    app = QtGui.QApplication(sys.argv)

    # widget_sorting_fails
    widget_sorting_fails = TestWidget(use_custom_headerview=True)
    widget_sorting_fails.setWindowTitle("Custom headerview")
    widget_sorting_fails.show()

    logger.info("-"*100)

    # widget_sorting_succeeds
    widget_sorting_succeeds = TestWidget(use_custom_headerview=False)
    widget_sorting_succeeds.setWindowTitle("No custom headerview")
    widget_sorting_succeeds.show()

    # start event loop
    sys.exit(app.exec_())

回答1:


Ok, found out that setClickable(True) will do the trick. Seems to be on by default on QTableView header views but not on instances created manually.



来源:https://stackoverflow.com/questions/44474135/qtableview-sorting-fails-after-sethorizontalheader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!