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 <paul.wicking@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
bb10
Friedemann Kleint 2018-01-02 08:51:31 +01:00
parent bb8ca16459
commit 72106832e4
3 changed files with 29 additions and 6 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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;
};