<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- dropsite.qdoc --> <title>Drop Site Example | Qt Widgets 5.12.2</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td >Drop Site Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.2 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#droparea-class-definition">DropArea Class Definition</a></li> <li class="level1"><a href="#droparea-class-implementation">DropArea Class Implementation</a></li> <li class="level1"><a href="#dropsitewindow-class-definition">DropSiteWindow Class Definition</a></li> <li class="level1"><a href="#dropsitewindow-class-implementation">DropSiteWindow Class Implementation</a></li> <li class="level1"><a href="#the-main-function">The main() Function</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Drop Site Example</h1> <span class="subtitle"></span> <!-- $$$draganddrop/dropsite-brief --> <p>The example shows how to distinguish the various MIME formats available in a drag and drop operation.</p> <!-- @@@draganddrop/dropsite --> <!-- $$$draganddrop/dropsite-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/dropsite-example.png" alt="Screenshot of the Drop Site example" /></p><p>The Drop Site example accepts drops from other applications, and displays the MIME formats provided by the drag object.</p> <p>There are two classes, <code>DropArea</code> and <code>DropSiteWindow</code>, and a <code>main()</code> function in this example. A <code>DropArea</code> object is instantiated in <code>DropSiteWindow</code>; a <code>DropSiteWindow</code> object is then invoked in the <code>main()</code> function.</p> <a name="droparea-class-definition"></a> <h2 id="droparea-class-definition">DropArea Class Definition</h2> <p>The <code>DropArea</code> class is a subclass of <a href="qlabel.html">QLabel</a> with a public slot, <code>clear()</code>, and a <code>changed()</code> signal.</p> <pre class="cpp"> <span class="keyword">class</span> DropArea : <span class="keyword">public</span> <span class="type"><a href="qlabel.html">QLabel</a></span> { Q_OBJECT <span class="keyword">public</span>: <span class="keyword">explicit</span> DropArea(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> clear(); <span class="keyword">signals</span>: <span class="type">void</span> changed(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmimedata.html">QMimeData</a></span> <span class="operator">*</span>mimeData <span class="operator">=</span> nullptr); </pre> <p>In addition, <code>DropArea</code> also contains a private instance of <a href="qlabel.html">QLabel</a> and reimplementations of four <a href="qwidget.html">QWidget</a> event handlers:</p> <ol class="1" type="1"><li><a href="qwidget.html#dragEnterEvent">dragEnterEvent()</a></li> <li><a href="qwidget.html#dragMoveEvent">dragMoveEvent()</a></li> <li><a href="qwidget.html#dragLeaveEvent">dragLeaveEvent()</a></li> <li><a href="qwidget.html#dropEvent">dropEvent()</a></li> </ol> <p>These event handlers are further explained in the implementation of the <code>DropArea</code> class.</p> <pre class="cpp"> <span class="keyword">protected</span>: <span class="type">void</span> dragEnterEvent(<span class="type"><a href="../qtgui/qdragenterevent.html">QDragEnterEvent</a></span> <span class="operator">*</span>event) override; <span class="type">void</span> dragMoveEvent(<span class="type"><a href="../qtgui/qdragmoveevent.html">QDragMoveEvent</a></span> <span class="operator">*</span>event) override; <span class="type">void</span> dragLeaveEvent(<span class="type"><a href="../qtgui/qdragleaveevent.html">QDragLeaveEvent</a></span> <span class="operator">*</span>event) override; <span class="type">void</span> dropEvent(<span class="type"><a href="../qtgui/qdropevent.html">QDropEvent</a></span> <span class="operator">*</span>event) override; <span class="keyword">private</span>: <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label; }; </pre> <a name="droparea-class-implementation"></a> <h2 id="droparea-class-implementation">DropArea Class Implementation</h2> <p>In the <code>DropArea</code> constructor, we set the <a href="qwidget.html#minimumSize-prop">minimum size</a> to 200x200 pixels, the <a href="qframe.html#setFrameStyle">frame style</a> to both <a href="qframe.html#Shadow-enum">QFrame::Sunken</a> and <a href="qframe.html#Shape-enum">QFrame::StyledPanel</a>, and we align its contents to the center.</p> <pre class="cpp"> DropArea<span class="operator">::</span>DropArea(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qlabel.html">QLabel</a></span>(parent) { setMinimumSize(<span class="number">200</span><span class="operator">,</span> <span class="number">200</span>); setFrameStyle(<span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>Sunken <span class="operator">|</span> <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>StyledPanel); setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignCenter); setAcceptDrops(<span class="keyword">true</span>); setAutoFillBackground(<span class="keyword">true</span>); clear(); } </pre> <p>Also, we enable drop events in <code>DropArea</code> by setting the <a href="qwidget.html#acceptDrops-prop">acceptDrops</a> property to <code>true</code>. Then, we enable the <a href="qwidget.html#autoFillBackground-prop">autoFillBackground</a> property and invoke the <code>clear()</code> function.</p> <p>The <a href="qwidget.html#dragEnterEvent">dragEnterEvent()</a> event handler is called when a drag is in progress and the mouse enters the <code>DropArea</code> object. For the <code>DropSite</code> example, when the mouse enters <code>DropArea</code>, we set its text to "<drop content>" and highlight its background.</p> <pre class="cpp"> <span class="type">void</span> DropArea<span class="operator">::</span>dragEnterEvent(<span class="type"><a href="../qtgui/qdragenterevent.html">QDragEnterEvent</a></span> <span class="operator">*</span>event) { setText(tr(<span class="string">"<drop content>"</span>)); setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Highlight); event<span class="operator">-</span><span class="operator">></span>acceptProposedAction(); <span class="keyword">emit</span> changed(event<span class="operator">-</span><span class="operator">></span>mimeData()); } </pre> <p>Then, we invoke <a href="../qtgui/qdropevent.html#acceptProposedAction">acceptProposedAction()</a> on <i>event</i>, setting the drop action to the one proposed. Lastly, we emit the <code>changed()</code> signal, with the data that was dropped and its MIME type information as a parameter.</p> <p>For <a href="qwidget.html#dragMoveEvent">dragMoveEvent()</a>, we just accept the proposed <a href="../qtgui/qdragmoveevent.html">QDragMoveEvent</a> object, <i>event</i>, with <a href="../qtgui/qdropevent.html#acceptProposedAction">acceptProposedAction()</a>.</p> <pre class="cpp"> <span class="type">void</span> DropArea<span class="operator">::</span>dragMoveEvent(<span class="type"><a href="../qtgui/qdragmoveevent.html">QDragMoveEvent</a></span> <span class="operator">*</span>event) { event<span class="operator">-</span><span class="operator">></span>acceptProposedAction(); } </pre> <p>The <code>DropArea</code> class's implementation of <a href="qwidget.html#dropEvent">dropEvent()</a> extracts the <i>event</i>'s mime data and displays it accordingly.</p> <pre class="cpp"> <span class="type">void</span> DropArea<span class="operator">::</span>dropEvent(<span class="type"><a href="../qtgui/qdropevent.html">QDropEvent</a></span> <span class="operator">*</span>event) { <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmimedata.html">QMimeData</a></span> <span class="operator">*</span>mimeData <span class="operator">=</span> event<span class="operator">-</span><span class="operator">></span>mimeData(); </pre> <p>The <code>mimeData</code> object can contain one of the following objects: an image, HTML text, plain text, or a list of URLs.</p> <pre class="cpp"> <span class="keyword">if</span> (mimeData<span class="operator">-</span><span class="operator">></span>hasImage()) { setPixmap(qvariant_cast<span class="operator"><</span><span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span><span class="operator">></span>(mimeData<span class="operator">-</span><span class="operator">></span>imageData())); } <span class="keyword">else</span> <span class="keyword">if</span> (mimeData<span class="operator">-</span><span class="operator">></span>hasHtml()) { setText(mimeData<span class="operator">-</span><span class="operator">></span>html()); setTextFormat(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RichText); } <span class="keyword">else</span> <span class="keyword">if</span> (mimeData<span class="operator">-</span><span class="operator">></span>hasText()) { setText(mimeData<span class="operator">-</span><span class="operator">></span>text()); setTextFormat(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>PlainText); } <span class="keyword">else</span> <span class="keyword">if</span> (mimeData<span class="operator">-</span><span class="operator">></span>hasUrls()) { <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">></span> urlList <span class="operator">=</span> mimeData<span class="operator">-</span><span class="operator">></span>urls(); <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text; <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> urlList<span class="operator">.</span>size() <span class="operator">&</span><span class="operator">&</span> i <span class="operator"><</span> <span class="number">32</span>; <span class="operator">+</span><span class="operator">+</span>i) text <span class="operator">+</span><span class="operator">=</span> urlList<span class="operator">.</span>at(i)<span class="operator">.</span>path() <span class="operator">+</span> QLatin1Char(<span class="char">'\n'</span>); setText(text); } <span class="keyword">else</span> { setText(tr(<span class="string">"Cannot display data"</span>)); } </pre> <ul> <li>If <code>mimeData</code> contains an image, we display it in <code>DropArea</code> with <a href="qlabel.html#pixmap-prop">setPixmap()</a>.</li> <li>If <code>mimeData</code> contains HTML, we display it with <a href="qlabel.html#text-prop">setText()</a> and set <code>DropArea</code>'s text format as <a href="../qtcore/qt.html#TextFormat-enum">Qt::RichText</a>.</li> <li>If <code>mimeData</code> contains plain text, we display it with <a href="qlabel.html#text-prop">setText()</a> and set <code>DropArea</code>'s text format as <a href="../qtcore/qt.html#TextFormat-enum">Qt::PlainText</a>. In the event that <code>mimeData</code> contains URLs, we iterate through the list of URLs to display them on individual lines.</li> <li>If <code>mimeData</code> contains other types of objects, we set <code>DropArea</code>'s text, with <a href="qlabel.html#text-prop">setText()</a> to "Cannot display data" to inform the user.</li> </ul> <p>We then set <code>DropArea</code>'s <a href="qwidget.html#backgroundRole">backgroundRole</a> to <a href="../qtgui/qpalette.html#ColorRole-enum">QPalette::Dark</a> and we accept <code>event</code>'s proposed action.</p> <pre class="cpp"> setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Dark); event<span class="operator">-</span><span class="operator">></span>acceptProposedAction(); } </pre> <p>The <a href="qwidget.html#dragLeaveEvent">dragLeaveEvent()</a> event handler is called when a drag is in progress and the mouse leaves the widget.</p> <pre class="cpp"> <span class="type">void</span> DropArea<span class="operator">::</span>dragLeaveEvent(<span class="type"><a href="../qtgui/qdragleaveevent.html">QDragLeaveEvent</a></span> <span class="operator">*</span>event) { clear(); event<span class="operator">-</span><span class="operator">></span>accept(); } </pre> <p>For <code>DropArea</code>'s implementation, we clear invoke <code>clear()</code> and then accept the proposed event.</p> <p>The <code>clear()</code> function sets the text in <code>DropArea</code> to "<drop content>" and sets the <a href="qwidget.html#backgroundRole">backgroundRole</a> to <a href="../qtgui/qpalette.html#ColorRole-enum">QPalette::Dark</a>. Lastly, it emits the <code>changed()</code> signal.</p> <pre class="cpp"> <span class="type">void</span> DropArea<span class="operator">::</span>clear() { setText(tr(<span class="string">"<drop content>"</span>)); setBackgroundRole(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Dark); <span class="keyword">emit</span> changed(); } </pre> <a name="dropsitewindow-class-definition"></a> <h2 id="dropsitewindow-class-definition">DropSiteWindow Class Definition</h2> <p>The <code>DropSiteWindow</code> class contains a constructor and a public slot, <code>updateFormatsTable()</code>.</p> <pre class="cpp"> <span class="keyword">class</span> DropSiteWindow : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: DropSiteWindow(); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> updateFormatsTable(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmimedata.html">QMimeData</a></span> <span class="operator">*</span>mimeData); <span class="keyword">private</span>: DropArea <span class="operator">*</span>dropArea; <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>abstractLabel; <span class="type"><a href="qtablewidget.html">QTableWidget</a></span> <span class="operator">*</span>formatsTable; <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>clearButton; <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>quitButton; <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span> <span class="operator">*</span>buttonBox; }; </pre> <p>The class also contains a private instance of <code>DropArea</code>, <code>dropArea</code>, <a href="qlabel.html">QLabel</a>, <code>abstractLabel</code>, <a href="qtablewidget.html">QTableWidget</a>, <code>formatsTable</code>, <a href="qdialogbuttonbox.html">QDialogButtonBox</a>, <code>buttonBox</code>, and two <a href="qpushbutton.html">QPushButton</a> objects, <code>clearButton</code> and <code>quitButton</code>.</p> <a name="dropsitewindow-class-implementation"></a> <h2 id="dropsitewindow-class-implementation">DropSiteWindow Class Implementation</h2> <p>In the constructor of <code>DropSiteWindow</code>, we instantiate <code>abstractLabel</code> and set its <a href="qlabel.html#wordWrap-prop">wordWrap</a> property to <code>true</code>. We also call the <a href="qwidget.html#adjustSize">adjustSize()</a> function to adjust <code>abstractLabel</code>'s size according to its contents.</p> <pre class="cpp"> DropSiteWindow<span class="operator">::</span>DropSiteWindow() { abstractLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">"This example accepts drags from other "</span> <span class="string">"applications and displays the MIME types "</span> <span class="string">"provided by the drag object."</span>)); abstractLabel<span class="operator">-</span><span class="operator">></span>setWordWrap(<span class="keyword">true</span>); abstractLabel<span class="operator">-</span><span class="operator">></span>adjustSize(); </pre> <p>Then we instantiate <code>dropArea</code> and connect its <code>changed()</code> signal to <code>DropSiteWindow</code>'s <code>updateFormatsTable()</code> slot.</p> <pre class="cpp"> dropArea <span class="operator">=</span> <span class="keyword">new</span> DropArea; connect(dropArea<span class="operator">,</span> <span class="operator">&</span>DropArea<span class="operator">::</span>changed<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>DropSiteWindow<span class="operator">::</span>updateFormatsTable); </pre> <p>We now set up the <a href="qtablewidget.html">QTableWidget</a> object, <code>formatsTable</code>. Its horizontal header is set using a <a href="../qtcore/qstringlist.html">QStringList</a> object, <code>labels</code>. The number of columms are set to two and the table is not editable. Also, the <code>formatTable</code>'s horizontal header is formatted to ensure that its second column stretches to occupy additional space available.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> labels; labels <span class="operator"><</span><span class="operator"><</span> tr(<span class="string">"Format"</span>) <span class="operator"><</span><span class="operator"><</span> tr(<span class="string">"Content"</span>); formatsTable <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidget.html">QTableWidget</a></span>; formatsTable<span class="operator">-</span><span class="operator">></span>setColumnCount(<span class="number">2</span>); formatsTable<span class="operator">-</span><span class="operator">></span>setEditTriggers(<span class="type"><a href="qabstractitemview.html">QAbstractItemView</a></span><span class="operator">::</span>NoEditTriggers); formatsTable<span class="operator">-</span><span class="operator">></span>setHorizontalHeaderLabels(labels); formatsTable<span class="operator">-</span><span class="operator">></span>horizontalHeader()<span class="operator">-</span><span class="operator">></span>setStretchLastSection(<span class="keyword">true</span>); </pre> <p>Two <a href="qpushbutton.html">QPushButton</a> objects, <code>clearButton</code> and <code>quitButton</code>, are instantiated and added to <code>buttonBox</code> - a <a href="qdialogbuttonbox.html">QDialogButtonBox</a> object. We use <a href="qdialogbuttonbox.html">QDialogButtonBox</a> here to ensure that the push buttons are presented in a layout that conforms to the platform's style.</p> <pre class="cpp"> clearButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"Clear"</span>)); quitButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"Quit"</span>)); buttonBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span>; buttonBox<span class="operator">-</span><span class="operator">></span>addButton(clearButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>ActionRole); buttonBox<span class="operator">-</span><span class="operator">></span>addButton(quitButton<span class="operator">,</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>RejectRole); connect(quitButton<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close); connect(clearButton<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span> dropArea<span class="operator">,</span> <span class="operator">&</span>DropArea<span class="operator">::</span>clear); </pre> <p>The <a href="qabstractbutton.html#clicked">clicked()</a> signals for <code>quitButton</code> and <code>clearButton</code> are connected to <a href="qwidget.html#close">close()</a> and <code>clear()</code>, respectively.</p> <p>For the layout, we use a <a href="qvboxlayout.html">QVBoxLayout</a>, <code>mainLayout</code>, to arrange our widgets vertically. We also set the window title to "Drop Site" and the minimum size to 350x500 pixels.</p> <pre class="cpp"> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(abstractLabel); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(dropArea); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(formatsTable); mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(buttonBox); setWindowTitle(tr(<span class="string">"Drop Site"</span>)); setMinimumSize(<span class="number">350</span><span class="operator">,</span> <span class="number">500</span>); } </pre> <p>We move on to the <code>updateFormatsTable()</code> function. This function updates the <code>formatsTable</code>, displaying the MIME formats of the object dropped onto the <code>DropArea</code> object. First, we set <a href="qtablewidget.html">QTableWidget</a>'s <a href="qtablewidget.html#setRowCount">rowCount</a> property to 0. Then, we validate to ensure that the <a href="../qtcore/qmimedata.html">QMimeData</a> object passed in is a valid object.</p> <pre class="cpp"> <span class="type">void</span> DropSiteWindow<span class="operator">::</span>updateFormatsTable(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qmimedata.html">QMimeData</a></span> <span class="operator">*</span>mimeData) { formatsTable<span class="operator">-</span><span class="operator">></span>setRowCount(<span class="number">0</span>); <span class="keyword">if</span> (<span class="operator">!</span>mimeData) <span class="keyword">return</span>; </pre> <p>Once we are sure that <code>mimeData</code> is valid, we iterate through its supported formats using the <a href="../qtcore/containers.html#the-foreach-keyword">foreach keyword</a>. This keyword has the following format:</p> <pre class="cpp"> foreach(variable<span class="operator">,</span> container) </pre> <p>In our example, <code>format</code> is the <i>variable</i> and the <i>container</i> is a <a href="../qtcore/qstringlist.html">QStringList</a>, obtained from <code>mimeData->formats()</code>.</p> <p><b>Note: </b>The <a href="../qtcore/qmimedata.html#formats">formats()</a> function returns a <a href="../qtcore/qstringlist.html">QStringList</a> object, containing all the formats supported by the <code>mimeData</code>.</p><pre class="cpp"> <span class="keyword">for</span> (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>format : mimeData<span class="operator">-</span><span class="operator">></span>formats()) { <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span> <span class="operator">*</span>formatItem <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(format); formatItem<span class="operator">-</span><span class="operator">></span>setFlags(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ItemIsEnabled); formatItem<span class="operator">-</span><span class="operator">></span>setTextAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignTop <span class="operator">|</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignLeft); </pre> <p>Within each iteration, we create a <a href="qtablewidgetitem.html">QTableWidgetItem</a>, <code>formatItem</code> and we set its <a href="qtablewidgetitem.html#setFlags">flags</a> to <a href="../qtcore/qt.html#ItemFlag-enum">Qt::ItemIsEnabled</a>, and its <a href="qtablewidgetitem.html#setTextAlignment">text alignment</a> to <a href="../qtcore/qt.html#AlignmentFlag-enum">Qt::AlignTop</a> and <a href="../qtcore/qt.html#AlignmentFlag-enum">Qt::AlignLeft</a>.</p> <p>A <a href="../qtcore/qstring.html">QString</a> object, <code>text</code>, is customized to display data according to the contents of <code>format</code>. We invoke <a href="../qtcore/qstring.html">QString</a>'s <a href="../qtcore/qstring.html#simplified">simplified()</a> function on <code>text</code>, to obtain a string that has no additional space before, after or in between words.</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text; <span class="keyword">if</span> (format <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">"text/plain"</span>)) { text <span class="operator">=</span> mimeData<span class="operator">-</span><span class="operator">></span>text()<span class="operator">.</span>simplified(); } <span class="keyword">else</span> <span class="keyword">if</span> (format <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">"text/html"</span>)) { text <span class="operator">=</span> mimeData<span class="operator">-</span><span class="operator">></span>html()<span class="operator">.</span>simplified(); } <span class="keyword">else</span> <span class="keyword">if</span> (format <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">"text/uri-list"</span>)) { <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">></span> urlList <span class="operator">=</span> mimeData<span class="operator">-</span><span class="operator">></span>urls(); <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> urlList<span class="operator">.</span>size() <span class="operator">&</span><span class="operator">&</span> i <span class="operator"><</span> <span class="number">32</span>; <span class="operator">+</span><span class="operator">+</span>i) text<span class="operator">.</span>append(urlList<span class="operator">.</span>at(i)<span class="operator">.</span>toString() <span class="operator">+</span> QLatin1Char(<span class="char">' '</span>)); } <span class="keyword">else</span> { <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> data <span class="operator">=</span> mimeData<span class="operator">-</span><span class="operator">></span>data(format); <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> data<span class="operator">.</span>size() <span class="operator">&</span><span class="operator">&</span> i <span class="operator"><</span> <span class="number">32</span>; <span class="operator">+</span><span class="operator">+</span>i) text<span class="operator">.</span>append(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"%1 "</span>)<span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qtglobal.html#uchar-typedef">uchar</a></span>(data<span class="operator">[</span>i<span class="operator">]</span>)<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">16</span><span class="operator">,</span> QLatin1Char(<span class="char">'0'</span>))<span class="operator">.</span>toUpper()); } </pre> <p>If <code>format</code> contains a list of URLs, we iterate through them, using spaces to separate them. On the other hand, if <code>format</code> contains an image, we display the data by converting the text to hexadecimal.</p> <pre class="cpp"> <span class="type">int</span> row <span class="operator">=</span> formatsTable<span class="operator">-</span><span class="operator">></span>rowCount(); formatsTable<span class="operator">-</span><span class="operator">></span>insertRow(row); formatsTable<span class="operator">-</span><span class="operator">></span>setItem(row<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(format)); formatsTable<span class="operator">-</span><span class="operator">></span>setItem(row<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="keyword">new</span> <span class="type"><a href="qtablewidgetitem.html">QTableWidgetItem</a></span>(text)); } formatsTable<span class="operator">-</span><span class="operator">></span>resizeColumnToContents(<span class="number">0</span>); } </pre> <p>Once <code>text</code> has been customized to contain the appropriate data, we insert both <code>format</code> and <code>text</code> into <code>formatsTable</code> with <a href="qtablewidget.html#setItem">setItem()</a>. Lastly, we invoke <a href="qtableview.html#resizeColumnToContents">resizeColumnToContents()</a> on <code>formatsTable</code>'s first column.</p> <a name="the-main-function"></a> <h2 id="the-main-function">The main() Function</h2> <p>Within the <code>main()</code> function, we instantiate <code>DropSiteWindow</code> and invoke its <a href="qwidget.html#show">show()</a> function.</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); DropSiteWindow window; window<span class="operator">.</span>show(); <span class="keyword">return</span> app<span class="operator">.</span>exec(); } </pre> <p>Files:</p> <ul> <li><a href="qtwidgets-draganddrop-dropsite-droparea-cpp.html">draganddrop/dropsite/droparea.cpp</a></li> <li><a href="qtwidgets-draganddrop-dropsite-droparea-h.html">draganddrop/dropsite/droparea.h</a></li> <li><a href="qtwidgets-draganddrop-dropsite-dropsite-pro.html">draganddrop/dropsite/dropsite.pro</a></li> <li><a href="qtwidgets-draganddrop-dropsite-dropsitewindow-cpp.html">draganddrop/dropsite/dropsitewindow.cpp</a></li> <li><a href="qtwidgets-draganddrop-dropsite-dropsitewindow-h.html">draganddrop/dropsite/dropsitewindow.h</a></li> <li><a href="qtwidgets-draganddrop-dropsite-main-cpp.html">draganddrop/dropsite/main.cpp</a></li> </ul> </div> <!-- @@@draganddrop/dropsite --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>