当前位置: 首页 > news >正文

QML----Webengineview点击网页上的下载没反应,下载文件

问题

使用webe加载网页时,点击下载页面会没有反应。原因就是它默认是关闭下载功能

解决

需要在profile里监听下载事件打开onDownloadRequested,当有下载时会触发这个信号,会获取到一个WebEngineDownloadItem这是下载的东西,查询它的一些相关参数,可以修改路径和开始下载

import QtQuick 2.15
import QtQuick.Window 2.15
import QtWebEngine 1.9
import Qt.labs.platform 1.1
import QtQuick.Controls 2.15
import QtQuick.Dialogs 1.0Window {width: 640height: 480visible: truetitle: qsTr("Hello World")property var savePath: "";WebEngineView{id:webviewanchors.fill:parent;url:"https://www.kuwo.cn/down"// 使用 WebEngineProfile 来监听下载事件profile: WebEngineProfile {id:webprofileonDownloadRequested: function(downloadItem) {//当触发下载时,onDownloadRequested 事件会被调用,接收 WebEngineDownloadItem 对象来管理下载过程。console.log("Download requested: ", downloadItem.url)// 使用 FileDialog 让用户选择文件路径// folderDialog.open();//设置下载路径,会获取电脑标准的下载路径进行拼接var customSavePath = StandardPaths.writableLocation(StandardPaths.DownloadLocation).toString().replace("file:///", "");;savePath = customSavePath;console.log("Custom save path: ", customSavePath);console.log("downloadDirectory path: ", downloadItem.downloadDirectory);downloadItem.downloadDirectory = customSavePath;console.log("downloadDirectory path: ", downloadItem.downloadDirectory);downloadItem.accept();}onDownloadFinished: function(downloadItem){if(downloadItem.state === WebEngineDownloadItem.DownloadCompleted){console.log("下载成功 ");dialogText.text = "下载成功,地址为:" + savePath;downloadCompleteDialog.open();}else if(downloadItem.state === WebEngineDownloadItem.DownloadCancelled){console.log("下载失败");}}}}Dialog {id: downloadCompleteDialog;title: "下载通知";standardButtons: Dialog.Ok;anchors.centerIn: parent;property var downloadItem: null;onAccepted: {console.log("Dialog accepted");}onRejected: {console.log("Dialog closed");}Text {id: dialogText;anchors.margins: 10;anchors.fill: parent;text: "下载信息将在这里显示";}}}

这样修改完成后,就能够进行下载
file

使用选择文件夹的方式下载

qml中的filedialog无法卡住程序因此无法使用,dowmITemonDownloadRequested这个信号中如果不调用accept就会销毁,外边也无法拿到,我们需要使用c++类的方式.
新建一个c++类SelectDownloadFolder
.h 把拿到文件夹的函数暴露给qml

#ifndef SELECTDOWNLOADFOLDER_H
#define SELECTDOWNLOADFOLDER_H#include <QObject>
#include <QFileDialog>
#include <QString>class SelectDownloadFolder : public QObject
{Q_OBJECT
public:explicit SelectDownloadFolder(QObject *parent = nullptr);Q_INVOKABLE QString getSelectDownloadFolder();
signals:
};#endif // SELECTDOWNLOADFOLDER_H

.cpp 使用c++的filedialog来打开文件夹卡住程序

#include "selectdownloadfolder.h"SelectDownloadFolder::SelectDownloadFolder(QObject *parent): QObject{parent}
{}QString SelectDownloadFolder::getSelectDownloadFolder()
{QString savepath = QFileDialog::getExistingDirectory(nullptr,QString(" 选择文件夹 "),QString(),QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);return savepath;
}

写完类后需要注册这个c++给qml
main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "selectdownloadfolder.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication app(argc, argv);QQmlApplicationEngine engine;const QUrl url(QStringLiteral("qrc:/main.qml"));// 注册类并创建实例qmlRegisterType<SelectDownloadFolder>("com.mycompany", 1, 0, "SelectDownloadFolder");QObject::connect(&engine,&QQmlApplicationEngine::objectCreated,&app,[url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl)QCoreApplication::exit(-1);},Qt::QueuedConnection);engine.load(url);return app.exec();
}

然后就可以在qml中调用,这样我们就可以拿到选择的路径,在路径选择完之后才会开始下载.

import com.mycompany 1.0SelectDownloadFolder{id:selectedfolder}savePath = selectedfolder.getSelectDownloadFolder();

file


http://www.mrgr.cn/news/57477.html

相关文章:

  • 计算机网络(十二) —— 高级IO
  • 简单的windows java -jar 无法启动jar包解决方法
  • 转变软件交付方式:通过统一 API 和测试策略提高质量和速度
  • Selenium自动化测试工具
  • The Open Group 2024年度大会大咖来袭
  • RS CMW100无线通信生产测试仪
  • 关于搜索接口被攻击,如何优化思路
  • 背包九讲——混合背包问题
  • 华为OD机试真题---We Are A Team
  • paddleocr使用FastDeploy 部署工具部署 rknn 模型
  • 智能扭矩系统Torque在新能源领域的应用_SunTorque
  • threejs中的小案例
  • autMan奥特曼机器人-出现argument list too long报错的解决方法
  • 哈希——哈希的基本概念
  • 两个开源AI应用让Claude 3.5 直接操作你的电脑;构建和部署多智能体系统课程;简化PDF文档管理并提供智能聊天功能
  • 通过运行窗口呼出Windows功能的快捷命令集合
  • Swarm集群管理常用命令与详解
  • 在 Spring 框架中,@ComponentScan` 扫描的注解类型
  • Bros!使用 focus 和 blur 事件时别忽略了这一点!
  • CentOS 6 修改 yun 源
  • 【Linux】 su 和 sudo 的区别剖析
  • C#,自动驾驶技术,ASAM OpenDRIVE BS 1.8.0 规范摘要与C# .NET Parser
  • 农业自动气象监测站的工作原理
  • 深入解析MySQL数据库:从基础到进阶的全面剖析
  • 哥德巴赫猜想渐行渐远
  • 《1024:致敬程序员的数字乐章》