From 72106832e4dfd9a773541e4490d5e594caf1cf50 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 2 Jan 2018 08:51:31 +0100 Subject: QtWidgets/D&D/dropsite example: Add copy functionality Add a slot copying the QMimeData contents as text. Task-number: QTBUG-60635 Change-Id: Ided14fccd8a49029ff1aff64969454ed2232dbeb Reviewed-by: Paul Wicking Reviewed-by: Friedemann Kleint --- .../widgets/draganddrop/dropsite/dropsitewindow.cpp | 21 +++++++++++++++++++++ .../widgets/draganddrop/dropsite/dropsitewindow.h | 2 ++ 2 files changed, 23 insertions(+) (limited to 'examples/widgets/draganddrop/dropsite') diff --git a/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp b/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp index 2dae83bb22..a937e24a4c 100644 --- a/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp +++ b/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp @@ -82,14 +82,21 @@ DropSiteWindow::DropSiteWindow() //! [constructor part4] clearButton = new QPushButton(tr("Clear")); + copyButton = new QPushButton(tr("Copy")); quitButton = new QPushButton(tr("Quit")); buttonBox = new QDialogButtonBox; buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(copyButton, QDialogButtonBox::ActionRole); +#if !QT_CONFIG(clipboard) + copyButton->setVisible(false); +#endif + buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear); + connect(copyButton, &QAbstractButton::clicked, this, &DropSiteWindow::copy); //! [constructor part4] //! [constructor part5] @@ -108,6 +115,7 @@ DropSiteWindow::DropSiteWindow() void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData) { formatsTable->setRowCount(0); + copyButton->setEnabled(false); if (!mimeData) return; //! [updateFormatsTable() part1] @@ -145,5 +153,18 @@ void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData) } formatsTable->resizeColumnToContents(0); +#if QT_CONFIG(clipboard) + copyButton->setEnabled(formatsTable->rowCount() > 0); +#endif } //! [updateFormatsTable() part4] + +void DropSiteWindow::copy() +{ +#if QT_CONFIG(clipboard) + QString text; + for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row) + text += formatsTable->item(row, 0)->text() + ": " + formatsTable->item(row, 1)->text() + '\n'; + QGuiApplication::clipboard()->setText(text); +#endif +} diff --git a/examples/widgets/draganddrop/dropsite/dropsitewindow.h b/examples/widgets/draganddrop/dropsite/dropsitewindow.h index d80476f369..a40b481637 100644 --- a/examples/widgets/draganddrop/dropsite/dropsitewindow.h +++ b/examples/widgets/draganddrop/dropsite/dropsitewindow.h @@ -72,6 +72,7 @@ public: public slots: void updateFormatsTable(const QMimeData *mimeData); + void copy(); private: DropArea *dropArea; @@ -79,6 +80,7 @@ private: QTableWidget *formatsTable; QPushButton *clearButton; + QPushButton *copyButton; QPushButton *quitButton; QDialogButtonBox *buttonBox; }; -- cgit v1.2.3