gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:32:03 24 4
gpt4 key购买 nike

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

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

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)

# ...

关于python - 如何在从另一个小部件的拖放事件期间突出显示表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com