I need to drop an item from a QTreeView with a QStandardItemModel into a QLineEdit.
I'm a little lost at how to get the data from the QTreeView. Assuming this has something to do with re-implementing the dropMimeData method but dealing with mimeData is not something I do that often (or fully understand for that matter).
Here's a skeletal bit of example code, I need to drag the items in MyTreeView into MyLineEdit and have it set the text to whatever the item text is.
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()