blob: 8eecfa53b775aa5cb0efbbd75bfb9bea568cd21f (
plain)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 | // Copyright (C) 2016 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause#include <QtGui>#include"mainwindow.h"MainWindow::MainWindow(QWidget *parent):QMainWindow(parent){ QFrame *centralFrame =newQFrame(this); QLabel *nameLabel =newQLabel(tr("Comment:"), centralFrame); commentEdit =newQTextEdit(centralFrame); QLabel *dragLabel =newQLabel(tr("<p>Drag the icon to a filer ""window or the desktop background:</p>"), centralFrame); iconLabel =newQLabel(centralFrame); iconPixmap.load(":/images/file.png"); iconLabel->setPixmap(iconPixmap); QGridLayout *grid =newQGridLayout(centralFrame); grid->addWidget(nameLabel,0,0); grid->addWidget(commentEdit,1,0,1,2); grid->addWidget(dragLabel,2,0); grid->addWidget(iconLabel,2,1);statusBar();setCentralWidget(centralFrame);setWindowTitle(tr("Dragging"));}//! [0]voidMainWindow::mousePressEvent(QMouseEvent *event){if(event->button() ==Qt::LeftButton && iconLabel->geometry().contains(event->pos())) {//! [1] QDrag *drag =newQDrag(this); QMimeData *mimeData =new QMimeData; mimeData->setText(commentEdit->toPlainText()); drag->setMimeData(mimeData);//! [1] drag->setPixmap(iconPixmap);Qt::DropAction dropAction = drag->exec();//! [0] QString actionText;switch(dropAction) {caseQt::CopyAction: actionText =tr("The text was copied.");break;caseQt::MoveAction: actionText =tr("The text was moved.");break;caseQt::LinkAction: actionText =tr("The text was linked.");break;caseQt::IgnoreAction: actionText =tr("The drag was ignored.");break;default: actionText =tr("Unknown action.");break;}statusBar()->showMessage(actionText);//! [2]}}//! [2]
|