Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
9 : QMainWindow(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
35{
36 if (event->button() == Qt::LeftButton
37 && iconLabel->geometry().contains(event->pos())) {
38
40 QDrag *drag = new QDrag(this);
42
43 mimeData->setText(commentEdit->toPlainText());
44 drag->setMimeData(mimeData);
46 drag->setPixmap(iconPixmap);
47
48 Qt::DropAction dropAction = drag->exec();
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;
63 actionText = tr("The drag was ignored.");
64 break;
65 default:
66 actionText = tr("Unknown action.");
67 break;
68 }
69 statusBar()->showMessage(actionText);
71 }
72}
void mousePressEvent(QMouseEvent *event) override
[0]
\inmodule QtGui
Definition qdrag.h:22
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Definition qdrag.cpp:201
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition qdrag.cpp:101
void setPixmap(const QPixmap &)
Sets pixmap as the pixmap used to represent the data in a drag and drop operation.
Definition qdrag.cpp:125
The QFrame class is the base class of widgets that can have a frame.
Definition qframe.h:17
The QGridLayout class lays out widgets in a grid.
Definition qgridlayout.h:21
void addWidget(QWidget *w)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qgridlayout.h:64
The QLabel widget provides a text or image display.
Definition qlabel.h:20
The QMainWindow class provides a main application window.
Definition qmainwindow.h:25
\inmodule QtCore
Definition qmimedata.h:16
void setText(const QString &text)
Sets text as the plain text (MIME type text/plain) used to represent the data.
\inmodule QtGui
Definition qevent.h:196
bool contains(const QRect &r, bool proper=false) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrect.cpp:855
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
QString toPlainText() const
QString QTextEdit::toPlainText() const.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition qwidget.h:106
@ LeftButton
Definition qnamespace.h:58
DropAction
@ CopyAction
@ IgnoreAction
@ MoveAction
@ LinkAction
struct _cl_event * event
#define tr(X)
QMimeData * mimeData
statusBar() -> addWidget(new MyReadWriteIndication)
[0]