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 --- examples/widgets/doc/dropsite.qdoc | 12 ++++++------ .../widgets/draganddrop/dropsite/dropsitewindow.cpp | 21 +++++++++++++++++++++ .../widgets/draganddrop/dropsite/dropsitewindow.h | 2 ++ 3 files changed, 29 insertions(+), 6 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/doc/dropsite.qdoc b/examples/widgets/doc/dropsite.qdoc index af8766a308..d8d09612f6 100644 --- a/examples/widgets/doc/dropsite.qdoc +++ b/examples/widgets/doc/dropsite.qdoc @@ -176,16 +176,16 @@ \snippet draganddrop/dropsite/dropsitewindow.cpp constructor part3 - Two QPushButton objects, \c clearButton and \c quitButton, are instantiated - and added to \c buttonBox - a QDialogButtonBox object. We use - QDialogButtonBox here to ensure that the push buttons are presented in a + Three QPushButton objects, \c clearButton, \c copyButton, and \c quitButton, + are instantiated and added to \c buttonBox - 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. \snippet draganddrop/dropsite/dropsitewindow.cpp constructor part4 - The \l{QPushButton::clicked()}{clicked()} signals for \c quitButton and - \c clearButton are connected to \l{QWidget::close()}{close()} and - \c clear(), respectively. + The \l{QPushButton::clicked()}{clicked()} signals for \c copyButton, + \c clearButton, and \c quitButton are connected to \c copy(), + \c clear() and \l{QWidget::close()}{close()}, respectively. For the layout, we use a QVBoxLayout, \c mainLayout, to arrange our widgets vertically. We also set the window title to "Drop Site" and the minimum 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