Results 1 to 16 of 16

Thread: QMimeData: setting and getting the right MIME types for arbitrary widgets

  1. #1
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default QMimeData: setting and getting the right MIME types for arbitrary widgets

    Using PySide/PyQt, I construct a draggable label that works exactly how I want:
    Qt Code:
    1. class DraggableLabel(QtGui.QLabel):
    2. def __init__(self, txt, parent):
    3. QtGui.QLabel.__init__(self, txt, parent)
    4. self.setStyleSheet("QLabel { background-color: rgb(255, 255, 0)}")
    5. def mouseMoveEvent(self, event):
    6. drag=QtGui.QDrag(self)
    7. dragLabMimeData=QtCore.QMimeData()
    8. drag.setMimeData(dragLabMimeData)
    9. drag.exec_(QtCore.Qt.MoveAction)
    To copy to clipboard, switch view to plain text mode 
    Unfortunately, I do not understand what is going on with QMimeData, and fear I am going to run into big problems when I use similar code in real-world examples.

    In particular, I am worried that my reimplementation of mouseMoveEvent creates an instance of QMimeData without any argument passed: QtCore.QMimeData(). Is this normal? Within more complex widgets will I be OK if I keep doing that within the relevant event handler: will the program automatically create the right type of MIME data for dragging and dropping?

    The reason I fear I am missing something is because at the Qt Drag and Drop documentation, it has lines of code like:
    Qt Code:
    1. mimeData -> setText(commentEdit->toPlainText());
    To copy to clipboard, switch view to plain text mode 
    which seems decidedly not like just letting the program take care of things within a reimplementation of an event handler.

    Also, the QMimeData documentation discusses convenience functions to test, get, and set data, but those are for standard data types (e.g., text, urls). I have found no clear way to define such convenience functions for widgets like my draggable QLabel. Am I missing it? Is there a simple way to find out if I am dragging around a widget of type X?

    Note I posted this also at Stack Overflow:
    http://stackoverflow.com/questions/2...itrary-widgets

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    What kind of data does the label hold that you want to drag to another widget or application?

    Your current code creates a drag without any content.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Just some arbitrary text on the QLabel. Here is the full code:
    Qt Code:
    1. # -*- coding: utf-8 -*-
    2. from PySide import QtGui, QtCore
    3.  
    4. class LabelDrag(QtGui.QWidget):
    5. def __init__(self):
    6. QtGui.QWidget.__init__(self)
    7. self.initUI()
    8. def initUI(self):
    9. self.lbl=DraggableLabel("Drag me", self)
    10. self.setAcceptDrops(True)
    11. self.setGeometry(40,50,200,200)
    12. self.show()
    13. def dragEnterEvent(self,event):
    14. event.accept()
    15. def dropEvent(self, event):
    16. self.lbl.move(event.pos()) #moves label to position once the movement finishes (dropped)
    17. event.accept()
    18.  
    19. class DraggableLabel(QtGui.QLabel):
    20. def __init__(self, txt, parent):
    21. QtGui.QLabel.__init__(self, txt, parent)
    22. self.setStyleSheet("QLabel { background-color: rgb(255, 255, 0)}")
    23. def mouseMoveEvent(self, event):
    24. drag=QtGui.QDrag(self)
    25. dragLabMimeData=QtCore.QMimeData()
    26. print dragLabMimeData.hasText()
    27. drag.setMimeData(dragLabMimeData)
    28. drag.exec_(QtCore.Qt.MoveAction)
    29.  
    30. def main():
    31. import sys
    32. qt_app=QtGui.QApplication(sys.argv)
    33. myMover=LabelDrag()
    34. sys.exit(qt_app.exec_())
    35.  
    36. if __name__=="__main__":
    37. main()
    To copy to clipboard, switch view to plain text mode 

    But note I have now done the same with much more complicated wigets (e.g., QGroupBox) and it seems to work (though it is not seamless: I am presently tinkering with it).
    Last edited by neuronet; 5th July 2014 at 13:42.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    You don't drag any data.

    I get the impression you want to move widgets around instead.

    Cheers,
    _

  5. #5
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Quote Originally Posted by anda_skoa View Post
    You don't drag any data.

    I get the impression you want to move widgets around instead.
    Yes, I am dragging widgets around: that is precisely my goal. Am i doing it wrong or am I generally confused? I thought there would be a custom mime type implemented dynamically, but I have no idea what's going on.

  6. #6
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Perhaps I should be using QGraphicsView?

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    If you want widgets that can be moved using the mouse, you can implement the movement directly in the mouse event handler methods.
    The Drag&Drop classes are for transferring data, often across different applications (e.g. from a file manager to a program that should open these files).

    In the most usual cases widget locations and sizes are managed by layouts, which position and resize child widgets within their parent's boundaries.
    But you can manage that yourself of course.

    I guess it depends on what you are trying to achieve, i.e. whether QGraphicsView is a better approach or not.

    Cheers,
    _

  8. #8
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Quote Originally Posted by anda_skoa View Post
    If you want widgets that can be moved using the mouse, you can implement the movement directly in the mouse event handler methods.
    The Drag&Drop classes are for transferring data, often across different applications (e.g. from a file manager to a program that should open these files).

    In the most usual cases widget locations and sizes are managed by layouts, which position and resize child widgets within their parent's boundaries.
    But you can manage that yourself of course.

    I guess it depends on what you are trying to achieve, i.e. whether QGraphicsView is a better approach or not.

    Cheers,
    _

    My goal is to make a desktop application in which I have multiple widgets open that the user can move around with drag/drop. Basically a to do list app (yes, I know, that is trite ). Something like the following markup:
    layout_example_toPost.jpg

    Given that goal, do you think QGraphicsView would be better? (Note someone has responded at Stack Overflow as well and I have yet to implement their suggestions). Or more accurately, do you think it would stupid to try to do it without using QGraphicsView?
    Last edited by neuronet; 6th July 2014 at 13:35. Reason: clarifying

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    So the only movable objects are the Todo items? Not things like the Save button or the Notes box?

    I would say it is bascially a trade-off. QGraphicsView would have item moving built-in, while Widgets would have things like the tree view built-in.

    If you have comfortable knowledge about widgets and no or only little know-how about graphics view I would go with widgets.

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    neuronet (6th July 2014)

  11. #10
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Quote Originally Posted by anda_skoa View Post
    So the only movable objects are the Todo items? Not things like the Save button or the Notes box?

    I would say it is bascially a trade-off. QGraphicsView would have item moving built-in, while Widgets would have things like the tree view built-in.

    If you have comfortable knowledge about widgets and no or only little know-how about graphics view I would go with widgets.

    Cheers,
    _
    Yes, just todo items can be moved around, not the command buttons. Based on your suggestion, I will stick with Widgets

  12. #11
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    While my simple example worked, it seems i do need to define a new MIME type for more complex examples. The following works, but I don't really understand it:
    Qt Code:
    1. itemData = QtCore.QByteArray()
    2. dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
    3. dataStream << QtCore.QPoint(event.pos() - self.rect().topLeft())
    4. mimeData = QtCore.QMimeData()
    5. mimeData.setData('x-QLabel', itemData)
    To copy to clipboard, switch view to plain text mode 
    It looks more like C than Python in places

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    I am confused.
    I thought you wanted to move the Todo objects, not drag some data?

    For a data drag this sounds about right, though the MIME type is usually in the form of "major/minor", e.g. "application/x-vnd.vendorname.vendorspecifictype"

    Cheers,
    _

  14. The following user says thank you to anda_skoa for this useful post:

    neuronet (7th July 2014)

  15. #13
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Quote Originally Posted by anda_skoa View Post
    I am confused.
    I thought you wanted to move the Todo objects, not drag some data?

    For a data drag this sounds about right, though the MIME type is usually in the form of "major/minor", e.g. "application/x-vnd.vendorname.vendorspecifictype"
    I want to drag widgets around, you are not confused. I was unaware of the naming conventions for newly created MIME types, it is something I should look into, am open to suggestions.

  16. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    If you want to move widgets around using the mouse, then you can ignore anything related to MIME type, QMimeData or QDrag.

    You just move the widget, see QWidget::move()
    Cheers,
    _

  17. The following user says thank you to anda_skoa for this useful post:

    neuronet (7th July 2014)

  18. #15
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Quote Originally Posted by anda_skoa View Post
    If you want to move widgets around using the mouse, then you can ignore anything related to MIME type, QMimeData or QDrag.

    You just move the widget, see QWidget::move()
    Cheers,
    _
    Wait really? I knew I could move it around programatically using move. But it is possible to enable user to move it around with the mouse too? That sounds pretty awesome!!! Is this a good example of what youa re talking about?
    http://stackoverflow.com/questions/1...-in-the-screen

    If so, then wow I really do belong in the noob forum.

    If anyone has a full worked out example of moving a widget around (without all this mime nonsense) that would be very helpful...The example I linked to is really a snippet, and in C++. Anything in pyside? If not c++ is fine.

    Edit: it seems this is a point of confusion among many noobs, and luckily a pyqt example (which is basically pyside):
    http://stackoverflow.com/questions/1...button-in-pyqt

    This is actually really important. Noobs like me take the phrase 'drag and drop' in a colloquial sense to mean 'move something with the mouse', while in *qt, these two things are not the same at all. At this point I'm surprised the doc for drag 'n' drop doesn't have a huge yellow sign before starting, saying "If you want to just move something with your mouse, this is not what you want! Drag and drop is much more than that!"

    I have seen so much confusion on the web about this, and generated it myself. Thanks for helping to point me int he right direction. I really wish there was a good book on this for Python users! Summerfield is good, but dated, and I am using that.
    Last edited by neuronet; 7th July 2014 at 21:40.

  19. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData: setting and getting the right MIME types for arbitrary widgets

    Yes, like that.

    Mouse down initializes a base line position, mouse move then calculates relative movement, moves the widget and resets the base line position for the next calculation,

    Cheers,
    _

  20. The following user says thank you to anda_skoa for this useful post:

    neuronet (7th July 2014)

Similar Threads

  1. Setting width of widgets from QSplitter?
    By adutzu89 in forum Newbie
    Replies: 4
    Last Post: 24th May 2014, 13:07
  2. Replies: 3
    Last Post: 11th August 2011, 17:16
  3. Replies: 4
    Last Post: 10th December 2009, 16:37
  4. Arbitrary widgets placement in window.
    By someralex in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2006, 12:14
  5. MIME database and QMimedata
    By nupul in forum Qt Programming
    Replies: 12
    Last Post: 12th April 2006, 14:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.