5. Drag file onto Window (3/4)
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain"))
event->acceptProposedAction();
}
Standard MIME types are defined by the Internet Assigned
Numbers Authority (IANA). They consist of a type and a subtype
separated by a slash.
The official list of MIME types is available at http://www.iana.org/
assignments/media-types/
12. Drag custom data (1/2)
1. Provide arbitrary data as a QByteArray using
QMimeData::setData() and extract it later
using QMimeData::data()
2. Subclass QMimeData and re-implement
formats() and retrieveData() to handle our
custom data types
3. For drag and drop operations within a
single application, we can subclass
QMimeData and store the data using any
data structure we want
13. Drag custom data (2/2)
• Drawbacks of Method 1
‣ Need
to
convert
our
data
structure
to
a
QByteArray
even
if
the
drag
is
not
ul1mately
accepted
‣ Providing
several
MIME
types
to
interact
nicely
with
a
wide
range
of
applica=ons,
we
need
to
store
the
data
several
1mes
‣ If
the
data
is
large,
this
can
slow
down
the
applica1on
needlessly
14. Add drag and drop capabilities to a
QTableWidget (1/3)
• Method 1
void MyTableWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - startPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
performDrag();
}
QTableWidget::mouseMoveEvent(event);
}
15. Add drag and drop capabilities to a
QTableWidget (2/3)
void MyTableWidget::performDrag()
{
QString plainText = selectionAsPlainText(); Chap4 (p.87)
if (plainText.isEmpty())
return;
QMimeData *mimeData = new QMimeData;
mimeData->setText(plainText);
mimeData->setHtml(toHtml(plainText));
mimeData->setData("text/csv", toCsv(plainText).toUtf8());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
if (drag->exec(Qt::CopyAction | Qt::MoveAction) == Qt::MoveAction)
deleteSelection();
}
16. Add drag and drop capabilities to a
QTableWidget (3/3)
void MyTableWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("text/csv")) {
QByteArray csvData = event->mimeData()->data("text/csv");
QString csvText = QString::fromUtf8(csvData);
...
event->acceptProposedAction();
} else if (event->mimeData()->hasFormat("text/plain")) {
QString plainText = event->mimeData()->text();
...
event->acceptProposedAction();
}
}
QTableWidget Html OK
19. Subclass QMimeData (3/3)
void MyTableWidget::dropEvent(QDropEvent *event)
{
const TableMimeData *tableData =
qobject_cast<const TableMimeData *>(event->mimeData());
if (tableData) {
const QTableWidget *otherTable = tableData->tableWidget();
QTableWidgetSelectionRange otherRange = tableData->range();
...
event->acceptProposedAction();
} else if (event->mimeData()->hasFormat("text/csv")) {
QByteArray csvData = event->mimeData()->data("text/csv");
QString csvText = QString::fromUtf8(csvData);
...
we can directly access the table
}
QTableWidget::mouseMoveEvent(event); data instead of going through
QMimeData's API
}
20. Clipboard Handling
• Access clipboard: QApplication::clipboard()
• Built-in functionality might not be sufficient
(not just text or an image)
‣ Subclass
QMimeData
and
re-‐implement
a
few
virtual
func=ons
‣ Reuse
the
QMimeData
subclass
and
put
it
on
the
clipboard
using
the
setMimeData()
func=on.
To
retrieve
the
data,
we
can
call
mimeData()
on
the
clipboard
• Clipboard's contents change
‣ QClipboard::dataChanged()
signal