Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mainwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtGui>
5
6#include "mainwindow.h"
7
8MainWindow::MainWindow(QWidget *parent)
10{
11 QFrame *centralFrame = new QFrame(this);
12
13 QLabel *nameLabel = new QLabel(tr("Comment:"), centralFrame);
14 commentEdit = new QTextEdit(centralFrame);
15 QLabel *dragLabel = new QLabel(tr("<p>Drag the icon to a filer "
16 "window or the desktop background:</p>"),
17 centralFrame);
18 iconLabel = new QLabel(centralFrame);
19 iconPixmap.load(":/images/file.png");
20 iconLabel->setPixmap(iconPixmap);
21
22 QGridLayout *grid = new QGridLayout(centralFrame);
23 grid->addWidget(nameLabel, 0, 0);
24 grid->addWidget(commentEdit, 1, 0, 1, 2);
25 grid->addWidget(dragLabel, 2, 0);
26 grid->addWidget(iconLabel, 2, 1);
27
28 statusBar();
29 setCentralWidget(centralFrame);
30 setWindowTitle(tr("Dragging"));
31}
32
33//! [0]
34void MainWindow::mousePressEvent(QMouseEvent *event)
35{
36 if (event->button() == Qt::LeftButton
37 && iconLabel->geometry().contains(event->pos())) {
38
39//! [1]
40 QDrag *drag = new QDrag(this);
41 QMimeData *mimeData = new QMimeData;
42
43 mimeData->setText(commentEdit->toPlainText());
44 drag->setMimeData(mimeData);
45//! [1]
46 drag->setPixmap(iconPixmap);
47
48 Qt::DropAction dropAction = drag->exec();
49//! [0]
50
51 QString actionText;
52 switch (dropAction) {
53 case Qt::CopyAction:
54 actionText = tr("The text was copied.");
55 break;
56 case Qt::MoveAction:
57 actionText = tr("The text was moved.");
58 break;
59 case Qt::LinkAction:
60 actionText = tr("The text was linked.");
61 break;
62 case Qt::IgnoreAction:
63 actionText = tr("The drag was ignored.");
64 break;
65 default:
66 actionText = tr("Unknown action.");
67 break;
68 }
69 statusBar()->showMessage(actionText);
70//! [2]
71 }
72}
73//! [2]
void mousePressEvent(QMouseEvent *event) override
[0]
MainWindow(QWidget *parent=nullptr)