将项目文本从QTreeView拖放到QLineEdit

时间:2018-04-21 01:49:48

标签: python drag-and-drop pyqt pyside qtreeview

我需要将带有QStandardItemModel的QTreeView中的项目放入QLineEdit。

我对如何从QTreeView获取数据感到有点迷失。假设这与重新实现dropMimeData方法有关,但处理mimeData并不是我经常做的事情(或者完全理解那个问题)。

这是一个示例代码的骨架,我需要将MyTreeView中的项目拖动到MyLineEdit中,并将文本设置为项目文本。

from PySide.QtCore import *
from PySide.QtGui import *

class MyWidget(QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()

        model = MyModel()
        view = MyTreeView()
        view.setModel(model)
        lineEdit = MyLineEdit()

        model.addItem('My Item')
        model.addItem('My Item2')


        layout = QVBoxLayout()
        layout.addWidget(view)
        layout.addWidget(lineEdit)

        self.setLayout(layout)

class MyLineEdit(QLineEdit):
    def __init__(self):
        super(MyLineEdit, self).__init__()
        self.setAcceptDrops( True )

    def dragEnterEvent(self, event):
        data_type = "application/x-qstandarditemmodeldatalist"
        if event.mimeData().hasFormat(data_type):
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        data_type = "application/x-qstandarditemmodeldatalist"
        if event.mimeData().hasFormat(data_type):
            #Get the QStandardItem text somehow?
            item_text = 'Get the text somehow'
            self.setText(item_text)


class MyModel(QStandardItemModel):
    def __init__(self):
        super(MyModel, self).__init__()

    def addItem(self, text):        
        root_item = self.invisibleRootItem()
        item = QStandardItem(text)
        root_item.appendRow(item)

class MyTreeView(QTreeView):
    def __init__(self):
        super(MyTreeView, self).__init__()
        self.setDragEnabled( True )

widget = MyWidget()
widget.show()

2 个答案:

答案 0 :(得分:2)

通过在模型中重新实现mimeData,可以非常简单地实现这一目标:

class MyModel(QStandardItemModel):
    ...
    def mimeData(self, indexes):
        mimedata = super(MyModel, self).mimeData(indexes)
        if indexes:
            mimedata.setText(indexes[0].data())
        return mimedata

这就是所有需要的。您不需要在行编辑中实施dragEnterEventdropEvent

答案 1 :(得分:1)

有几种方法可以获得文本:

  • 使用mimeData()
def dropEvent(self, event):
    data_type = "application/x-qstandarditemmodeldatalist"
    mimeData = event.mimeData()
    if mimeData.hasFormat(data_type):
        encodedData = mimeData.data(data_type)
        stream = QDataStream(encodedData, QIODevice.ReadOnly)
        row = stream.readInt32()
        column = stream.readInt32()
        it = QStandardItem()
        stream >> it
        self.setText(it.text())
  • 使用source()这是执行拖动操作的小部件,在这种情况下它是视图,然后我们访问当前的QModelIndex,这是选定的。{/ li>
def dropEvent(self, event):
    if isinstance(event.source(), QAbstractItemView):
        ix = event.source().currentIndex()
        self.setText(ix.data())

如果您启用了多个项目的选择,则必须使用所选项目:

...

class MyLineEdit(QLineEdit):
    def __init__(self):
        super(MyLineEdit, self).__init__()
        self.setAcceptDrops( True )

    def dragEnterEvent(self, event):
        data_type = "application/x-qstandarditemmodeldatalist"
        if event.mimeData().hasFormat(data_type):
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if isinstance(event.source(), QAbstractItemView):
            ixdexes = event.source().selectedIndexes()
            text = " ".join([ix.data() for ix in sorted(ixdexes)])
            self.setText(text)


...

class MyTreeView(QTreeView):
    def __init__(self):
        super(MyTreeView, self).__init__()
        self.setDragEnabled( True )
        self.setSelectionMode(QAbstractItemView.MultiSelection)

...