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
finalwidget.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/*
5finalwidget.cpp
6
7A widget to display an image and a label containing a description.
8*/
9
10#include "finalwidget.h"
11
12#include <QApplication>
13#include <QBuffer>
14#include <QDrag>
15#include <QLabel>
16#include <QMimeData>
17#include <QMouseEvent>
18#include <QVBoxLayout>
19
21 const QSize &labelSize)
22 : QFrame(parent)
23{
24 hasImage = false;
25 imageLabel = new QLabel;
26 imageLabel->setFrameShadow(QFrame::Sunken);
28 imageLabel->setMinimumSize(labelSize);
29 nameLabel = new QLabel(name);
30
31 QVBoxLayout *layout = new QVBoxLayout(this);
32 layout->addWidget(imageLabel, 1);
33 layout->addWidget(nameLabel, 0);
34}
35
42{
43 if (!(event->buttons() & Qt::LeftButton))
44 return;
45 if ((event->pos() - dragStartPosition).manhattanLength()
47 return;
48 if (!hasImage)
49 return;
50
51 QDrag *drag = new QDrag(this);
53
56 QBuffer outputBuffer(&output);
57 outputBuffer.open(QIODevice::WriteOnly);
58 imageLabel->pixmap().toImage().save(&outputBuffer, "PNG");
59 mimeData->setData("image/png", output);
61/*
63 mimeData->setImageData(QVariant(*imageLabel->pixmap()));
65*/
66 drag->setMimeData(mimeData);
67 drag->setPixmap(imageLabel->pixmap().scaled(64, 64, Qt::KeepAspectRatio));
69 drag->setHotSpot(QPoint(drag->pixmap().width()/2,
70 drag->pixmap().height()));
72}
73
79{
80 if (event->button() == Qt::LeftButton)
81 dragStartPosition = event->pos();
82}
83
85{
86 return imageLabel->pixmap();
87}
88
90{
91 imageLabel->setPixmap(pixmap);
92 hasImage = true;
93}
void mousePressEvent(QMouseEvent *event) override
Check for left mouse button presses in order to enable drag and drop.
void mouseMoveEvent(QMouseEvent *event) override
If the mouse moves far enough when the left mouse button is held down, start a drag and drop operatio...
QPixmap pixmap() const
void setPixmap(const QPixmap &pixmap)
FinalWidget(QWidget *parent, const QString &name, const QSize &labelSize)
int startDragDistance
the minimum distance required for a drag and drop operation to start.
\inmodule QtCore \reentrant
Definition qbuffer.h:16
bool open(OpenMode openMode) override
\reimp
Definition qbuffer.cpp:295
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtGui
Definition qdrag.h:22
void setHotSpot(const QPoint &hotspot)
Sets the position of the hot spot relative to the top-left corner of the pixmap used to the point spe...
Definition qdrag.cpp:148
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition qdrag.cpp:101
QPixmap pixmap() const
Returns the pixmap used to represent the data in a drag and drop operation.
Definition qdrag.cpp:134
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
@ Sunken
Definition qframe.h:51
void setFrameShape(Shape)
Definition qframe.cpp:257
@ StyledPanel
Definition qframe.h:45
void setFrameShadow(Shadow)
Definition qframe.cpp:276
bool save(const QString &fileName, const char *format=nullptr, int quality=-1) const
Saves the image to the file with the given fileName, using the given image file format and quality fa...
Definition qimage.cpp:3888
The QLabel widget provides a text or image display.
Definition qlabel.h:20
QPixmap pixmap
the label's pixmap.
Definition qlabel.h:24
void setPixmap(const QPixmap &)
Definition qlabel.cpp:339
void addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout.
Definition qlayout.cpp:186
\inmodule QtCore
Definition qmimedata.h:16
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
\inmodule QtGui
Definition qevent.h:196
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
int height() const
Returns the height of the pixmap.
Definition qpixmap.cpp:480
QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition qpixmap.h:78
QImage toImage() const
Converts the pixmap to a QImage.
Definition qpixmap.cpp:408
int width() const
Returns the width of the pixmap.
Definition qpixmap.cpp:468
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QVBoxLayout class lines up widgets vertically.
Definition qboxlayout.h:91
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setMinimumSize(const QSize &)
Definition qwidget.h:832
QLayout * layout() const
Returns the layout manager that is installed on this widget, or \nullptr if no layout manager is inst...
@ LeftButton
Definition qnamespace.h:58
@ KeepAspectRatio
GLuint name
struct _cl_event * event
QT_BEGIN_NAMESPACE typedef uchar * output
QMimeData * mimeData
widget render & pixmap