gpt4 book ai didi

Python/PyQt4 : How to make a QComboBox Item draggable

转载 作者:行者123 更新时间:2023-11-30 23:29:24 26 4
gpt4 key购买 nike

我只是为了好玩而摆弄 PyQt...

到目前为止,我有以下代码,它接受从文本字段中删除的内容以填充 QComboBox:

class ComboBox(QtGui.QComboBox):
def __init__(self, parent):
super(ComboBox, self).__init__(parent)

self.setAcceptDrops(True)

def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()

def dropEvent(self, e):

self.addItem(QtCore.QString(e.mimeData().text()))

我现在想让 QComboBox 中的项目可拖动(就像您可以使用以下方法对 QLineEdit 进行操作一样:

.setDragEnabled(True) 

有人知道我该怎么做吗?

非常感谢

P

最佳答案

您可以使用combobox.view().setDragDropMode(Qt.QAbstractItemView.DragOnly)来启用拖动。以下工作示例说明了如何实现将项目从一个组合框拖动到另一个组合框:

combobox = Qt.QComboBox()
combobox.addItems(["test1", "test2", "test3"])
combobox.show()
combobox.view().setDragDropMode(Qt.QAbstractItemView.DragOnly)

model_mime_type = 'application/x-qabstractitemmodeldatalist'

class ComboBox(Qt.QComboBox):
def __init__(self):
super(ComboBox, self).__init__()
self.setAcceptDrops(True)

def dragEnterEvent(self, e):
if e.mimeData().hasFormat(model_mime_type) or \
e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()

def dropEvent(self, e):
if e.mimeData().hasFormat(model_mime_type):
encoded = e.mimeData().data(model_mime_type)
stream = Qt.QDataStream(encoded, Qt.QIODevice.ReadOnly)
while not stream.atEnd():
row = stream.readInt()
column = stream.readInt()
map = stream.readQVariantMap()
if len(map.values()) == 1:
self.addItem(map.values()[0].toString())
combobox.hidePopup()
else:
self.addItem(Qt.QString(e.mimeData().text()))

c2 = ComboBox()
c2.show()

关于Python/PyQt4 : How to make a QComboBox Item draggable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194959/

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