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