gpt4 book ai didi

python - QListWidget:如何拖放插入带有图标的自定义小部件?

转载 作者:行者123 更新时间:2023-12-01 08:20:24 25 4
gpt4 key购买 nike

我创建了一个自定义 QListWidget,其中添加了自定义列表小部件(在本例中只是 QPushButton 小部件),所有设置都添加到它的 QListWidgetItems 中,以便我可以从 QPushButton 拖放,以向 QListWidget 添加另一个按钮。在第一个列表小部件中,您会注意到两个列表小部件项目之间有一条虚线,表示您可以在两个现有小部件之间拖动并插入小部件。然而,通过我的自定义列表,我失去了这种行为。我仍然可以将它们放在现有的小部件之间,但是没有像以前那样向用户提供视觉指示,即两个小部件之间将插入的水平条。有谁知道我会怎么做?您可以在下图中看到,第一个列表中的行是在两个列表之间插入项目的默认指示符。红色箭头指向我想要创建的那种自定义插入指示器。

insertIndicator

其次,当我从自定义 QPushButton 中拖放时,它会突出显示为黑色,但再也不会返回到正常的灰色,如上图所示。我怎样才能让这个按钮恢复到默认状态?

from inspect import isclass
from shiboken2 import wrapInstance
from PySide2 import QtCore, QtGui, QtWidgets
from maya import OpenMayaUI as omui


def show_ui():
main_win_obj = omui.MQtUtil.mainWindow()
main_win = wrapInstance(long(main_win_obj), QtWidgets.QWidget)
win = Test(parent=main_win)
win.show()


class DragButton(QtWidgets.QPushButton):

def __init__(self, parent=None, text=''):
super(DragButton, self).__init__()
self.setText(text)

def mouseMoveEvent(self, event):

if event.buttons() != QtCore.Qt.LeftButton:
super(DragButton, self).mouseMoveEvent(event)
return

btn_img = self.grab()
painter = QtGui.QPainter(btn_img)
painter.setCompositionMode(
painter.CompositionMode_DestinationIn
)
painter.fillRect(btn_img.rect(), QtGui.QColor(0, 0, 0, 127))
painter.end()

data = QtCore.QMimeData()
data.setText('ReorderListAdd()')

drag = QtGui.QDrag(self)
drag.setMimeData(data)
drag.setPixmap(btn_img)
drag.setHotSpot(event.pos())
drag.exec_(QtCore.Qt.CopyAction)

super(DragButton, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):

data = QtCore.QMimeData()
data.setText('my text')

drag = QtGui.QDrag(self)
drag.setMimeData(data)
drag.exec_()

super(DragButton, self).mouseReleaseEvent(event)


class ReorderList(QtWidgets.QListWidget):

def __init__(self, parent=None):
super(ReorderList, self).__init__(parent)

self.setDragDropMode(
QtWidgets.QAbstractItemView.InternalMove
)
self.setAcceptDrops(True)
self.setAlternatingRowColors(True)
self.setSelectionMode(
QtWidgets.QAbstractItemView.SingleSelection
)
self.setDragDropMode(
QtWidgets.QAbstractItemView.InternalMove
)

def add_btn(self, text='', index=-1):

widget = QtWidgets.QWidget()
if not text:
text = 'item {0}'.format(self.count() + 1)
btn = QtWidgets.QPushButton(text)

layout = QtWidgets.QVBoxLayout(widget)
layout.setContentsMargins(2, 2, 2, 2)
layout.setSpacing(2)
layout.addWidget(btn)

item = QtWidgets.QListWidgetItem()
item.setSizeHint(widget.sizeHint())

if index < 0:
self.addItem(item)
else:
self.insertItem(index, item)
self.setItemWidget(item, widget)

def dropEvent(self, event):
drop_index = self.indexAt(event.pos()).row()
self.add_btn(index=drop_index)
super(ReorderList, self).dropEvent(event)

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

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

def onClick(self):
print self.sender().text()


class Test(QtWidgets.QMainWindow):

def __init__(self, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)

self.list1 = QtWidgets.QListWidget()
self.list1.setDragDropMode(
QtWidgets.QAbstractItemView.DragDrop
)
self.list1.setSelectionMode(
QtWidgets.QAbstractItemView.SingleSelection
)
self.list1.setDragDropMode(
QtWidgets.QAbstractItemView.InternalMove
)
self.list1.setMaximumHeight(60)
self.list2 = ReorderList()
self.list2.setMaximumHeight(120)
for name in ('item 1', 'item 2', 'item 3'):
item = QtWidgets.QListWidgetItem(name)
self.list1.addItem(item)
self.list2.add_btn(text=name)
self.btn = DragButton(text='Add list button')
self.btn.clicked.connect(self.list2.add_btn)

self.widget = QtWidgets.QWidget()
self.setCentralWidget(self.widget)
self.layout = QtWidgets.QVBoxLayout(self.widget)
for item in (self.list1, self.list2, self.btn):
self.layout.addWidget(item)


if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = Test()
win.show()
sys.exit(app.exec_())

我希望在 QListWidget 中插入小部件时能够有一个水平条,并使已药物的按钮返回到默认颜色。

以下是我可以找到的一些类似链接,帮助我做到这一点:

最佳答案

由于拖放是自定义的,因此您必须进行绘制,因此您必须在 DragMoveEvent 方法中使用 QModelIndex 检测项目的矩形。然后进行绘画,但由于小部件的大小,它不会被看到,所以我看了一下,我创建了一个修改编辑器几何形状的委托(delegate)。

class Delegate(QtWidgets.QStyledItemDelegate):
def updateEditorGeometry(self, editor, option, index):
super(Delegate, self).updateEditorGeometry(editor, option, index)
geo = editor.geometry().adjusted(0, 4, 0, 0)
editor.setGeometry(geo)

class ReorderList(QtWidgets.QListWidget):
def __init__(self, parent=None):
super(ReorderList, self).__init__(parent)
delegate = Delegate(self)
self.setItemDelegate(delegate)
self.dropIndicatorRect = QtCore.QRect()
self.setDragDropMode(
QtWidgets.QAbstractItemView.InternalMove
)
self.setAcceptDrops(True)
self.setAlternatingRowColors(True)
self.setSelectionMode(
QtWidgets.QAbstractItemView.SingleSelection
)
self.setDragDropMode(
QtWidgets.QAbstractItemView.InternalMove
)

def add_btn(self, text='', index=-1):
widget = QtWidgets.QWidget()
if not text:
text = 'item {0}'.format(self.count() + 1)
btn = QtWidgets.QPushButton(text)

item = QtWidgets.QListWidgetItem()
item.setSizeHint(btn.sizeHint())

if index < 0:
self.addItem(item)
else:
self.insertItem(index, item)
self.setItemWidget(item, btn)

def dropEvent(self, event):
drop_index = self.indexAt(event.pos()).row()
self.add_btn(index=drop_index)
self.dropIndicatorRect = QtCore.QRect()
self.viewport().update()
super(ReorderList, self).dropEvent(event)

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

def dragLeaveEvent(self, event):
self.dropIndicatorRect = QtCore.QRect()
self.viewport().update()
super(ReorderList, self).dragLeaveEvent(event)

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

def onClick(self):
print(self.sender().text())

def dragMoveEvent(self, event):
index = self.indexAt(event.pos())
if index.isValid():
rect = self.visualRect(index)
if self.dropIndicatorPosition() == QtWidgets.QAbstractItemView.OnItem:
self.dropIndicatorRect = rect
else:
self.dropIndicatorRect = QtCore.QRect()
else:
self.dropIndicatorRect = QtCore.QRect()
self.viewport().update()
super(ReorderList, self).dragMoveEvent(event)

def paintEvent(self, event):
super(ReorderList, self).paintEvent(event)
if not self.dropIndicatorRect.isNull() and self.showDropIndicator():
painter = QtGui.QPainter(self.viewport())
p = QtGui.QPen(painter.pen())
p.setWidthF(1.5)
painter.setPen(p)
r = self.dropIndicatorRect
painter.drawLine(r.topLeft(), r.topRight())

关于python - QListWidget:如何拖放插入带有图标的自定义小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54684840/

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