qt中实现鼠标拖动一张图片到qt控件内,图片自动在控件中展示

很多电脑软件都有鼠标拖动一张图片或者拖动一个文件到软件的指定区域内,就可以自动在软件中显示图片内容或者文件内容。qt中也可以这样实现。
本文介绍两种方法:
1、只可以以非管理员的身份运行软件时,才可以实现上述功能。
mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    //重写下面2个函数来完成接受外来拖拽的操作.
    void dragEnterEvent(QDragEnterEvent *event);

    void dropEvent(QDropEvent *event);

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDropEvent>
#include <QMimeData>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setAcceptDrops(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls())
    {
        event->acceptProposedAction();
    }
}


void MainWindow::dropEvent(QDropEvent *event)
{
    if (event->mimeData()->hasUrls())
    {
        QList<QUrl> urlList = event->mimeData()->urls();
        if (urlList.size() == 1)
        {
            QString filePath = urlList.first().toLocalFile();
            QPixmap pixmap(filePath);
            pixmap.scaled(ui->pictureLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            ui->pictureLabel->setPixmap(pixmap);
        }
    }
    event->acceptProposedAction();
    event->setDropAction(Qt::MoveAction);
    event->accept();
}
效果:

QQ录屏20231225103401

2、以管理员身份运行软件时,也可以实现上述的功能。
(1)、需要在.pro文件中加上两个静态依赖库,Ole32.lib, User32.lib
CustomDragDrap.pro


QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target


win32:CONFIG(release, debug|release): LIBS += -lOle32
else:win32:CONFIG(debug, debug|release): LIBS +=  -lOle32d
else:unix: LIBS += -lOle32

INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib

win32:CONFIG(release, debug|release): LIBS += -lUser32
else:win32:CONFIG(debug, debug|release): LIBS += -lUser32d
else:unix: LIBS += -lUser32

INCLUDEPATH += $$PWD/../lib
DEPENDPATH += $$PWD/../lib

(2)、在main.cpp中设置允许管理员身份获取拖放事件。
main.cpp

#include "mainwindow.h"

#include <QApplication>
#include "Windows.h"
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    //处理当以管理员身份运行程序时,不能捕捉到文件拖放事件的问题
    {
        void* user32 = LoadLibraryA("user32");
        FARPROC func = GetProcAddress((HMODULE)user32, "ChangeWindowMessageFilter");
        user32 = LoadLibraryA("user32");
        func = GetProcAddress((HMODULE)user32, "ChangeWindowMessageFilter");
        ChangeWindowMessageFilter(WM_DROPFILES, 1);
        qDebug() << w.winId() << w.effectiveWinId();
        ChangeWindowMessageFilterEx((HWND)w.effectiveWinId(), WM_DROPFILES, MSGFLT_ALLOW, NULL);
        ChangeWindowMessageFilterEx((HWND)w.effectiveWinId(), WM_COPYDATA, MSGFLT_ALLOW, NULL);
        ChangeWindowMessageFilterEx((HWND)w.effectiveWinId(), 0x0049, MSGFLT_ALLOW, NULL);
        DragAcceptFiles((HWND)w.effectiveWinId(), true);
        RevokeDragDrop((HWND)w.winId());
    }

    w.show();
    return a.exec();
}
(3)、重写nativeEvent获取拖放事件中的数据
mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    //重新该函数实现捕获系统的拖放事件和数据
    bool nativeEvent(const QByteArray &eventType, void *message, long *result);

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMimeData>
#ifdef Q_OS_WIN
#include <qt_windows.h>
#include <Windowsx.h>
#endif

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setAcceptDrops(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    if (eventType == "windows_generic_MSG")
    {
        MSG* msg = static_cast<MSG*>(message);
        if (msg->message == WM_DROPFILES)
        {
            *result = 0;
#ifdef Q_OS_WIN
            MSG* msg = reinterpret_cast<MSG*>(message);
            HDROP dropHandle = reinterpret_cast<HDROP>(msg->wParam);

            int numFiles = DragQueryFile(dropHandle, 0xFFFFFFFF, nullptr, 0);
            if (numFiles > 0)
            {
                //获取鼠标释放时的位置
                HWND hWnd = msg->hwnd;
                POINT point;
                GetCursorPos(&point);
                ScreenToClient(hWnd, &point);

                WCHAR filePath[MAX_PATH];
                DragQueryFileW(dropHandle, 0, filePath, MAX_PATH);
                // 处理拖入文件的路径
                QString picturepath = QString::fromWCharArray(filePath);
                //显示图片
                QPixmap pixmap(picturepath);
                ui->pictureLabel->setPixmap(pixmap);
            }
            DragFinish(dropHandle);
            return true;
#else
            Q_UNUSED(message)
            return false;
#endif
        }
    }
    return QWidget::nativeEvent(eventType, message, result);
}
效果

QQ录屏20231225105502

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

commonbelive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值