使用BackgroundRole通过qdatawidgetmapper从qabstractitemmodel读取数据

时间:2016-08-14 22:22:26

标签: python qt pyqt pyqt5

我有以下问题重新讨论Qt(使用PyQt 5):

我修改了我为QDataWidgetMapper和QAbstractTableModel找到的一个例子,并为角色Qt.BackgroundRole添加了一个额外的if子句给数据方法。

目标是根据模型的内容获得不同的背景颜色(例如,如果数组元素等于“错误”,则为红色背景)

适用于qlistview2,但不适用于映射到模型的qLineEdits。我知道这不是默认的QDataWidgetMapper(只是将模型的一部分逐个映射到一个小部件的一个属性)。

将其他属性/信息映射到行编辑(修改其样式,如背景颜色,可见性,启用行编辑/未启用)的最佳做法是什么?

我需要在模型中对这些属性进行计算(对于更复杂的模型),并且不希望对QLineEdit进行子类化(以实现基于QLineEdit文本内容更改颜色的特定方法)。 / p>

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from  PyQt5.QtWidgets import (QWidget, QDataWidgetMapper,
                              QLineEdit, QApplication, QGridLayout, QListView)
from PyQt5.QtCore import Qt, QAbstractTableModel, QModelIndex
from PyQt5.QtGui import QBrush


class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # Set up the widgets.
        self.nameEdit = QLineEdit()
        self.nameEdit2 = QLineEdit()

        # set up the layout
        layout = QGridLayout()
        layout.addWidget(self.nameEdit, 0, 1, 1, 1)
        layout.addWidget(self.nameEdit2, 0, 2, 1, 1)
        self.setLayout(layout)

        self.mapper = None

    def setModel(self, model):
        # Set up the mapper.
        self.mapper = QDataWidgetMapper(self)
        self.mapper.setModel(model)
        self.mapper.addMapping(self.nameEdit, 0)
        self.mapper.addMapping(self.nameEdit2, 1)
        self.mapper.toFirst()


class MyModel(QAbstractTableModel):
    def __init__(self, data, parent=None):
        QAbstractTableModel.__init__(self, parent)
        self.lst = data

    def columnCount(self, parent=QModelIndex()):
        return len(self.lst[0])

    def rowCount(self, parent=QModelIndex()):
        return len(self.lst)

    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        col = index.column()

        if role == Qt.EditRole:
            return self.lst[row][col]
        elif role == Qt.DisplayRole:
            return self.lst[row][col]
        elif role == Qt.BackgroundRole:
            redBackground = QBrush(Qt.red)
            greenBackground = QBrush(Qt.green)
            if self.lst[row][col] == "error":
                return redBackground
            else:
                return greenBackground

    def flags(self, index):
        flags = super(MyModel, self).flags(index)

        if index.isValid():
            flags |= Qt.ItemIsEditable
            flags |= Qt.ItemIsDragEnabled
        else:
            flags = Qt.ItemIsDropEnabled

        return flags

    def setData(self, index, value, role=Qt.EditRole):

        if not index.isValid() or role != Qt.EditRole:
            return False

        self.lst[index.row()][index.column()] = value
        self.dataChanged.emit(index, index)
        return True


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    myModel = MyModel([['row 1 col1', 'error'],
                       ['error', 'row 2 col2'],
                       ['row 3 col1', 'row 3 col2'],
                       ['error', 'row 4 col2']])

    # myModel = MyModel()
    mywindow = Window()
    mywindow.setModel(myModel)

    qlistview2 = QListView()
    qlistview2.setModel(myModel)

    mywindow.show()
    qlistview2.show()

    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我找到了一个简单的解决方案:我必须使用自定义代理来解决问题。

class LineDelegate(QStyledItemDelegate):

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

    def createEditor(self, parent, option, index):
        pass

    def setEditorData(self, editor, index):
        editor.setText(index.model().data(index, Qt.EditRole))
        color = index.model().data(index, Qt.BackgroundColorRole)
        palette = QPalette()
        palette.setBrush(QPalette.Base,color)
        editor.setPalette(palette)
        editor.repaint()        

    def setModelData(self, editor, model, index):
        model.setData(index, editor.text(), Qt.EditRole)

由于我需要多个代理来处理两个QLineEdits,the question and answer given by AlexVhr帮助了很多。

完整修改后的源代码位于pastebin