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

发布时间:2023年12月25日
很多电脑软件都有鼠标拖动一张图片或者拖动一个文件到软件的指定区域内,就可以自动在软件中显示图片内容或者文件内容。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

文章来源:https://blog.csdn.net/ml29895063/article/details/135190912
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。