【问题标题】:How can I select cells in a QTableView like characters in a QTextEdit?如何像 QTextEdit 中的字符一样选择 QTableView 中的单元格?
【发布时间】:2015-04-28 00:05:58
【问题描述】:

我想使用 QTableView 构建一个类似于十六进制编辑器的视图。每个单元格将代表一个字节的数据。如何配置 QTableView 选择行为,使其类似于典型的文本编辑控件?也就是说,它应该选择一行上剩余的单元格、任何中间行的全部内容以及最后一行的部分内容,而不是选择一个矩形的单元格区域。

在图表形式中(x 被选中,. 未被选中),我想要这个:

..................
......xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx......
..................

我确实想要这个:

..................
......xxxxxx......
......xxxxxx......
......xxxxxx......
......xxxxxx......
..................

【问题讨论】:

    标签: qt


    【解决方案1】:

    最终,我通过继承QItemSelectionModel 并覆盖select 方法成功地更改了默认行为,如下所述:https://stackoverflow.com/a/10920294/87207

    这是我的代码的关键部分:

    class RollingItemSelectionModel(QItemSelectionModel):
        def qindex2index(self, index):
            """ convert QModelIndex to offset into buffer """
            m = self.model()
            return (m.columnCount() * index.row()) + index.column()
    
        def index2qindex(self, index):
            """ convert offset into buffer into QModelIndex """
            m = self.model()
            r = index // m.columnCount()
            c = index % m.columnCount()
            return m.index(r, c)
    
        def select(self, selection, selectionFlags):
            # PyQt5 doesn't have method overloading, so we type switch
            if isinstance(selection, QItemSelection):
                # This is the overload with the QItemSelection passed to arg 0
                qindexes = selection.indexes()
                indices = []
    
                for qindex in qindexes:
                    indices.append(self.qindex2index(qindex))
    
                if indices:
                    low = min(indices)
                    high = max(indices)
    
                    selection = QItemSelection()
                    for i in xrange(low, high):
                        qi = self.index2qindex(i)
                        # inefficient, but functional
                        # manually add all the bytes to select, one-by-one
                        selection.select(qi, qi)
            elif isinstance(selection, QModelIndex):
                # This is the overload with the QModelIndex passed to arg 0
                # since it's a single index, already works as expected.
                pass
    
            else:  # Just in case
                raise RuntimeError("Unexpected type for arg 0: '%s'" % type(selection))
    
            # Fall through. Select as normal
            super(RollingItemSelectionModel, self).select(selection, SelectionFlags)
    

    【讨论】:

      【解决方案2】:

      我认为您可以为此目的使用 QItemSelection/QItemSelectionRange 类。

      创建一个新的项目选择模型:

      QItemSelectionModel::select( ... )
      

      然后将其传递给视图:

      QAbstractItemView::setSelectionModel( ... )
      

      【讨论】:

      • 感谢您的回复。我已经阅读了 QItemSelectionRange 的文档,它似乎表明该行为捕获了矩形区域:“这可以被可视化为表格中的二维单元格块”。您能否建议我可以提供哪些选项来选择问题中描述的区域?
      • 我认为这个this 会帮助你。
      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多