If the source calls QDrag::exec(), and it returns MoveAction, the source is responsible for deleting any original data if it chooses to do so. The target may also choose to handle other actions, specifically the MoveAction and LinkAction actions. This is described by the CopyAction action. In the simplest case, the target of a drag and drop action receives a copy of the data being dragged, and the source decides whether to delete the original. When subclassing these widgets, it may be necessary to reimplement dragMoveEvent() in addition to dragEnterEvent() and dropEvent() to prevent the base class from providing default drag and drop handling, and to handle any special cases you are interested in. Subclassing Complex WidgetsĬertain standard Qt widgets provide their own support for drag and drop. This ensures that the replacement drop action is used instead of the proposed action.įor more sophisticated applications, reimplementing dragMoveEvent() and dragLeaveEvent() will let you make certain parts of your widgets sensitive to drop events, and give you more control over drag and drop in your application. To do this, we would call the event object's setDropAction() with the preferred action from Qt::DropAction before calling accept(). We may also ignore the proposed action, and perform some other action on the data. For example, we may choose to ignore Qt::LinkAction actions if we do not support links to external sources in our application. In a real world application, it may be necessary to return from the dropEvent() function without accepting the proposed action or handling the data if the action is not relevant. In this case, we accept the proposed action without checking what it is. MimeTypeCombo - >addItems(event - >mimeData() - >formats()) TextBrowser - >setPlainText(event - >mimeData() - >text()) Void Window ::dropEvent( QDropEvent *event) To be able to receive media dropped on a widget, call setAcceptDrops(true) for the widget, and reimplement the dragEnterEvent() and dropEvent() event handler functions.įor example, the following code enables drop events in the constructor of a QWidget subclass, making it possible to usefully implement drop event handlers: This function trades accuracy for speed, and is usually suitable for this purpose. This particular approach uses the QPoint::manhattanLength() function to get a rough estimate of the distance between where the mouse click occurred and the current cursor position. Qt ::DropAction dropAction = drag - >exec( Qt ::CopyAction | Qt ::MoveAction) If ((event - >pos() - dragStartPosition). If ( !(event - >buttons() & Qt ::LeftButton)) Void DragWidget ::mouseMoveEvent( QMouseEvent *event) However, the simplest way to enable dragging from a widget is to reimplement the widget's mousePressEvent() and start a drag and drop operation: In most applications, it is a good idea to begin a drag and drop operation only after a mouse button has been pressed and the cursor has been moved a certain distance. To start a drag, create a QDrag object, and call its exec() function. For using drag and drop inside a Qt Quick scene, please read the documentation for the Qt Quick Drag, DragEvent, and DropArea items, as well as the Qt Quick Drag and Drop examples. The rest of the document focuses mainly on how to implement drag and drop in C++. These quantities provide sensible default values that are compliant with the underlying windowing system for you to use if you provide drag and drop support in your controls. A value of 0 means that there is no such limit. QStyleHints::startDragVelocity() indicates how fast (in pixels/second) the user has to move the mouse to start a drag.QStyleHints::startDragDistance() indicates how far the user has to move the mouse while holding down a mouse button before the movement will be interpreted as dragging.QStyleHints::startDragTime() describes the amount of time in milliseconds that the user must hold down a mouse button over an object before a drag will begin.The QStyleHints object provides some properties that are related to drag and drop operations: Support for MIME-based drag and drop data transferĮvent which is sent to a widget when a drag and drop action enters itĮvent that is sent to a widget when a drag and drop action leaves itĮvent which is sent while a drag and drop action is in progressĮvent which is sent when a drag and drop action is completedĬonverts between a MIME type and a Uniform Type Identifier (UTI) format
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |