网页快照网站,西安印象网站建设,wordpress的版本号,wordpress局部刷新很多电脑软件都有鼠标拖动一张图片或者拖动一个文件到软件的指定区域内#xff0c;就可以自动在软件中显示图片内容或者文件内容。qt中也可以这样实现。
本文介绍两种方法#xff1a;
1、只可以以非管理员的身份运行软件时#xff0c;才可以实现上述功能。
mainwindow.h#…很多电脑软件都有鼠标拖动一张图片或者拖动一个文件到软件的指定区域内就可以自动在软件中显示图片内容或者文件内容。qt中也可以这样实现。
本文介绍两种方法
1、只可以以非管理员的身份运行软件时才可以实现上述功能。
mainwindow.h#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include QMainWindowQT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent nullptr);~MainWindow();//重写下面2个函数来完成接受外来拖拽的操作.void dragEnterEvent(QDragEnterEvent *event);void dropEvent(QDropEvent *event);private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_Hmainwindow.cpp#include mainwindow.h
#include ui_mainwindow.h
#include QDropEvent
#include QMimeDataMainWindow::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()){QListQUrl 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.proQT core guigreaterThan(QT_MAJOR_VERSION, 4): QT widgetsCONFIG c17# 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_BEFORE0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES \main.cpp \mainwindow.cppHEADERS \mainwindow.hFORMS \mainwindow.ui# Default rules for deployment.
qnx: target.path /tmp/$${TARGET}/bin
else: unix:!android: target.path /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS targetwin32:CONFIG(release, debug|release): LIBS -lOle32
else:win32:CONFIG(debug, debug|release): LIBS -lOle32d
else:unix: LIBS -lOle32INCLUDEPATH $$PWD/../lib
DEPENDPATH $$PWD/../libwin32:CONFIG(release, debug|release): LIBS -lUser32
else:win32:CONFIG(debug, debug|release): LIBS -lUser32d
else:unix: LIBS -lUser32INCLUDEPATH $$PWD/../lib
DEPENDPATH $$PWD/../lib
(2)、在main.cpp中设置允许管理员身份获取拖放事件。
main.cpp#include mainwindow.h#include QApplication
#include Windows.h
#include QDebugint 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 QMainWindowQT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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
#endifMainWindow::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_castMSG*(message);if (msg-message WM_DROPFILES){*result 0;
#ifdef Q_OS_WINMSG* msg reinterpret_castMSG*(message);HDROP dropHandle reinterpret_castHDROP(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;
#elseQ_UNUSED(message)return false;
#endif}}return QWidget::nativeEvent(eventType, message, result);
}效果 QQ录屏20231225105502