如何在另一个小部件的拖放事件期间突出显示 table 单元格

How to highlight table cell during drag&drop event from another widget

有两个可停靠小部件:一个带有图标 (QLabel),另一个带有 table(QTableWidget)。

我一直在尝试在第二个小部件的拖放事件期间在鼠标悬停时实现 tables 的单元格颜色。问题是他们不这样做。 tables' class 很简单,添加了悬停事件过滤器。

class TableEdit(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(TableEdit, self).__init__(parent=parent)

        # self.setMinimumSize(QtCore.QSize(self.width(), self.height()))

        self.current_hover = [0, 0]
        row = 3
        column = 3

        self.setColumnCount(column)
        self.setAlternatingRowColors(True)

        self.setRowCount(row)

        for row in range(0, 3):
            for column in range(0, 3):
                item = QtWidgets.QTableWidgetItem("This is cell {} {}".format(row + 1, column + 1))
                self.setItem(row, column, item)

        self.setHorizontalHeaderItem(0, QtWidgets.QTableWidgetItem("Col1"))
        self.horizontalHeader().setVisible(False)
        # self.setShowGrid(False)

        self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)

        self.setAcceptDrops(True)
        self.setMouseTracking(True)
        self.cellEntered.connect(self.cellHover)

    def cellHover(self, row, column):
        item = self.item(row, column)
        old_item = self.item(self.current_hover[0], self.current_hover[1])
        if self.current_hover != [row, column]:
            old_item.setBackground(QtGui.QBrush(QtGui.QColor('white')))
            item.setBackground(QtGui.QBrush(QtGui.QColor('yellow')))
        self.current_hover = [row, column]

    def dragEnterEvent(self, event):
        event.accept()
        print("drag enter +")

    def dragMoveEvent(self, event):
        event.accept()

    def dropEvent(self, event):
        event.accept()
        print("drop +")

        mime = event.mimeData()
        if mime.hasFormat("application/x-fire"):
            print("Fire")

这是一张屏幕截图,显示当您将 QLabel 拖入单元格时没有任何反应。

您必须实现 dragMoveEvent 方法并实现颜色逻辑,要获取您必须使用 QCursor 的位置的项目:

class TableEdit(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(TableEdit, self).__init__(parent)
        self.hover_item = None

        row, column = 3, 3
        self.setColumnCount(column)
        self.setAlternatingRowColors(True)
        self.setRowCount(row)

        for r in range(self.rowCount()):
            for c in range(self.columnCount()):
                item = QtWidgets.QTableWidgetItem("This is cell {} {}".format(r + 1, c + 1))
                self.setItem(r, c, item)

        self.setHorizontalHeaderItem(0, QtWidgets.QTableWidgetItem("Col1"))
        self.horizontalHeader().setVisible(False)
        # self.setShowGrid(False)
        self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)

        self.setAcceptDrops(True)
        self.setMouseTracking(True)

    def dragMoveEvent(self, event):
        p = self.viewport().mapFromGlobal(QtGui.QCursor().pos())
        it = self.itemAt(p)
        if self.hover_item != it:
            if self.hover_item is not None:
                self.hover_item.setBackground(QtCore.Qt.white)
            self.hover_item = it
            if self.hover_item is not None:
                self.hover_item.setBackground(QtCore.Qt.yellow)
        super(TableEdit, self).dragMoveEvent(event)

    # ...