简体   繁体   English

如何增加 QTableWidget 编辑 state 中的单元格大小超过行大小?

[英]How can I increase the cell size in the QTableWidget editing state by more than the row size?

在此处输入图像描述

I wonder if I can increase the cell size without affecting the row size when I try to fix the cell like the picture above.我想知道当我尝试像上图那样修复单元格时,是否可以在不影响行大小的情况下增加单元格大小。

One could use a custom editor via QStyledItemDelegate with eg several components and a defined minimum size.可以通过 QStyledItemDelegate 使用自定义编辑器,例如几个组件和定义的最小尺寸。

So basically you would have:所以基本上你会有:

  • a table model (subclassed from QAbstractTableModel)表 model(从 QAbstractTableModel 继承而来)
  • there flags with Qt.ItemIsEditable is defined定义了带有 Qt.ItemIsEditable 的标志
  • for your column with the tags you would set your own delegate via setItemDelegateForColumn对于带有标签的列,您将通过 setItemDelegateForColumn 设置自己的委托
  • the delegate inherits from QStyledItemDelegate委托继承自 QStyledItemDelegate
  • override createEditor and return your custom editor覆盖 createEditor 并返回您的自定义编辑器
  • override updateEditorGeometry to set size and position of the custom editor覆盖 updateEditorGeometry 以设置自定义编辑器的大小和 position
  • implement setEditorData and setModelData accordingly相应地实施 setEditorData 和 setModelData
  • if you want an additional button to close the editing state, use QApplication.postEvent(self, QKeyEvent(QKeyEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier))如果你想要一个额外的按钮来关闭编辑 state,使用QApplication.postEvent(self, QKeyEvent(QKeyEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier))

Demo演示

A small example showing two QLineEdit elements and a button to change text and background color of a cell could look like this:一个显示两个 QLineEdit 元素和一个用于更改单元格文本和背景颜色的按钮的小示例可能如下所示:

演示

As you can see, the height of the custom editor in the editing state of the QTableWidget is greater than the row height.可以看到,QTableWidget的编辑state中自定义编辑器的高度大于行高。

Complete, Self-contained PySide2 Example完整、独立的 PySide2 示例

In code could look like this:在代码中可能如下所示:

import sys
from PySide2 import QtWidgets, QtCore
from PySide2.QtCore import Qt, QSize
from PySide2.QtGui import QColor, QKeyEvent
from PySide2.QtWidgets import QLineEdit, QPushButton, QVBoxLayout, QApplication


class MyDelegate(QtWidgets.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtWidgets.QStyledItemDelegate.__init__(self, parent)

    def createEditor(self, parent, option, index):
        return CustomEditor(parent)

    def updateEditorGeometry(self, editor, option, index):
        # adjust position if close to border
        editor.setGeometry(option.rect)

    def paint(self, painter, option, index):
        (text, bgColor) = ('', '')
        data = index.data()
        if type(data) == tuple and len(data) > 1:
            (text, bgColor) = data
        painter.setBrush(QColor(bgColor))
        painter.drawRect(option.rect)
        painter.setPen(QColor(255, 255, 255))
        painter.drawText(option.rect, QtCore.Qt.AlignCenter, text)

    def setEditorData(self, editor, index):
        data = index.data()
        if type(data) == tuple and len(data) > 1:
            (text, bgColor) = index.data()
            editor.edit1.setText(text)
            editor.edit2.setText(bgColor)

    def setModelData(self, editor, model, index):
        model.setData(index, (editor.edit1.text(), editor.edit2.text()))


class CustomEditor(QtWidgets.QWidget):

    def __init__(self, parent):
        QtWidgets.QWidget.__init__(self, parent)
        self.edit1 = QLineEdit()
        self.edit2 = QLineEdit()
        self.button = QPushButton("close")
        layout = QVBoxLayout()
        layout.addWidget(self.edit1)
        layout.addWidget(self.edit2)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.button.clicked.connect(self.saveAndClose)
        self.setAutoFillBackground(True)
        self.setMinimumSize(QSize(128, 128))

    def saveAndClose(self):
        QApplication.postEvent(self, QKeyEvent(QKeyEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier))


class Model(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.colorData = [('abc', '#173f5f'),
                          ('def', '#20639b'),
                          ('ghi', '#3caea3'),
                          ('red', '#ff0000'),
                          ('mno', '#ed553b')]

    def rowCount(self, parent):
        return len(self.colorData)

    def columnCount(self, parent):
        return 2

    def data(self, index, role):
        if role == Qt.DisplayRole or role == Qt.EditRole:
            return self.colorData[index.row()]
        return None

    def setData(self, index, value, role=None):
        if role != Qt.EditRole:
            self.colorData[index.row()] = value
            self.dataChanged.emit(index, index)
            return True
        return False

    def flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsEditable


class TableView(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        tableModel = Model()
        tableView = QtWidgets.QTableView()
        tableView.setModel(tableModel)
        mydelegate = MyDelegate(self)
        tableView.setItemDelegateForColumn(1, mydelegate)

        hbox = QtWidgets.QHBoxLayout()
        hbox.addWidget(tableView)
        self.setLayout(hbox)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    tableView = TableView()
    tableView.setMinimumSize(QSize(400, 360))
    tableView.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM