Drop Site Example¶
Example shows how to distinguish the various MIME formats available in a drag and drop operation
droparea.cpp Example File¶
droparea.h Example File¶
dropsitewindow.cpp Example File¶
dropsitewindow.h Example File¶
main.cpp Example File¶
dropsite.pro Example File¶
The example shows how to distinguish the various MIME formats available in a drag and drop operation.
The Drop Site example accepts drops from other applications, and displays the MIME formats provided by the drag object.
There are two classes,
DropArea
andDropSiteWindow
, and amain()
function in this example. ADropArea
object is instantiated inDropSiteWindow
; aDropSiteWindow
object is then invoked in themain()
function.
DropArea Class Definition¶
The
DropArea
class is a subclass of QLabel with a public slot,clear()
, and achanged()
signal.class DropArea : public QLabel { Q_OBJECT public: explicit DropArea(QWidget *parent = nullptr); public slots: void clear(); signals: void changed(const QMimeData *mimeData = nullptr);In addition,
DropArea
also contains a private instance of QLabel and reimplementations of four QWidget event handlers:
dragEnterEvent()
dragMoveEvent()
dragLeaveEvent()
dropEvent()
These event handlers are further explained in the implementation of the
DropArea
class.protected: void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; void dragLeaveEvent(QDragLeaveEvent *event) override; void dropEvent(QDropEvent *event) override; private: QLabel *label; };
DropArea Class Implementation¶
In the
DropArea
constructor, we set the minimum size to 200x200 pixels, the frame style to both QFrame::Sunken and QFrame::StyledPanel, and we align its contents to the center.DropArea::DropArea(QWidget *parent) : QLabel(parent) { setMinimumSize(200, 200); setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); setAlignment(Qt::AlignCenter); setAcceptDrops(true); setAutoFillBackground(true); clear(); }Also, we enable drop events in
DropArea
by setting the acceptDrops property totrue
. Then, we enable the autoFillBackground property and invoke theclear()
function.The dragEnterEvent() event handler is called when a drag is in progress and the mouse enters the
DropArea
object. For theDropSite
example, when the mouse entersDropArea
, we set its text to “<drop content>” and highlight its background.<Code snippet "draganddrop/dropsite/droparea.cpp:dragEnterEvent() function" not found>Then, we invoke acceptProposedAction() on
event
, setting the drop action to the one proposed. Lastly, we emit thechanged()
signal, with the data that was dropped and its MIME type information as a parameter.For dragMoveEvent(), we just accept the proposed QDragMoveEvent object,
event
, with acceptProposedAction().<Code snippet "draganddrop/dropsite/droparea.cpp:dragMoveEvent() function" not found>The
DropArea
class’s implementation of dropEvent() extracts theevent
‘s mime data and displays it accordingly.<Code snippet "draganddrop/dropsite/droparea.cpp:dropEvent() function part1" not found>The
mimeData
object can contain one of the following objects: an image, HTML text, plain text, or a list of URLs.<Code snippet "draganddrop/dropsite/droparea.cpp:dropEvent() function part2" not found>
If
mimeData
contains an image, we display it inDropArea
with setPixmap().If
mimeData
contains HTML, we display it with setText() and setDropArea
’s text format asRichText
.If
mimeData
contains plain text, we display it with setText() and setDropArea
’s text format asPlainText
. In the event thatmimeData
contains URLs, we iterate through the list of URLs to display them on individual lines.If
mimeData
contains other types of objects, we setDropArea
’s text, with setText() to “Cannot display data” to inform the user.We then set
DropArea
‘s backgroundRole to QPalette::Dark and we acceptevent
‘s proposed action.<Code snippet "draganddrop/dropsite/droparea.cpp:dropEvent() function part3" not found>The dragLeaveEvent() event handler is called when a drag is in progress and the mouse leaves the widget.
<Code snippet "draganddrop/dropsite/droparea.cpp:dragLeaveEvent() function" not found>For
DropArea
‘s implementation, we clear invokeclear()
and then accept the proposed event.The
clear()
function sets the text inDropArea
to “<drop content>” and sets the backgroundRole to QPalette::Dark. Lastly, it emits thechanged()
signal.<Code snippet "draganddrop/dropsite/droparea.cpp:clear() function" not found>
DropSiteWindow Class Definition¶
The
DropSiteWindow
class contains a constructor and a public slot,updateFormatsTable()
.class DropSiteWindow : public QWidget { Q_OBJECT public: DropSiteWindow(); public slots: void updateFormatsTable(const QMimeData *mimeData); private: DropArea *dropArea; QLabel *abstractLabel; QTableWidget *formatsTable; QPushButton *clearButton; QPushButton *quitButton; QDialogButtonBox *buttonBox; };The class also contains a private instance of
DropArea
,dropArea
, QLabel,abstractLabel
, QTableWidget,formatsTable
, QDialogButtonBox,buttonBox
, and two QPushButton objects,clearButton
andquitButton
.
DropSiteWindow Class Implementation¶
In the constructor of
DropSiteWindow
, we instantiateabstractLabel
and set its wordWrap property totrue
. We also call the adjustSize() function to adjustabstractLabel
‘s size according to its contents.DropSiteWindow::DropSiteWindow() { abstractLabel = new QLabel(tr("This example accepts drags from other " "applications and displays the MIME types " "provided by the drag object.")); abstractLabel->setWordWrap(true); abstractLabel->adjustSize();Then we instantiate
dropArea
and connect itschanged()
signal toDropSiteWindow
‘supdateFormatsTable()
slot.dropArea = new DropArea; connect(dropArea, &DropArea::changed, this, &DropSiteWindow::updateFormatsTable);We now set up the QTableWidget object,
formatsTable
. Its horizontal header is set using a QStringList object,labels
. The number of columms are set to two and the table is not editable. Also, theformatTable
‘s horizontal header is formatted to ensure that its second column stretches to occupy additional space available.QStringList labels; labels << tr("Format") << tr("Content"); formatsTable = new QTableWidget; formatsTable->setColumnCount(2); formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); formatsTable->setHorizontalHeaderLabels(labels); formatsTable->horizontalHeader()->setStretchLastSection(true);Two QPushButton objects,
clearButton
andquitButton
, are instantiated and added tobuttonBox
- a QDialogButtonBox object. We use QDialogButtonBox here to ensure that the push buttons are presented in a layout that conforms to the platform’s style.clearButton = new QPushButton(tr("Clear")); quitButton = new QPushButton(tr("Quit")); buttonBox = new QDialogButtonBox; buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear);The clicked() signals for
quitButton
andclearButton
are connected to close() andclear()
, respectively.For the layout, we use a QVBoxLayout,
mainLayout
, to arrange our widgets vertically. We also set the window title to “Drop Site” and the minimum size to 350x500 pixels.QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(abstractLabel); mainLayout->addWidget(dropArea); mainLayout->addWidget(formatsTable); mainLayout->addWidget(buttonBox); setWindowTitle(tr("Drop Site")); setMinimumSize(350, 500); }We move on to the
updateFormatsTable()
function. This function updates theformatsTable
, displaying the MIME formats of the object dropped onto theDropArea
object. First, we set QTableWidget’s rowCount property to 0. Then, we validate to ensure that the QMimeData object passed in is a valid object.<Code snippet "draganddrop/dropsite/dropsitewindow.cpp:updateFormatsTable() part1" not found>Once we are sure that
mimeData
is valid, we iterate through its supported formats.Note
The formats() function returns a QStringList object, containing all the formats supported by the
mimeData
.<Code snippet "draganddrop/dropsite/dropsitewindow.cpp:updateFormatsTable() part2" not found>Within each iteration, we create a QTableWidgetItem,
formatItem
and we set its flags toItemIsEnabled
, and its text alignment toAlignTop
andAlignLeft
.A QString object,
text
, is customized to display data according to the contents offormat
. We invoke QString ‘s simplified() function ontext
, to obtain a string that has no additional space before, after or in between words.<Code snippet "draganddrop/dropsite/dropsitewindow.cpp:updateFormatsTable() part3" not found>If
format
contains a list of URLs, we iterate through them, using spaces to separate them. On the other hand, ifformat
contains an image, we display the data by converting the text to hexadecimal.<Code snippet "draganddrop/dropsite/dropsitewindow.cpp:updateFormatsTable() part4" not found>Once
text
has been customized to contain the appropriate data, we insert bothformat
andtext
intoformatsTable
with setItem(). Lastly, we invoke resizeColumnToContents() onformatsTable
‘s first column.
The main() Function¶
Within the
main()
function, we instantiateDropSiteWindow
and invoke its show() function.<Code snippet "draganddrop/dropsite/main.cpp:main() function" not found>