<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Drag and drop into QMdiArea]]></title><description><![CDATA[<p dir="auto">Hi folks!</p>
<p dir="auto">I've successfully created an example that uses the Model/View programming to read some data (from an <em>xlsx</em> file) and fills:</p>
<ul>
<li>A <em>QListView</em> with the signals header.</li>
<li>A <em>QTableView</em> with the spreadsheet values.</li>
</ul>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/cec7acd4-37ba-4e7f-a161-29e4a666e3d1.png" alt="eb0caa93-941a-47c8-a963-2ec65a4ab446-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">The application is based on a <em>QAbstractTableModel</em> that stores the header and the data which are used by the above views as in the code:</p>
<pre><code>def main():

    app = QApplication(sys.argv)
    data = MyData(filepath=XSLX_FILE).load_data()
    model_table = MyModelTable(parent=None, data=data[:])
    model_list = MyModelList(parent=None, data=data[:])
    view = MyView(parent=None, model_table=model_table, model_list=model_list)
    view.show()
    sys.exit(app.exec())
</code></pre>
<p dir="auto">What I'd like to achieve is to be able to <em>drag and drop</em> (any of) the signals from the <em>QListView</em> into the <em>MdiArea</em> and plot them.<br />
More specifically, I'd like to get a "pop-up menu" (like a <em>dropdown</em> widget to give you an idea) at the mouse pointer, where I can select which type of widget use to plot the data (scatter, lines...).</p>
<p dir="auto">I read the <a href="https://doc.qt.io/qt-6/model-view-programming.html#using-drag-and-drop-with-item-views" target="_blank" rel="noopener noreferrer nofollow ugc">official documentation on the topic</a> and I also took a look to some <a href="https://doc.qt.io/qt-6/examples-draganddrop.html" target="_blank" rel="noopener noreferrer nofollow ugc">examples</a>, but I still miss some concepts that I hope you could help me to understand:</p>
<ul>
<li>Is it correct use the <em>QMdiArea</em> as a "desktop" for my widgets?</li>
<li>The item dropped from the <em>QListView</em> is of a <em>QMimeData</em> type?
<ul>
<li>From the <a href="https://doc.qt.io/qt-6/qmimedata.html" target="_blank" rel="noopener noreferrer nofollow ugc">documentation</a>, to me, this data types looks related to the clipboard, why should be the designated one for the drag and drop action?</li>
</ul>
</li>
<li>The <em>QListView</em> needs to have the <em>QWidget::setAcceptDrops</em> property even if it will be used as "source" of the data to be dropped? In some example (like <a href="https://www.pythonstudio.us/pyqt-programming/drag-and-drop.html" target="_blank" rel="noopener noreferrer nofollow ugc">this</a>) was the case, but I'm not convinced.</li>
<li>How many methods do I need to re-implement in the model to make it work?
<ul>
<li>(<em>QDragEnterEvent</em>, <em>QDragMoveEvent</em>, ...) Unfortunately this is still not clear to me, who and when needs to be used.</li>
</ul>
</li>
<li>There are many other properties (like: <em>QAbstractItemView::setDragDropMode</em>, ...) that I'm not sure if they are needed just to enable the drag and drop functionality, or are used to enhance the example functionalities.</li>
</ul>
<p dir="auto">--<br />
Here the contents of the view and the model files:</p>
<p dir="auto"><a href="http://view.py" target="_blank" rel="noopener noreferrer nofollow ugc">view.py</a></p>
<pre><code>from PyQt6.QtWidgets import QWidget, QVBoxLayout, QTableView, QListView, QMdiArea, QSplitter, QAbstractItemView
from PyQt6 import QtCore
from PyQt6.QtCore import QAbstractTableModel, QAbstractListModel

class MyView(QWidget):
    def __init__(self, model_table: QAbstractTableModel, model_list: QAbstractListModel, parent=None) -&gt; None:
        super().__init__(parent=parent)

        # create layout
        self.initUI(model_table=model_table, model_list=model_list)

    def initUI(self, model_table: QAbstractTableModel, model_list: QAbstractListModel) -&gt; None:

        # make the vbox the external "container"
        vbox = QVBoxLayout()
        #
        self.list_data = QListView()
        self.list_data.setAcceptDrops(True)
        self.list_data.setDragEnabled(True)
        self.list_data.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
        self.list_data.setDragDropMode(QAbstractItemView.DragDropMode.DragDrop)
        # show drop indicator
        #self.list_data.setDropIndicatorShown(True)
        self.mdi_area = QMdiArea()
        self.mdi_area.setAcceptDrops(True)
        
        #
        # add a vertical splitter between the list view and the mdi area
        h_splitter = QSplitter(QtCore.Qt.Orientation.Horizontal)
        h_splitter.addWidget(self.list_data)
        h_splitter.addWidget(self.mdi_area)
        #
        #
        # add table for the table model view
        self.table_data = QTableView()
        #
        # add an horizontal splitter between the above hbox and the next table view
        v_splitter = QSplitter(QtCore.Qt.Orientation.Vertical)
        v_splitter.addWidget(h_splitter)
        v_splitter.addWidget(self.table_data)
        vbox.addWidget(v_splitter)
        self.setLayout(vbox)

        # assign model to the table-data widget
        self.table_data.setModel(model_table)
        self.table_data.resizeColumnsToContents()
        #assign model to the list data
        self.list_data.setModel(model_list)


        self.setGeometry(0, 0, 640, 480)
        self.center()
        self.setWindowTitle("Demo MVC")

    def center(self):
        # this represents the created window size (is not the screen size!)
        window_size = self.frameGeometry()
        # get the screen size
        screen_size = self.screen().availableGeometry()
        # move the window (screen reference is always the top left) to the center of the screen
        window_size.moveCenter(screen_size.center())
        # finally move the window reference to top left, so the rectagle matches the screen reference
        self.move(window_size.topLeft())
</code></pre>
<p dir="auto"><a href="http://model.py" target="_blank" rel="noopener noreferrer nofollow ugc">model.py</a></p>
<pre><code>from dataclasses import dataclass
from sys import flags
from typing import Any
from PyQt6.QtCore import QAbstractTableModel, QAbstractListModel, Qt, QModelIndex, QObject
from PyQt6.QtGui import QColor
from PyQt6 import QtCore

@dataclass
class SignalDefinition:
    name: str
    description: str

class MyModelList(QAbstractListModel):
    def __init__(self, data: list, parent: QObject = None) -&gt; None:
        super(MyModelList, self).__init__(parent=parent)

        # extract info - related to the signal list
        self._descr = data.pop(0)
        self._signals = data.pop(0)
        self._definitions = []

        for row in range(len(self._signals)):
            self._definitions.append(SignalDefinition(self._signals[row], self._descr[row]))


    # reimplement the model methods
    def rowCount(self, parent: QModelIndex = QModelIndex()) -&gt; int:
        if parent.isValid():
            return 0

        return len(self._signals)

    def data(self, index: QModelIndex, role: Qt.ItemDataRole) -&gt; Any:
        
        definition = self._definitions[index.row()]

        if role == Qt.ItemDataRole.DisplayRole:
            return definition.name
        elif role == Qt.ItemDataRole.ToolTipRole:
            return definition.description

        return None


    def supportedDragActions(self) -&gt; Qt.DropAction:

        return Qt.DropAction.CopyAction or Qt.DropAction.MoveAction
class MyModelTable(QAbstractTableModel):
    def __init__(self, data: list, parent: QObject = None) -&gt; None:
        super(MyModelTable, self).__init__(parent=parent)
        
        # extract info - related to the table
        self._descr = data.pop(0)
        self._header = data.pop(0)
        self._data = data
        self._rows = len(data)
        self._columns = len(data[0])

    # reimplement the model methods
    def rowCount(self, parent: QModelIndex) -&gt; int:
        if parent.isValid():
            return 0
        return self._rows

    def columnCount(self, parent: QModelIndex) -&gt; int:
        if parent.isValid():
            return 0
        return self._columns

    def data(self, index: QModelIndex, role: Qt.ItemDataRole) -&gt; Any:
        if role != Qt.ItemDataRole.DisplayRole:
            return QtCore.QVariant()

        return self._data[index.row()][index.column()]

    def headerData(
        self, section: int, orientation: Qt.Orientation, role: Qt.ItemDataRole
    ) -&gt; Any:
        if orientation == Qt.Orientation.Horizontal:

            if role == Qt.ItemDataRole.BackgroundRole and section == 0:
                return QColor("blue")

            if role == Qt.ItemDataRole.ForegroundRole and section == 1:
                return QColor("red")

            if role == Qt.ItemDataRole.DisplayRole:
                return self._header[section]
            if role == Qt.ItemDataRole.ToolTipRole:
                return self._descr[section]

        if orientation == Qt.Orientation.Vertical:
            if role == Qt.ItemDataRole.DisplayRole:
                return None
</code></pre>
<p dir="auto">I know, I'm missing a lot of things... But I didn't find how to link all the info together.<br />
Is there someone that can help me to understand what I miss (step by step) in the right order from my current <strong>working</strong> example?</p>
<p dir="auto">Super thanks!<br />
AGA</p>
]]></description><link>https://forum.qt.io/topic/135046/drag-and-drop-into-qmdiarea</link><generator>RSS for Node</generator><lastBuildDate>Fri, 23 Oct 2026 13:13:30 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/135046.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 14 Mar 2022 17:54:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Wed, 20 Jul 2022 18:30:01 GMT]]></title><description><![CDATA[<p dir="auto">You should create your own mime type(s) since they are specific to your application / data structure.</p>
<p dir="auto">As for QDataStream, it's the easier way if you have complex data structures that cannot be represented by text.</p>
<p dir="auto">Create the QDataStream operators for your class/struct, use them to serialize the data on the sending end and deserialize them on the receiving end.</p>
]]></description><link>https://forum.qt.io/post/722071</link><guid isPermaLink="true">https://forum.qt.io/post/722071</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 20 Jul 2022 18:30:01 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Mon, 18 Jul 2022 09:59:37 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>,</p>
<p dir="auto">Many thanks for your valuable help!<br />
Thanks to your example I think I'm heading to the right direction.</p>
<ul>
<li>First of all I understood that the custom <em>mime</em> data type it is just a label, probably was obvious to many, but not to me.</li>
<li>I have to first encode (in the <em>model</em>) and then decode (in the <em>view</em>) data with the same "tools" like same mimeType, and "data container".</li>
</ul>
<p dir="auto">Now my code looks like:</p>
<p dir="auto"><em><a href="http://view.py" target="_blank" rel="noopener noreferrer nofollow ugc">view.py</a></em> Here I just added some of your code, so this is not related to what I want to finally get.</p>
<pre><code>class DropQMdiArea(QMdiArea):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)
    
    ...
    ...

    # this method is invoked when the mouse enters (while dragging) this (mdiArea) area
    def dragEnterEvent(self, event) -&gt; None:
        event.accept()
        if event.mimeData().hasFormat('application/x-qstandardlist'):
            event.accept()
        else:
            event.ignore()
    
    # this method is invoked when the mouse drops in this (mdiArea) area
    def dropEvent(self, event) -&gt; None:
        if event.mimeData().hasFormat('application/x-qstandardlist'):
            data = event.mimeData().data('application/x-qstandardlist')
            stream = QDataStream(data, QIODevice.OpenModeFlag.ReadOnly)
            
            event.accept()
        else:
            event.ignore()

        data = event.mimeData().data("application/x-qstandardlist")
        label = QLabel(bytes(data).decode())

        sub_window = self.addSubWindow(label)
        sub_window.setAttribute(QtCore.Qt.WidgetAttribute.WA_DeleteOnClose)
        sub_window.show()
</code></pre>
<p dir="auto"><em><a href="http://model.py" target="_blank" rel="noopener noreferrer nofollow ugc">model.py</a></em> This doesn't work properly, in fact I still have  (unfortunately for you 😅) other questions.</p>
<pre><code>class MyModelList(QAbstractListModel):
    def __init__(self, data: list, parent: QObject = None) -&gt; None:
        super(MyModelList, self).__init__(parent=parent)

        self._data = data
        ...
        ...

    def supportedDragActions(self) -&gt; Qt.DropAction:
        return Qt.DropAction.CopyAction


    def flags(self, index: QModelIndex) -&gt; Qt.ItemFlag:
        flags_default = super(MyModelList, self).flags(index)
        
        if index.isValid():
            return (Qt.ItemFlag.ItemIsDragEnabled | flags_default)


    def mimeTypes(self):
        # HThe general structure of MIME is type/subtype
        return ["application/x-qstandardlist"]

    def mimeData(self, index):
        encoded_data = QtCore.QByteArray()
        # is this stream needed?
        stream = QDataStream(encoded_data, QIODevice.OpenModeFlag.WriteOnly)
        for idx in index:
            if not idx.isValid():
                continue
            else:
                selected_row = idx.row()
                selected_list = [row[selected_row] for row in self._data]
                mime_data = QMimeData()
                mime_data.setData("application/x-qstandardlist", encoded_data)
                # if needed, how should I use it?
                stream = selected_list

</code></pre>
<p dir="auto">I saw some (misleading?) examples where before send data I had to:</p>
<ul>
<li>create a <em>QByteArray</em> "data container".</li>
<li>create a <em>QDataStream</em> to "transport" the previous data container.
<ul>
<li>Is this necessary?</li>
</ul>
</li>
<li>Is there a "general" way to "pack" the moved data?
<ul>
<li>I know that <em>mime</em> has the methods: <em>setText()</em>, <em>setHtml()</em>, ..., but I'm not sure if, in case of list, do I need to use one of those or do I need to "encapsulate" these within the stream. Examples that I found are generally text based.</li>
</ul>
</li>
<li>Is it correct use the <em>setData()</em> method on the <em>mime</em> object? (<em>mime_data.setData("application/x-qstandardlist", encoded_data)</em>)</li>
</ul>
<p dir="auto">At the moment I'm not sure if data is correctly "copied" from the <em>model</em> to the <em>view</em></p>
<p dir="auto">Kind regards,<br />
AGA</p>
]]></description><link>https://forum.qt.io/post/721778</link><guid isPermaLink="true">https://forum.qt.io/post/721778</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Mon, 18 Jul 2022 09:59:37 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Tue, 12 Jul 2022 15:59:47 GMT]]></title><description><![CDATA[<p dir="auto">Here you have minimal example for dragging an item from a QListView to a QMdiArea:</p>
<pre><code class="language-python">import sys

from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItemModel
from PySide6.QtGui import QStandardItem
from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtWidgets import QLabel
from PySide6.QtWidgets import QListView
from PySide6.QtWidgets import QMdiArea
from PySide6.QtWidgets import QWidget


class DNDMdiArea(QMdiArea):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        print(event.mimeData().formats())
        event.accept()

    def dropEvent(self, event):
        data = event.mimeData().data("application/x-qstandarditemmodeldatalist")
        label = QLabel(bytes(data).decode())

        sub_window = self.addSubWindow(label)
        sub_window.setAttribute(Qt.WA_DeleteOnClose)
        sub_window.show()
        event.accept()


if __name__ == "__main__":
    app = QApplication(sys.argv)

    mdi_area = DNDMdiArea()
    list_view = QListView()
    list_view.setDragEnabled(True)
    model = QStandardItemModel()
    model.setColumnCount(1)
    model.setRowCount(5)
    model.setHorizontalHeaderLabels(["Header"])
    for i in range(model.rowCount()):
        model.setItem(i, QStandardItem(f"Item {i}"))

    list_view.setModel(model)

    widget = QWidget()
    layout = QHBoxLayout(widget)
    layout.addWidget(list_view)
    layout.addWidget(mdi_area)

    widget.show()

    sys.exit(app.exec())
</code></pre>
<p dir="auto">There is nothing fancy here. It's just to show you the basics. You can replace the QStandardItemModel with yours.<br />
It also shows data retrieval from mime data.</p>
]]></description><link>https://forum.qt.io/post/721090</link><guid isPermaLink="true">https://forum.qt.io/post/721090</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 12 Jul 2022 15:59:47 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Tue, 12 Jul 2022 10:27:26 GMT]]></title><description><![CDATA[<p dir="auto">HI <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>,</p>
<p dir="auto">to recap you suggestions:</p>
<ul>
<li>Drag needs to be implemented in the model.</li>
<li>Drag widget (<em>QListView</em>) doesn't need to be re-implemented in the view; just its properties need to be set to accept drop.</li>
<li>Drop doesn't need to be re-implemented in the model but only in the view.</li>
<li>Drop widget (<em>QMdiArea</em>) does need to be re-implemented in the view with the methods: <em>dragEnterEvent</em>, <em>dragMoveEvent</em> and <em>dropEvent</em>.</li>
</ul>
<p dir="auto">But from the <a href="https://doc.qt.io/qt-6/model-view-programming.html#inserting-dropped-data-into-a-model" target="_blank" rel="noopener noreferrer nofollow ugc">documentation</a> looks that drop still need to be part of the model. This is the part where I was referring to. I didn't understand <a href="https://doc.qt.io/qt-6/model-view-programming.html#inserting-dropped-data-into-a-model" target="_blank" rel="noopener noreferrer nofollow ugc">how to encode</a> my data.</p>
<p dir="auto">The goal is to plot dropped data into a graph in the <em>QMdiArea</em>.</p>
<p dir="auto">Many thanks!<br />
AGA</p>
]]></description><link>https://forum.qt.io/post/720974</link><guid isPermaLink="true">https://forum.qt.io/post/720974</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Tue, 12 Jul 2022 10:27:26 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Mon, 11 Jul 2022 20:20:56 GMT]]></title><description><![CDATA[<p dir="auto">Oh !</p>
<p dir="auto">I thought you were using your custom widget based on QMdiArea. The one where you reimplemented the dragEnter, dragMove and dropEvent. It's in dragEnterEvent that you can decide whether the mime type fits your widget.</p>
]]></description><link>https://forum.qt.io/post/720953</link><guid isPermaLink="true">https://forum.qt.io/post/720953</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 11 Jul 2022 20:20:56 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Mon, 11 Jul 2022 06:51:06 GMT]]></title><description><![CDATA[<p dir="auto">HI <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> ,</p>
<p dir="auto">Both the widgets (<em>QListView</em> and <em>QMdiArea</em>) form the <a href="http://view.py" target="_blank" rel="noopener noreferrer nofollow ugc">view.py</a> are "configured" (not reimplemented) as:</p>
<pre><code>        ...
        ...
        vbox = QVBoxLayout()
        #
        # place the list view
        self.list_data = QListView(self)
        self.list_data.setDragEnabled(True)
        self.list_data.setDropIndicatorShown(True)
        #
        # place the mdi area
        self.mdi_area = QMdiArea(self)
        self.mdi_area.setAcceptDrops(True)
        #
        # add a vertical splitter between the list view and the mdi area
        h_splitter = QSplitter(QtCore.Qt.Orientation.Horizontal)
        ...
        ...
</code></pre>
<p dir="auto">So no, the <em>QMdiArea</em> has the <em>setAcceptDrop</em> properties.</p>
<p dir="auto">I assume that the <em>QMdiArea</em> widget need to be re-implemented to achieve a minimum "functionality" since it basically doesn't do anything. is this correct? What is the minimum code should I add to "see the dropped data" (also as print)?</p>
<p dir="auto">Before do that (re-implement the above widget) I wanted to have the "mime" working, but unfortunately I didn't find any clear explanation that helped me <strong>to understand</strong>.<br />
To be more specific, looking on the <a href="https://doc.qt.io/qt-6/model-view-programming.html#encoding-exported-data" target="_blank" rel="noopener noreferrer nofollow ugc">official help page</a> I couldn't find how to "classify" my source data. The example refers to the "plain text" but my data is a <em>list</em>, so in this case I don't know how to "define" the data to be passed. Should I use a <a href="https://doc.qt.io/qt-6/qmetaobject.html" target="_blank" rel="noopener noreferrer nofollow ugc"><em>meta object</em></a>? In that case I didn't find a clear example.</p>
<p dir="auto">Many thanks to anyone that can help!<br />
AGA</p>
<pre><code></code></pre>
]]></description><link>https://forum.qt.io/post/720853</link><guid isPermaLink="true">https://forum.qt.io/post/720853</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Mon, 11 Jul 2022 06:51:06 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Fri, 08 Jul 2022 18:30:39 GMT]]></title><description><![CDATA[<p dir="auto">Any chances you forget to call <code>setAcceptDrops(True);</code>  in your custom QMdiArea ?</p>
]]></description><link>https://forum.qt.io/post/720667</link><guid isPermaLink="true">https://forum.qt.io/post/720667</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 08 Jul 2022 18:30:39 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Fri, 08 Jul 2022 11:46:46 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
fixing the model I've been able to start the <em>view</em> to show the drag and drop behavior that I had when I subclasses the widgets.<br />
So, one mistake I made was on model, I forgot to re-implement the methods: <em>supportedDragActions</em> and <em>flags</em>.<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Not sure if you were referring to these or others (not in the model) but looks that these at least these are necessary to let the model support the drag and drop functionality.</p>
<pre><code>class MyModelList(QAbstractListModel):
    def __init__(self, data: list, parent: QObject = None) -&gt; None:
        super(MyModelList, self).__init__(parent=parent)

	...
	...
	
    def supportedDragActions(self) -&gt; Qt.DropAction:
        return Qt.DropAction.CopyAction


    def flags(self, index: QModelIndex) -&gt; Qt.ItemFlag:
        flags_default = super(MyModelList, self).flags(index)
        if index.isValid():
            return (Qt.ItemFlag.ItemIsDragEnabled | flags_default)
</code></pre>
<p dir="auto">Now I progressed up to this level (see picture below), but I still miss: to "move data" and to accept the dropped data into the mdiArea.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/9949c137-e4fd-4b1a-93c0-670641867fe0.png" alt="drag_drop_01.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Any help would be very appreciated!<br />
AGA</p>
]]></description><link>https://forum.qt.io/post/720582</link><guid isPermaLink="true">https://forum.qt.io/post/720582</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Fri, 08 Jul 2022 11:46:46 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Thu, 07 Jul 2022 05:55:39 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>, many thanks to answered to this.</p>
<p dir="auto">Currently when I start to drag I don't see anything (no cursor change and no print messages - put in the methods to check if the code runs there).<br />
When cursor goes into the mdiArea nothing change as well.</p>
<p dir="auto">I also re-implemented the <em>dragMoveEvent</em> while I was trying (sorry to forgot to mention) but didn't change.</p>
<p dir="auto">So, to summarize:</p>
<ul>
<li>The right approach is the second (since my code is Model/View).</li>
<li>The widgets don't need to be subclasses, but it is enough enable/change some properties.</li>
</ul>
<p dir="auto">To start to view "something", both the model and the view need to be correctly "set" or should I start already to see some drag and drop behavior only with a well "configured" <em>view</em>?<br />
I ask you this just to understand in which direction I have to move.</p>
<p dir="auto">Since editing my demo adding the "examples code" didn't produce the desired behavior what should I change/try?<br />
Could you please drive me through a "checklist" to verify all the basic necessary steps to enable the drag and drop?</p>
<p dir="auto">As always, many thanks! 🙏<br />
AGA</p>
]]></description><link>https://forum.qt.io/post/720362</link><guid isPermaLink="true">https://forum.qt.io/post/720362</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Thu, 07 Jul 2022 05:55:39 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Wed, 06 Jul 2022 20:10:47 GMT]]></title><description><![CDATA[<p dir="auto">That last approach is the correct one, custom model + view configuration.</p>
<p dir="auto">What behaviour do you have now ?<br />
If memory serves well, you might also need to implement the dragMoveEvent method on your target widget.</p>
]]></description><link>https://forum.qt.io/post/720337</link><guid isPermaLink="true">https://forum.qt.io/post/720337</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 06 Jul 2022 20:10:47 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Wed, 06 Jul 2022 14:17:07 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>, sorry for the looong absence, but I've been suddenly switched to another project.</p>
<p dir="auto">I understood that I have to replace the 2 drag and drop involved widget subclassing them.<br />
I then created 2 new classes: <em>DragQListView</em> and <em>DropQMdiArea</em><br />
For the new <em>DragQListView</em> subclass (see code below) I re implemented the mouse methods (<em>mousePressEvent</em> and <em>MouseMoveEvent</em> as well explained in the <a href="https://doc.qt.io/qtforpython/overviews/dnd.html#dragging" target="_blank" rel="noopener noreferrer nofollow ugc">drag and drop example</a>).<br />
While for the other class I re implemented the <em>dragEnterEvent</em> and the <em>dropEvent</em>.<br />
With these change I started to move to the "right direction".</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/caa9784c-496a-471c-94a9-cd0d6708323f.png" alt="drag_drop_01.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Here the updated code:</p>
<p dir="auto"><em><a href="http://view.py" target="_blank" rel="noopener noreferrer nofollow ugc">view.py</a></em></p>
<pre><code>        ...
        ...

	# place the list view
	self.list_data = DragQListView()
	#
	# place the mdi area
	self.mdi_area = DropQMdiArea()
	#
	# add a vertical splitter between the list view and the mdi area
	h_splitter = QSplitter(QtCore.Qt.Orientation.Horizontal)
		
	...
        ...

class DragQListView(QListView):
    def __init__(self):
        super().__init__()
        self.setDragEnabled(True)        

    def mousePressEvent(self, event) -&gt; None:
        # allows to drag just with the left button
        if event.buttons() == Qt.MouseButton.LeftButton:
            # save the starting mouse press point
            self.drag_start_pos = event.pos()
            return super().mousePressEvent(event)

    def mouseMoveEvent(self, event) -&gt; None:
        if event.buttons() != Qt.MouseButton.LeftButton:
            return
        if (event.pos() - self.drag_start_pos).manhattanLength() &lt; QApplication.startDragDistance():
            drag = QDrag(self)
            mime_data = QMimeData()
            #mime_data.setData(mimetype=mimeType, data=)
            drag.setMimeData(mime_data)
            drag.exec(Qt.DropAction.CopyAction)
            return super().mouseMoveEvent(event)


class DropQMdiArea(QMdiArea):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)
    
    def dragEnterEvent(self, event) -&gt; None:
        event.accept()   

    def dropEvent(self, event) -&gt; None:
        event.setDropAction(Qt.DropAction.CopyAction)
        event.accept()

</code></pre>
<p dir="auto">While I was continuing reading the doc I saw that for the <em>Model/View</em> there is <a href="https://doc.qt.io/qtforpython/overviews/model-view-programming.html#using-drag-and-drop-with-item-views" target="_blank" rel="noopener noreferrer nofollow ugc">another approach to follow</a>.<br />
It explains that I have to enable certain <strong>properties</strong>:<br />
<em>To allow items to be dragged around, certain properties of the view need to be enabled, and the items themselves must also allow dragging to occur.</em></p>
<p dir="auto">So I reverted my code to the previous version (like in the first post above) and I started to add "these" properties (both in model and in view), but I didn't get the same drag and drop behavior.</p>
<p dir="auto"><em><a href="http://view.py" target="_blank" rel="noopener noreferrer nofollow ugc">view.py</a></em></p>
<pre><code>...
...

    # place the list view
    self.list_data = QListView(self)
    self.list_data.setDragEnabled(True)
    self.list_data.setAcceptDrops(True)
    self.list_data.setDropIndicatorShown(True)
    #
    # place the mdi area
    self.mdi_area = QMdiArea()
...
...
</code></pre>
<p dir="auto"><em><a href="http://model.py" target="_blank" rel="noopener noreferrer nofollow ugc">model.py</a></em></p>
<pre><code>class MyModelList(QAbstractListModel):
    def __init__(self, data: list, parent: QObject = None) -&gt; None:
        super(MyModelList, self).__init__(parent=parent)

       ...
       ...

    def supportedDragActions(self) -&gt; Qt.DropAction:

        return Qt.DropAction.CopyAction or Qt.DropAction.MoveAction
</code></pre>
<p dir="auto">I feel that this second approach although didn't show any "positive sign" is the right one (probably the other works for the other not Model/View design) but I can't make it work.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> could you please guide me in this step by step process to achieve the desired drag and drop?</p>
<p dir="auto">Many thanks!<br />
AGA</p>
]]></description><link>https://forum.qt.io/post/720288</link><guid isPermaLink="true">https://forum.qt.io/post/720288</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Wed, 06 Jul 2022 14:17:07 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Tue, 15 Mar 2022 20:38:04 GMT]]></title><description><![CDATA[<p dir="auto">For the custom view, my bad. I misunderstood your point about qabstractitemview::setDragDropMode and thought you wanted to create subclasses for your item views. So it's all good.</p>
<p dir="auto">QMdiArea does not know how to handle your custom data hence the need for a subclass. You can see an example in the Dropping part of the drag and drop chapter in the documentation. There they update a combo box content. In your case, you will create a new widget with a chart based.</p>
]]></description><link>https://forum.qt.io/post/706514</link><guid isPermaLink="true">https://forum.qt.io/post/706514</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 15 Mar 2022 20:38:04 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Tue, 15 Mar 2022 09:10:14 GMT]]></title><description><![CDATA[<p dir="auto">HI <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>, many thanks for your answers.</p>
<p dir="auto">These points are clear:</p>
<ul>
<li>For QMdiArea, it depends whether you want to handle multiple different charts each in its own widget.
<ul>
<li>Yes, I'd like to use the area to drop as many plots as I like. Each plot (I assume) is a widget.</li>
</ul>
</li>
<li>The item dropped from the QListView is of a QMimeData type?
<ul>
<li>Yes, it's a generic container for data transmission.</li>
</ul>
</li>
<li>The QListView needs to have the QWidget::setAcceptDrops property even if it will be used as "source" of the data to be dropped?
<ul>
<li>No</li>
</ul>
</li>
</ul>
<p dir="auto">I still have some questions on these:</p>
<ul>
<li>No need to do a custom view as you don't do anything special with them.
<ul>
<li>What do you mean for "custom view"? The way how I'd like to show dropped data?</li>
</ul>
</li>
<li>You will have to handle the drop part in a subclass of QMdiArea (if you continue with that class).
<ul>
<li>Could you please give me a hint on this?</li>
</ul>
</li>
</ul>
<p dir="auto">In the meantime, I'll start to have a look to these <em>mimeTypes</em> and <em>mimeData</em> methods to see how data is "shared" during the drag and drop actions. Anyway you clarified me already many points! 🙏</p>
<p dir="auto">Best regards,<br />
AGA</p>
]]></description><link>https://forum.qt.io/post/706424</link><guid isPermaLink="true">https://forum.qt.io/post/706424</guid><dc:creator><![CDATA[superaga]]></dc:creator><pubDate>Tue, 15 Mar 2022 09:10:14 GMT</pubDate></item><item><title><![CDATA[Reply to Drag and drop into QMdiArea on Mon, 14 Mar 2022 20:02:26 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">For QMdiArea, it depends whether you want to handle multiple different charts each in its own widget.</p>
<p dir="auto">It's a QMimeData object yes, it's a generic container for drag and drop data transmission.</p>
<p dir="auto">No, if your view is just a source, no need to accept drops.</p>
<p dir="auto">There's nothing mouse related to implement on your model. The mimeTypes and mimeData methods are the one you need for your custom data and the other is supportedDropActions if you want drop support as well. The flags method is required for fine grained control of which item shall be draggable.</p>
<p dir="auto">No need to do a custom view as you don't do anything special with them.</p>
<p dir="auto">You will have to handle the drop part in a subclass of QMdiArea (if you continue with that class).</p>
]]></description><link>https://forum.qt.io/post/706377</link><guid isPermaLink="true">https://forum.qt.io/post/706377</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 14 Mar 2022 20:02:26 GMT</pubDate></item></channel></rss>