allowed to DnD file on window for easier testing

This commit is contained in:
Gary Wang 2020-10-31 14:30:29 +08:00
parent 0469afeb05
commit 81a289c4d0
3 changed files with 57 additions and 27 deletions

View File

@ -3,10 +3,13 @@
#include <QImageReader> #include <QImageReader>
#include <QDebug> #include <QDebug>
#include <QMimeData>
#include <QMouseEvent>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, m_filePath(R"(E:\16bit-raw-argb-affinityphoto.psd)")
{ {
ui->setupUi(this); ui->setupUi(this);
} }
@ -16,6 +19,31 @@ MainWindow::~MainWindow()
delete ui; 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<QUrl> &urls = mimeData->urls();
if (!urls.isEmpty()) {
m_filePath = urls.first().toLocalFile();
ui->label->setText(m_filePath);
}
}
}
void MainWindow::on_pushButton_clicked() void MainWindow::on_pushButton_clicked()
{ {
@ -23,7 +51,7 @@ void MainWindow::on_pushButton_clicked()
qDebug() << QImageReader::supportedImageFormats(); qDebug() << QImageReader::supportedImageFormats();
// 后面的文件改成 psd 文件的路径 // 后面的文件改成 psd 文件的路径
QScopedPointer< QImageReader> ir(new QImageReader(R"(E:\ff.psd)")); QScopedPointer< QImageReader> ir(new QImageReader(m_filePath));
qDebug() << ir->canRead(); qDebug() << ir->canRead();
QImage read = ir->read(); QImage read = ir->read();
if (read.isNull()) { if (read.isNull()) {

View File

@ -15,10 +15,16 @@ public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
private slots: private slots:
void on_pushButton_clicked(); void on_pushButton_clicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QString m_filePath;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -10,36 +10,32 @@
<height>600</height> <height>600</height>
</rect> </rect>
</property> </property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>MainWindow</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>801</width>
<height>561</height>
</rect>
</property>
<property name="text"> <property name="text">
<string>TextLabel</string> <string>TextLabel</string>
</property> </property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget> </widget>
</item>
<item>
<widget class="QPushButton" name="pushButton"> <widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>0</x>
<y>570</y>
<width>801</width>
<height>24</height>
</rect>
</property>
<property name="text"> <property name="text">
<string>PushButton</string> <string>Display Image</string>
</property> </property>
</widget> </widget>
</item>
</layout>
</widget> </widget>
</widget> </widget>
<resources/> <resources/>