mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 07:15:27 +08:00
qt 6.5.1 original
This commit is contained in:
38
examples/widgets/draganddrop/dropsite/CMakeLists.txt
Normal file
38
examples/widgets/draganddrop/dropsite/CMakeLists.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dropsite LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/dropsite")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(dropsite
|
||||
droparea.cpp droparea.h
|
||||
dropsitewindow.cpp dropsitewindow.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(dropsite PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(dropsite PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS dropsite
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
93
examples/widgets/draganddrop/dropsite/droparea.cpp
Normal file
93
examples/widgets/draganddrop/dropsite/droparea.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "droparea.h"
|
||||
|
||||
#include <QDragEnterEvent>
|
||||
#include <QMimeData>
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
//! [DropArea constructor]
|
||||
DropArea::DropArea(QWidget *parent)
|
||||
: QLabel(parent)
|
||||
{
|
||||
setMinimumSize(200, 200);
|
||||
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
|
||||
setAlignment(Qt::AlignCenter);
|
||||
setAcceptDrops(true);
|
||||
setAutoFillBackground(true);
|
||||
clear();
|
||||
}
|
||||
//! [DropArea constructor]
|
||||
|
||||
//! [dragEnterEvent() function]
|
||||
void DropArea::dragEnterEvent(QDragEnterEvent *event)
|
||||
{
|
||||
setText(tr("<drop content>"));
|
||||
setBackgroundRole(QPalette::Highlight);
|
||||
|
||||
event->acceptProposedAction();
|
||||
emit changed(event->mimeData());
|
||||
}
|
||||
//! [dragEnterEvent() function]
|
||||
|
||||
//! [dragMoveEvent() function]
|
||||
void DropArea::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
//! [dragMoveEvent() function]
|
||||
|
||||
//! [dropEvent() function part1]
|
||||
void DropArea::dropEvent(QDropEvent *event)
|
||||
{
|
||||
const QMimeData *mimeData = event->mimeData();
|
||||
//! [dropEvent() function part1]
|
||||
|
||||
//! [dropEvent() function part2]
|
||||
if (mimeData->hasImage()) {
|
||||
setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
|
||||
} else if (mimeData->hasFormat(u"text/markdown"_s)) {
|
||||
setText(QString::fromUtf8(mimeData->data(u"text/markdown"_s)));
|
||||
setTextFormat(Qt::MarkdownText);
|
||||
} else if (mimeData->hasHtml()) {
|
||||
setText(mimeData->html());
|
||||
setTextFormat(Qt::RichText);
|
||||
} else if (mimeData->hasText()) {
|
||||
setText(mimeData->text());
|
||||
setTextFormat(Qt::PlainText);
|
||||
} else if (mimeData->hasUrls()) {
|
||||
QList<QUrl> urlList = mimeData->urls();
|
||||
QString text;
|
||||
for (qsizetype i = 0, count = qMin(urlList.size(), qsizetype(32)); i < count; ++i)
|
||||
text += urlList.at(i).path() + u'\n';
|
||||
setText(text);
|
||||
} else {
|
||||
setText(tr("Cannot display data"));
|
||||
}
|
||||
//! [dropEvent() function part2]
|
||||
|
||||
//! [dropEvent() function part3]
|
||||
setBackgroundRole(QPalette::Dark);
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
//! [dropEvent() function part3]
|
||||
|
||||
//! [dragLeaveEvent() function]
|
||||
void DropArea::dragLeaveEvent(QDragLeaveEvent *event)
|
||||
{
|
||||
clear();
|
||||
event->accept();
|
||||
}
|
||||
//! [dragLeaveEvent() function]
|
||||
|
||||
//! [clear() function]
|
||||
void DropArea::clear()
|
||||
{
|
||||
setText(tr("<drop content>"));
|
||||
setBackgroundRole(QPalette::Dark);
|
||||
|
||||
emit changed();
|
||||
}
|
||||
//! [clear() function]
|
37
examples/widgets/draganddrop/dropsite/droparea.h
Normal file
37
examples/widgets/draganddrop/dropsite/droparea.h
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DROPAREA_H
|
||||
#define DROPAREA_H
|
||||
|
||||
#include <QLabel>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QMimeData;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [DropArea header part1]
|
||||
class DropArea : public QLabel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DropArea(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void clear();
|
||||
|
||||
signals:
|
||||
void changed(const QMimeData *mimeData = nullptr);
|
||||
//! [DropArea header part1]
|
||||
|
||||
//! [DropArea header part2]
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dragLeaveEvent(QDragLeaveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
};
|
||||
//! [DropArea header part2]
|
||||
|
||||
#endif // DROPAREA_H
|
12
examples/widgets/draganddrop/dropsite/dropsite.pro
Normal file
12
examples/widgets/draganddrop/dropsite/dropsite.pro
Normal file
@ -0,0 +1,12 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(tablewidget))
|
||||
|
||||
HEADERS = droparea.h \
|
||||
dropsitewindow.h
|
||||
SOURCES = droparea.cpp \
|
||||
dropsitewindow.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/dropsite
|
||||
INSTALLS += target
|
135
examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
Normal file
135
examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QPushButton>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QHeaderView>
|
||||
#include <QTableWidget>
|
||||
#include <QTableWidgetItem>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QGuiApplication>
|
||||
|
||||
#include <QMimeData>
|
||||
|
||||
#include "droparea.h"
|
||||
#include "dropsitewindow.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
//! [constructor part1]
|
||||
DropSiteWindow::DropSiteWindow()
|
||||
{
|
||||
abstractLabel = new QLabel(tr("This example accepts drags from other "
|
||||
"applications and displays the MIME types "
|
||||
"provided by the drag object."));
|
||||
abstractLabel->setWordWrap(true);
|
||||
abstractLabel->adjustSize();
|
||||
//! [constructor part1]
|
||||
|
||||
//! [constructor part2]
|
||||
dropArea = new DropArea;
|
||||
connect(dropArea, &DropArea::changed,
|
||||
this, &DropSiteWindow::updateFormatsTable);
|
||||
//! [constructor part2]
|
||||
|
||||
//! [constructor part3]
|
||||
formatsTable = new QTableWidget;
|
||||
formatsTable->setColumnCount(2);
|
||||
formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
formatsTable->setHorizontalHeaderLabels({tr("Format"), tr("Content")});
|
||||
formatsTable->horizontalHeader()->setStretchLastSection(true);
|
||||
//! [constructor part3]
|
||||
|
||||
//! [constructor part4]
|
||||
clearButton = new QPushButton(tr("Clear"));
|
||||
copyButton = new QPushButton(tr("Copy"));
|
||||
quitButton = new QPushButton(tr("Quit"));
|
||||
|
||||
buttonBox = new QDialogButtonBox;
|
||||
buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
|
||||
buttonBox->addButton(copyButton, QDialogButtonBox::ActionRole);
|
||||
#if !QT_CONFIG(clipboard)
|
||||
copyButton->setVisible(false);
|
||||
#endif
|
||||
|
||||
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
||||
|
||||
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||
connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear);
|
||||
connect(copyButton, &QAbstractButton::clicked, this, &DropSiteWindow::copy);
|
||||
//! [constructor part4]
|
||||
|
||||
//! [constructor part5]
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||||
mainLayout->addWidget(abstractLabel);
|
||||
mainLayout->addWidget(dropArea);
|
||||
mainLayout->addWidget(formatsTable);
|
||||
mainLayout->addWidget(buttonBox);
|
||||
|
||||
setWindowTitle(tr("Drop Site"));
|
||||
resize(700, 500);
|
||||
}
|
||||
//! [constructor part5]
|
||||
|
||||
//! [updateFormatsTable() part1]
|
||||
void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
|
||||
{
|
||||
formatsTable->setRowCount(0);
|
||||
copyButton->setEnabled(false);
|
||||
if (!mimeData)
|
||||
return;
|
||||
//! [updateFormatsTable() part1]
|
||||
|
||||
//! [updateFormatsTable() part2]
|
||||
const QStringList formats = mimeData->formats();
|
||||
for (const QString &format : formats) {
|
||||
QTableWidgetItem *formatItem = new QTableWidgetItem(format);
|
||||
formatItem->setFlags(Qt::ItemIsEnabled);
|
||||
formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft);
|
||||
//! [updateFormatsTable() part2]
|
||||
|
||||
//! [updateFormatsTable() part3]
|
||||
QString text;
|
||||
if (format == u"text/plain") {
|
||||
text = mimeData->text().simplified();
|
||||
} else if (format == u"text/markdown") {
|
||||
text = QString::fromUtf8(mimeData->data(u"text/markdown"_s));
|
||||
} else if (format == u"text/html") {
|
||||
text = mimeData->html().simplified();
|
||||
} else if (format == u"text/uri-list") {
|
||||
QList<QUrl> urlList = mimeData->urls();
|
||||
for (qsizetype i = 0, count = qMin(urlList.size(), qsizetype(32)); i < count; ++i)
|
||||
text.append(urlList.at(i).toString() + u' ');
|
||||
} else {
|
||||
QByteArray data = mimeData->data(format);
|
||||
if (data.size() > 32)
|
||||
data.truncate(32);
|
||||
text = QString::fromLatin1(data.toHex(' ')).toUpper();
|
||||
}
|
||||
//! [updateFormatsTable() part3]
|
||||
|
||||
//! [updateFormatsTable() part4]
|
||||
int row = formatsTable->rowCount();
|
||||
formatsTable->insertRow(row);
|
||||
formatsTable->setItem(row, 0, new QTableWidgetItem(format));
|
||||
formatsTable->setItem(row, 1, new QTableWidgetItem(text));
|
||||
}
|
||||
|
||||
formatsTable->resizeColumnToContents(0);
|
||||
#if QT_CONFIG(clipboard)
|
||||
copyButton->setEnabled(formatsTable->rowCount() > 0);
|
||||
#endif
|
||||
}
|
||||
//! [updateFormatsTable() part4]
|
||||
|
||||
void DropSiteWindow::copy()
|
||||
{
|
||||
#if QT_CONFIG(clipboard)
|
||||
QString text;
|
||||
for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row)
|
||||
text += formatsTable->item(row, 0)->text() + ": " + formatsTable->item(row, 1)->text() + '\n';
|
||||
QGuiApplication::clipboard()->setText(text);
|
||||
#endif
|
||||
}
|
42
examples/widgets/draganddrop/dropsite/dropsitewindow.h
Normal file
42
examples/widgets/draganddrop/dropsite/dropsitewindow.h
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DROPSITEWINDOW_H
|
||||
#define DROPSITEWINDOW_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDialogButtonBox;
|
||||
class QLabel;
|
||||
class QMimeData;
|
||||
class QPushButton;
|
||||
class QTableWidget;
|
||||
QT_END_NAMESPACE
|
||||
class DropArea;
|
||||
|
||||
//! [DropSiteWindow header]
|
||||
class DropSiteWindow : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DropSiteWindow();
|
||||
|
||||
public slots:
|
||||
void updateFormatsTable(const QMimeData *mimeData);
|
||||
void copy();
|
||||
|
||||
private:
|
||||
DropArea *dropArea;
|
||||
QLabel *abstractLabel;
|
||||
QTableWidget *formatsTable;
|
||||
|
||||
QPushButton *clearButton;
|
||||
QPushButton *copyButton;
|
||||
QPushButton *quitButton;
|
||||
QDialogButtonBox *buttonBox;
|
||||
};
|
||||
//! [DropSiteWindow header]
|
||||
|
||||
#endif // DROPSITEWINDOW_H
|
16
examples/widgets/draganddrop/dropsite/main.cpp
Normal file
16
examples/widgets/draganddrop/dropsite/main.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "dropsitewindow.h"
|
||||
|
||||
//! [main() function]
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
DropSiteWindow window;
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
||||
//! [main() function]
|
Reference in New Issue
Block a user