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 <QDebug>
#include <QMimeData>
#include <QMouseEvent>
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<QUrl> &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()) {

View File

@ -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

View File

@ -10,36 +10,32 @@
<height>600</height>
</rect>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<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">
<string>TextLabel</string>
</property>
</widget>
<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">
<string>PushButton</string>
</property>
</widget>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Display Image</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>