Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3805240
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:38:47+00:00 2026-05-19T14:38:47+00:00

I am trying to handle a drop event on a TreeWidget from itself by

  • 0

I am trying to handle a drop event on a TreeWidget from itself by overriding the dropEvent method. Ultimately, I need to get a handle to the TreeWidgetItem being dropped. So far, the only useful information I get from the event regarding the dropped item is a QByteArray that seems to contain text from the item being dropped, except that it’s poorly formatted with lots of spaces and a bunch of non-printable characters.

Any help would be greatly appreciated.

edit:

Here is the code, as asked, but I’m really not doing anything special, I’m literally just reading the only type of data contained in the mimeData of the drop event. It sounds as though I’m going to have to override the Drag event?? And add some type of identifier to allow me to get a handle back to the original QTreeWidget??

def dropEvent( self, event ):
    data = event.mimeData().data( 'application/x-qabstractitemmodeldatalist' )
    print data
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:38:48+00:00Added an answer on May 19, 2026 at 2:38 pm

    not quite sure I’m understanding the question correctly, but your mime data comes from the startDrag method, where you’ve created a QMimeData object, set it’s type and supplied data accordingly. In your dropEvent method check the type of the incoming data and process it accordingly or ignore if you don’t recognize the type.

    Also take a look at the documentation here: Drag and Drop it should give you an idea on how drag and drop works in qt

    I also made a small example here, see if it would work for you:

    import sys
    from PyQt4 import QtGui, QtCore
    
    class TestTreeWidget(QtGui.QTreeWidget):
        def __init__(self, parent = None):
            super(TestTreeWidget, self).__init__(parent)
            self.setDragEnabled(True)
            self.setAcceptDrops(True)
    
        def startDrag(self, dropAction):
            # create mime data object
            mime = QtCore.QMimeData()
            mime.setData('application/x-item', '???')
            # start drag 
            drag = QtGui.QDrag(self)
            drag.setMimeData(mime)        
            drag.start(QtCore.Qt.CopyAction | QtCore.Qt.CopyAction)
    
        def dragMoveEvent(self, event):
            if event.mimeData().hasFormat("application/x-item"):
                event.setDropAction(QtCore.Qt.CopyAction)
                event.accept()
            else:
                event.ignore()
    
        def dragEnterEvent(self, event):
            if (event.mimeData().hasFormat('application/x-item')):
                event.accept()
            else:
                event.ignore()    
    
        def dropEvent(self, event): 
            if (event.mimeData().hasFormat('application/x-item')):
                event.acceptProposedAction()
                data = QtCore.QString(event.mimeData().data("application/x-item"))
                item = QtGui.QTreeWidgetItem(self)
                item.setText(0, data)
                self.addTopLevelItem(item)
            else:
                event.ignore()    
    
    class MainForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainForm, self).__init__(parent)
    
            self.view = TestTreeWidget(self)
            self.view.setColumnCount(1)
            item0 = QtGui.QTreeWidgetItem(self.view)
            item0.setText(0, 'item0')
            item1 = QtGui.QTreeWidgetItem(self.view)
            item1.setText(0, 'item1')
            self.view.addTopLevelItems([item0, item1])
    
            self.setCentralWidget(self.view)
    
    def main():
        app = QtGui.QApplication(sys.argv)
        form = MainForm()
        form.show()
        app.exec_()
    
    if __name__ == '__main__':
        main()
    

    also you may want to take a look at the similar post here: QTreeView with drag and drop support in PyQt

    hope this helps, regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to handle Winsock_Connect event (Actually I need it in Excel macro) using
I'm trying to get a handle on the amount of memory overhead associated with
I'm trying to handle this possible exploit and wondering what is the best way
I'm trying to handle the possibility that no arguments and no piped data is
We are trying to decide how to handle object persistence accross postbacks, to avoid
I'm trying to find the handle of the main thread of an external application.
I'm trying to create a use-once HTTP server to handle a single callback and
I'm currently trying to implement a class to handle secure communications between instances of
I am trying decide on how I want to handle the UI for an
Trying to add an onclick handler to my tabs, and can't seem to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.