From 81a289c4d054db78490eec821b055f7079964e42 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 31 Oct 2020 14:30:29 +0800 Subject: [PATCH] allowed to DnD file on window for easier testing --- mainwindow.cpp | 30 +++++++++++++++++++++++++++++- mainwindow.h | 6 ++++++ mainwindow.ui | 48 ++++++++++++++++++++++-------------------------- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index c6fdb70..4b80e2f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,10 +3,13 @@ #include #include +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) + , m_filePath(R"(E:\16bit-raw-argb-affinityphoto.psd)") { ui->setupUi(this); } @@ -16,6 +19,31 @@ MainWindow::~MainWindow() delete ui; } +void MainWindow::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasUrls()) { + event->acceptProposedAction(); + } else { + event->ignore(); + } + + return QMainWindow::dragEnterEvent(event); +} + +void MainWindow::dropEvent(QDropEvent *event) +{ + event->acceptProposedAction(); + + const QMimeData * mimeData = event->mimeData(); + + if (mimeData->hasUrls()) { + const QList &urls = mimeData->urls(); + if (!urls.isEmpty()) { + m_filePath = urls.first().toLocalFile(); + ui->label->setText(m_filePath); + } + } +} void MainWindow::on_pushButton_clicked() { @@ -23,7 +51,7 @@ void MainWindow::on_pushButton_clicked() qDebug() << QImageReader::supportedImageFormats(); // 后面的文件改成 psd 文件的路径 - QScopedPointer< QImageReader> ir(new QImageReader(R"(E:\ff.psd)")); + QScopedPointer< QImageReader> ir(new QImageReader(m_filePath)); qDebug() << ir->canRead(); QImage read = ir->read(); if (read.isNull()) { diff --git a/mainwindow.h b/mainwindow.h index e02a946..7be6887 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -15,10 +15,16 @@ public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); +protected: + void dragEnterEvent(QDragEnterEvent *event) override; + void dropEvent(QDropEvent *event) override; + private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; + + QString m_filePath; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 303d58c..274e6d7 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -10,36 +10,32 @@ 600 + + true + MainWindow - - - - 0 - 0 - 801 - 561 - - - - TextLabel - - - - - - 0 - 570 - 801 - 24 - - - - PushButton - - + + + + + TextLabel + + + Qt::AlignCenter + + + + + + + Display Image + + + +