Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Qt drag frames?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "what are the Qt drag and drop frameworks". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the Qt drag frameworks?"

Demo Picture drag and drop

  

Control drag and drop

  

Window drag and drag

  

Dragging framework (advanced development)

     

Drag and drop (Drag and Drop)

   drag and drop provides a simple visual mechanism that users can use to transfer information between and within applications. The function of drag and drop is similar to the cut and paste mechanism of the clipboard.    this document describes the basic drag-and-drop mechanism and outlines how to enable it in custom controls. Many qt controls also support drag-and-drop operations, such as project views and graphical view frames, as well as editing controls for qt widgets and qt quick. For more information about project views and drawing views, see drag and drop using Project views and drawing View frames.

Drag and drop class

   these classes handle drag-and-drop and the necessary mime type encoding and decoding.   

Configuration

The    QStyleHints object provides some properties related to drag-and-drop operations:

QStyleHints::startDragTime (): describes the amount of time (in milliseconds) that the user must hold down the mouse button on the object before starting dragging.

QStyleHints::StartDragDistance (): indicates the distance the user must move the mouse button while holding down the mouse button before moving is interpreted as dragging.

QStyleHints::StartDragVelocity (): indicates the speed at which the user moves the mouse to start dragging (in pixels per second). A value of 0 means that there is no such limit. If    provides drag-and-drop support in the control, these quantities will provide reasonable default values that are compatible with the underlying window system for your use.

Drag and drop in Qt Quick species

The rest of the    document focuses on how to implement drag and drop in C++. To use drag-and-drop in Qt Quick scenes, read the documentation for Qt Quick drag-and-drop, DragEvent, and DropArea items, and the Qt Quick drag-and-drop example.

Drag (Dragging)

To start dragging   , create a QDrag object and call its exec () function. In most applications, drag-and-drop operations can only begin after pressing the mouse button and moving the cursor a certain distance. However, the easiest way to enable widget dragging is to reimplement the widget's mousePressEvent () and start the drag-and-drop operation: although       may take some time for the user to complete the drag operation, as far as the application is concerned, the exec () function is a blocking function with one of several values. These describe how the operation ends, which is described in more detail below.    note that the exec () function does not block the main event loop. For widgets that need to distinguish between mouse clicks and drags,    is useful to reimplement the mousePressEvent () function of the widget to record the start position of the drag:   

   later, in mouseMoveEvent (), we can determine whether we should start dragging and construct a drag object to handle the operation:   

   this special method uses the QPoint::manhattanlength () function to roughly estimate the distance between the mouse click position and the current cursor position. This function trades precision for speed and is usually suitable for this purpose.

Put down (Dropping)

To be able to receive discarded media on the widget,    calls setAcceptDrops (true) for the widget and reimplements the dragEnterEvent () and dropEvent () event handler functions. For example, the following code enables the Drop event in the constructor of the QWidget subclass to effectively implement the Drop event handler:    dragEnterEvent () is typically used to notify the qt widget of the accepted data type. If you want to receive QDragMoveEvent or QDropEvent in the reimplementation of DragMoveEvent () and dropEvent (), you must reimplement this function. The following code for error    shows how to reimplement DragEnterEvent () to tell the drag-and-drop system that we can only handle plain text:   

   dropEvent () is used to unpack discarded data and process it in a way that suits your application.    in the following code, the text provided in the event is passed to QTextBrowser,QComboBox to populate the list of mime types used to describe the data:   

   in this case, we accept the recommended action without checking what it is. In a real-world application, you might need to return from the dropEvent () function without accepting the recommended action, or to process the data if the operation is irrelevant. For example, if we do not support links to external sources in the application, we can choose to ignore the Qt::LinkAction operation.

Cover the proposed action

   can also ignore proposed actions and perform other operations on the data. To do this, we will invoke the setDropAction () of the event object using the preferred action in Qt::dropAction before calling accept (). This ensures that the replacement delete operation is used instead of the recommended action. For more complex applications, reimplementing dragMoveEvent () and dragLeaveEvent () will make parts of the widget sensitive to placement events and give you better control over drag and drop in the application.

Subclassing of complex widgets

Some of    's standard Qt widgets provide their own support for drag and drop. When subclassing these widgets, in addition to DragCenterEvent () and DropEvent (), you may need to reimplement DragMoveEvent () to prevent the base class from providing default drag-and-drop handling and to handle any special cases that interest you.

Drag and drop operation

   in the simplest case, the target of the drag-and-drop operation will receive a copy of the data being dragged, and the source will decide whether to delete the original data. This is described by the CopyAction operation. The target can also choose to handle other operations, especially MoveAction and LinkAction operations. If the source calls QDrag::exec () and returns MoveAction, then if the source chooses to delete any raw data, the source will be responsible for the deletion. QMimeData and QDrag objects created by the source widget should not be deleted-they will be destroyed by Qt. The goal is responsible for taking ownership of the data sent during the drag-and-drop operation; this is usually achieved by retaining a reference to the data.    if the target understands the LinkAction operation, it should store its own reference to the original information; the source does not need to perform any further processing on the data. The most common use of drag-and-drop operations is to perform movements in the same widget; for more information about this feature, see the section on drag-and-drop operations. Another major use of    drag operations is when using reference types such as text/uri-list, where the data dragged is actually a reference to a file or object.

Add a new drag-and-drop type

   drag and drop is not limited to text and images. Any type of information can be transmitted in a drag-and-drop operation. To drag information between applications, applications must be able to indicate to each other which data formats are acceptable and which data formats can be generated, by using the mime type. The QDrag object constructed from the source contains a list of mime types used to represent the data (in the most appropriate to the most inappropriate order), one of which is used by the drop target to access the data. For common data types, convenience functions deal with mime types that are used transparently, but for custom data types, they must be explicitly declared. The first and most important step for    to drag and drop information types not covered by the QDrag convenience is to find the appropriate existing format: the Internet assigned numbers Authority (IANA) provides a hierarchical list of MIME media types at the Institute of Information Science (ISI). Using standard mime types can maximize interoperability between applications and other software now and in the future. To support other media types, simply use the setData () function to set the data in the QMimeData object, providing the complete mime type and QByteArray that contains the data in the appropriate format. The following code takes the QPixmap from the tag and stores it as a portable network graphics (PNG) file in the QMimeData object:   

   for this case, we can simply use setImageData () to provide image data in various formats:   

   in this case, the QByteArray method is still useful because it has better control over the amount of data stored in the QMimeData object. Note that the custom data types used in the item view must be declared as meta-objects and must implement their flow operators.

Put down the action.

   in the clipboard model, users can cut or copy the source information, and then paste it. Similarly, in the drag-and-drop model, you can drag a copy of the information or drag the information itself to a new location (move the information). The drag-and-drop model has an additional complexity for programmers: the program does not know whether the user wants to cut or copy information until the operation is complete. This usually makes no difference when dragging information between applications, but in an application, it is important to check which placement operation is used.    can reimplement mouseMoveEvent () for a widget and initiate a drag-and-drop operation through a possible combination of drag-and-drop operations. For example, you might want to make sure that dragging always moves objects in the widget:   

   if the information is put into another application, the operation returned by the exec () function may default to copyAction, but if the information is put into another widget in the same application, we may get a different drop operation. You can filter the recommended placement actions in the dragMoveEvent () function of the widget. However, you can accept all the recommended actions in DragEnterEvent () and let the user decide which action to accept later:   

   when placement occurs in the widget, the DropEvent () handler function is called, and we can handle each possible operation in turn. First, we handle the drag and drop operation in the same widget:   

In this case,    refuses to handle the move operation. Each type of drop action accepted will be checked and handled accordingly:   

   notice that the separate placement operation is checked in the above code. As mentioned above, in the override recommended actions section, it is sometimes necessary to override the recommended delete operations and select different actions from the possible delete operations. To do this, you need to check whether each action exists in the value provided by possibleActions () of the event, set the Drop operation with setDropAction (), and call accept ().

Rectangular fall

The dragMoveEvent () of the    widget can be used to restrict the placement of certain parts of the widget by accepting the recommended placement operation only when the cursor is within these areas. For example, when the cursor is over a child widget (DropFrame), the following code accepts any recommended placement:   

   you can also use DragMoveEvent () if you need to provide visual feedback, scroll windows, or any appropriate action during a drag-and-drop operation.

Shearing board

   applications can also communicate by placing data on the clipboard. To access this, you need to get a QClipboard object from the QApplication object. The    QMimedata class is used to represent the data transferred in the clipboard. To put data on the clipboard, you can use the setText (), setImage (), and setPixmap () convenience functions to handle common data types. These functions are similar to those found in the QMimedata class, except that they have an additional parameter that controls where the data is stored: if the clipboard is specified, the data is placed on the clipboard; if selection is specified, the data is placed in the mouse selection (on x11 only). By default, the data is placed on the clipboard. For example, we can use the following code to copy the contents of QLineEdit to the clipboard:

   data with different mime types can also be placed on the clipboard. Construct a qmimedata object and use the setData () function to set up the data as described in the previous section; you can then use the setmimedata () function to put the object on the clipboard. The QClipboard class can notify the application of changes to the data it contains through its dataChanged () signal. For example, we can monitor the clipboard by connecting this signal to a slot in the widget:   

The slot where the    is connected to this signal can read the data on the clipboard using one of the MIME types that can be used to represent the signal: the       selectionChanged () signal can be used for x11 to monitor mouse selection.

Interoperate with other applications

   uses the public XDND protocol on x11 and the OLE standard on Windows Qt, while Qt for MacOS uses the Cocoa drag manager. On x11, XDND uses MIME, so no translation is required. QT API is the same regardless of the platform. On Windows, applications that support MIME can communicate using clipboard format names of type MIME. Some Windows applications have used the MIME naming convention for their clipboard format. Custom classes used by    to convert dedicated clipboard formats can be registered by re-implementing QwinMime on Windows or re-implementing QMacPasteboardMime on MacOS.

At this point, I believe you have a deeper understanding of "what is the Qt drag framework?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report