BUG记:new/delete

气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了气死了
写程序得脑子清醒,不清醒,写错一丢丢就会导致一晚上都在Debug!!!
而我!今天!就是!
错误就出现在
T* element = new T(size)T* element = new T[size]的区别
气死了!我以为自己写了个数组,还用delete []导致一运行到最后就中断了!
不过今天也学习了一下这个new 和delete,详细看这个大佬的博客浅谈 C++ 中的 new/delete 和 new[]/delete[]
再见!

import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.2 import FileManage 1.0 import Theme 1.0 import "../general" Rectangle { id:photoRect visible: true width: parent.width height: parent.height color: Theme.surfaceColor//"black"//"#2F2F2F" signal updateImagsSig() signal sendImagsSig(var imgIndex,var imgSource) signal backToImageSig() signal updateRectSig() // 关键优化1:使用ListModel替代普通数组(支持数据变化通知) ListModel { id: imageModel // 替代原images数组,作为GridView的模型 } FileManage { id: fileManage } // ====================== 新增:删除确认弹窗 ====================== Rectangle { id: deleteConfirmDlg visible: false // 默认隐藏,通过按钮点击显示 width: 580 // 固定宽度 height: 300 // 固定高度 anchors.centerIn: parent // 居中显示 color: Theme.backgroundColor//"#2F2F2F" // 深灰色背景 radius: 10 // 圆角 border.color: Theme.borderPrimary//"#666" border.width: 1 z:2 // 半透明遮罩(覆盖整个窗口,防止点击其他区域) Rectangle { id: modalMask anchors.fill: parent color: Theme.surfaceColor//"black" opacity: 0.5 visible: deleteConfirmDlg.visible // 与弹窗同步显示 } // 弹窗内容 ColumnLayout { anchors.fill: parent anchors.margins: 20 spacing: 20 // 提示文本 Text { text: "Confirm to delete the selected images?" color: Theme.textPrimary//"white" font.pointSize: 24 font.family: "等线 Light" horizontalAlignment: Text.AlignHCenter } // 按钮行 RowLayout { Layout.fillWidth: true spacing: 50 Layout.alignment: Qt.AlignHCenter // 按钮居中排列 // 取消按钮(修复color属性错误) Button { text: "Cancel" Layout.preferredWidth: 140 font.pointSize: 30 font.family: "等线 Light" // 关键修复:通过background属性设置背景色 background: Rectangle { anchors.fill: parent radius: 5 // 圆角 color: Theme.buttonCancel //"#666" // 原color属性移至此处 border.color: Theme.buttonCancelBorder//"#444" // 边框颜色(可选) border.width: 1 } // 设置文本颜色(避免与背景色冲突) contentItem: Text { text: parent.text font: parent.font color: Theme.textPrimary//"white" // 白色文本 horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter } onClicked: { deleteConfirmDlg.visible = false delBtn.currentIndex = 0; } } // 确认按钮(同样修复color属性错误) Button { text: "Confirm" Layout.preferredWidth: 140 font.pointSize: 30 background: Rectangle { anchors.fill: parent radius: 5 color: Theme.buttonConfirm//"#E74C3C" // 红色背景 border.color: Theme.buttonConfirmBorder//"#B03A2E" // 深边框(可选) border.width: 1 } contentItem: Text { text: parent.text font: parent.font color: Theme.textPrimary//"white" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter } onClicked: { deleteSelectedImages() deleteConfirmDlg.visible = false delBtn.currentIndex = 0 } } } } } // ====================== 弹窗结束 ====================== // Main container Rectangle { id:pRect anchors.fill: parent color: Theme.surfaceColor//"black" anchors.topMargin: 100 anchors.leftMargin: 20//140 width: 1760 property var itemScale: 1 // 新增:布局缩放相关属性 property var currentCellWidth: 350 // 动态单元格宽度(初始350) property var minCellWidth: 150 // 最小单元格宽度(控制最大列数) property var maxCellWidth: 600 // 最大单元格宽度(控制最小列数) property var baseCellWidth: 350 // 手势起始时的基准单元格宽度 RowLayout { anchors.fill: parent spacing:0 // GridView(关键优化:绑定ListModel) GridView { id: imageGridView Layout.fillWidth: true Layout.fillHeight: true // 关键修改:cellWidth 绑定到 currentCellWidth(cellHeight 可保持等比或固定) cellWidth: pRect.currentCellWidth*pRect.itemScale cellHeight: pRect.currentCellWidth*pRect.itemScale // 保持等比(若图片为方形),或根据需求调整(如 currentCellWidth * 1.5) flow: GridView.FlowLeftToRight model: imageModel //interactive: !pinchArea.active // 缩放时禁用滚动 delegate: Item { // 原有 delegate 保持不变(已绑定 GridView.view.cellWidth) id: deleItem width: GridView.view.cellWidth // 自动适配 cellWidth 变化 height: GridView.view.cellHeight // 自动适配 cellHeight 变化 Rectangle { id: rect1 layer.enabled: true // 启用GPU层(关键优化) layer.smooth: true // 平滑缩放 // 调整rect1的尺寸(基于父Item的宽高) width: parent.width //- 50 // parent.width 是Item的width(即GridView.view.cellWidth) height: parent.height// - 50 // parent.height 是Item的height(即GridView.view.cellHeight) color: Theme.gridViewUnSelected//"white" // 显式绑定border.color(关键优化) property bool selected: model.selected signal changeStatus(bool value)//Yun radius: 2 //border.width: 10 //border.color: "white" // anchors.fill:parent Binding { target: rect1 property: "border.color" value: selected ? Theme.gridViewSelected : Theme.gridViewUnSelected } // 显式绑定border.width(关键优化) // Binding { //target: rect1 //property: "border.width" //value: selected ? 12 : 10 // } Timer{ id:timer3 repeat: false interval: 10 onTriggered: { rect1.border.color = selected ? Theme.gridViewSelected : Theme.gridViewUnSelected //rect1.border.width = selected ? 12 : 10 mouseArea1.update() } } function changeStatusFun(value)//Yun { rect1.border.color = value ? Theme.gridViewSelected : Theme.gridViewUnSelected //rect1.border.width = value ? 12 : 10 rect1.update() } Row { visible: true x:15 y:300 width: 50 height: rect1.height spacing: 40 z:2 TagItem{ id:infoTag //x:10 //y:10 width: 32 height: 32 visible: true label: "I" z:2 Component.onCompleted: { //console.info(model.index) var isExist = checkInfoRGBX(model.index,"/Info/") isExist.Info? switchToGreen():switchToWhite() } } TagItem{ id:jpgTag // x:10 //y:80 width: 32 height: 32 visible: true label:"P" z:2 Component.onCompleted: { //console.info(model.index) var isExist = checkInfoRGBX(model.index,"/Rgb/") isExist.Rgb? switchToGreen():switchToWhite() } } TagItem{ id:xTag //x:10 // y:160 width: 32 height: 32 label:"X" visible: true z:2 Component.onCompleted: { //console.info(model.index) var isExist = checkInfoRGBX(model.index,"/X/") isExist.X? switchToGreen():switchToWhite() } } } Image { id: deteSingleImage anchors.fill: parent anchors.margins:model.selected?1:0.5; cache: true // 启用缓存(关键优化) asynchronous: true source: model.source + "?t=" + Date.now() // 若需强制刷新,可保留时间戳 fillMode: Image.PreserveAspectCrop // sourceSize.width: parent.width // 限制解码宽度(关键优化) // sourceSize.height: parent.height // 限制解码高度(关键优化) } // 选中遮罩(关键优化:visible直接绑定selected状态) // Rectangle { // id: selectMask // width: parent.width // height: parent.height // color: "#00FF00" // opacity: 0.3 // visible: rect1.selected // 单张图独立控制可见性 // } MouseArea { id:mouseArea1 anchors.fill: parent property int clickInterval: 500 // 延长双击间隔(可选优化) property int lastClickIndex: -1 // 录上次点击的索引 property int lastClickTime: 0 // 录上次点击时间 onClicked: { var now = Date.now() if (lastClickIndex !== model.index && (now - lastClickTime) < clickInterval) { mouse.accepted = true } else { // 修改选中状态 selected = model.selected imageModel.setProperty(model.index, "selected", !model.selected) } lastClickIndex = model.index lastClickTime = now //timer3.restart() rect1.changeStatus(selected) // 强制触发rect1重绘(关键修复) deleItem.update() // 触发当前delegate的重绘 //scrollToTop() //updateSelection() } onDoubleClicked: { // 仅当两次点击目标相同时触发双击(关键修复) if (lastClickIndex === model.index) { photoRect.sendImagsSig(model.index,model.source) } } // onDoubleClicked: { // photoRect.sendImagsSig(model.index) // 传递ListModel中的索引 // console.log("onDoubleClicked****************************") // } // onClicked: { // // 关键优化4:通过ListModel的setProperty修改属性(触发局部刷新) // imageModel.setProperty(model.index, "selected", !model.selected) // } } Component.onCompleted: { rect1.changeStatus.connect(rect1.changeStatusFun)//Yun } } } ScrollBar.vertical: ScrollBar { width: 20 active: true interactive: true } } } // PinchArea { // id: pinchArea // anchors.fill: parent // // 允许缩放(默认已启用,可显式设置) // enabled: true // // 最小/最大缩放限制(可选) // pinch.minimumScale: 0.5 // 最小缩放比例 // pinch.maximumScale: 3 // 最大缩放比例 // // 手势更新时触发 // onPinchUpdated: { // // 当前双指缩放比例(相对于手势开始时的比例) // var currentScale = pinchArea.scale // console.log("当前缩放比例:", currentScale) // // 同步更新目标元素的缩放 // pRect.itemScale = currentScale // } // } // // 新增:双指缩放手势检测区域 // PinchArea { // id: pinchArea // anchors.fill: parent // 覆盖整个 GridView 区域 // property real baseScale: 1.0 // 手势起始时的基准缩放值 // onPinchStarted: { // baseScale = scale // 录手势开始时的初始缩放值 // pRect.baseCellWidth = pRect.currentCellWidth // 录手势开始时的基准 cellWidth // } // onScaleChanged: { // // 计算缩放比例(当前缩放 / 基准缩放) // var scaleFactor = scale / baseScale // // 动态调整 currentCellWidth(基准宽度 * 缩放比例) // var newCellWidth = pRect.baseCellWidth * scaleFactor // // 限制在 min/max 范围内(确保列数合理) // pRect.currentCellWidth = Math.max(pRect.minCellWidth, Math.min(pRect.maxCellWidth, newCellWidth)) // } // onPinchFinished: { // // 可选:手势结束后,将 cellWidth 对齐到“能整除容器宽度”的值(避免列数边缘留白) // var containerWidth = imageGridView.width // var idealColumns = Math.round(containerWidth / pRect.currentCellWidth) // pRect.currentCellWidth = containerWidth / idealColumns // 强制列数为整数 // } // } // // 新增:平滑过渡动画(优化布局变化体验) // NumberAnimation on currentCellWidth { // duration: 200 // easing.type: Easing.InOutQuad // } } NewImageBtn{ id: delBtn x:1800 y:10 width: 100 height: 100 z:2 text:"" //border.color: "black" // border.width: 3 radius: 10 images:[Theme.deleteDarkChange,"../../../res/images/delete_on.png"]/*{ var arr = new Array; arr.push("../../../res/images/delete_off.png"); arr.push("../../../res/images/delete_on.png"); return arr; }*/ onCurrentIndexChanged: { if(currentIndex === 1) { deleteConfirmDlg.visible = true // 点击显示弹窗 } } } NewImageBtn{ id: addnoteBtn x:1800 y:150 width: 100 height: 100 z:2 text:"" //border.color: "black" // border.width: 3 radius: 10 images:[Theme.addnoteDarkChange,"../../../res/images/addnote_on.png"] // onCurrentIndexChanged: { // if(currentIndex === 1) // { // deleteConfirmDlg.visible = true // 点击显示弹窗 // } // } } // MouseArea{ // id : backArea // height: 50 // width: photoRect.width // anchors.top: photoRect.top // enabled: false // property var firstPosY: 100 // onPressed: { // firstPosY = mouseY // } // onReleased: { // var currentMouseY = mouseY // if(currentMouseY>300&&firstPosY<50) // { // photoRect.backToImageSig() // } // } // } CustomBackDlg { id:toPhotosDlg height: 100 width: 800 x:photoRect.width/2-width/2-91 y:5 normalImage: "../../../res/images/line_off.png" // 未选中时的星星图标 checkedImage: "../../../res/images/back2_on.png" // 选中时的填充星星图标 iconWidth: 64 iconHeight: 64 checked: false // 初始状态 opacity: 1 imageRotaAngle: 0 desc: ""//"ShootingWindow" textUpPos :0 function backToImageAlbum() { photoRect.backToImageSig(); } Component.onCompleted: { toPhotosDlg.backSig.connect(toPhotosDlg.backToImageAlbum) } // id:toPhotosDlg // height: 90 // width: 800 // x:photoRect.width/2-width/2 // y:photoRect.height-toPhotosDlg.height // normalImage: "../../../res/images/back2_off.png" // 未选中时的星星图标 // checkedImage: "../../../res/images/back2_on.png" // 选中时的填充星星图标 // iconWidth: 64 // iconHeight: 64 // checked: false // 初始状态 // opacity: 1 // imageRotaAngle: 180 // desc: "ShootingWindow" // textUpPos :1 // function backToImageAlbum() // { // photoRect.backToImageSig(); // //console.log("backToPhotoAlbum") // } // Component.onCompleted: { // toPhotosDlg.backSig.connect(toPhotosDlg.backToImageAlbum) // // toPhotosDlg.backSig.connect(imageArea.showDetecSig) // } } Component.onCompleted: { photoRect.updateImagsSig(); infoHandle.refreshPhotoAblumSignal.connect(photoRect.deleteImage) } // 新增:控制滚动到顶部的函数 function scrollToTop() { imageGridView.contentY = 0; // 设置垂直滚动偏移量为0(回到顶部) } // 重置选中状态(优化为遍历ListModel) function resetSelection() { for (var i = 0; i < imageModel.count; i++) { imageModel.setProperty(i, "selected", false) } } function updateSelection() {//Yun for (var i = 0; i < imageModel.count; i++) { if(imageModel.get(i).selected) { imageModel.setProperty(i, "selected", false) imageModel.setProperty(i, "selected", true) console.log(i,imageModel.get(i).selected) } } } // 删除选中项(优化为反向遍历ListModel) function deleteSelectedImages() { // 反向遍历避免索引错乱(ListModel删除后索引自动调整) var rest = true for (var i = imageModel.count - 1; i >= 0; i--) { if (imageModel.get(i).selected) { console.log(imageModel.get(i).source) rest &= deleteFiles(imageModel.get(i).source) if(rest) imageModel.remove(i) else return rest } } return rest } // 删除指定名称的文件 function deleteFiles(filePath) { console.log(filePath) var fileInfo = splitFilePath(filePath) //console.log("=================") //console.log(fileInfo.directory) //console.log(fileInfo.fileName) var result = fileManage.deleteFilesWithName(fileInfo.directory, fileInfo.fileName); if (result) { console.log("Files deleted successfully"); } else { console.log("Some files failed to delete"); } return result } function setImages(values) { imageModel.clear(); // 清空原有数据 if (!values || !Array.isArray(values)/*||(rgbPhoto.length!==values.length)*/) { return; } // 遍历数组并添加元素到ListModel for (var i = 0; i < values.length; i++) { var path = values[i]; if (typeof path === "string") { imageModel.append({ // 向ListModel添加元素(自动触发模型更新) source: path, selected: false, //selected: false }); } } } function deleteImage(value) { // 反向遍历避免索引错乱(ListModel删除后索引自动调整) for (var i = imageModel.count - 1; i >= 0; i--) { if (imageModel.get(i).source.indexOf(value) !== -1) { imageModel.remove(i) } } } // 分离路径的函数 function splitFilePath(filePath) { if (!filePath) return { directory: "", fileName : "" }; var path = filePath.toString(); if (path.startsWith("file:///")) { path = path.substring(8); // 移除 file:/// } else if (path.startsWith("file://")) { path = path.substring(7); // 移除 file:// } // 找到最后一个斜杠或反斜杠的位置 var lastSlash = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")); if (lastSlash === -1) { return { directory: "", fileName : path }; } return { directory: getParentDirectory(path), fileName : getFileNameWithoutExtension(path.substring(lastSlash + 1)) }; } // 获取文件扩展名 function getFileExtension(fileName) { var lastDot = fileName.lastIndexOf("."); if (lastDot === -1) return ""; return fileName.substring(lastDot + 1); } // 获取不带扩展名的文件名 function getFileNameWithoutExtension(fileName) { var lastDot = fileName.lastIndexOf("."); if (lastDot === -1) return fileName; return fileName.substring(0, lastDot); } // 获取父级目录(倒数第二个斜杠前的内容) function getParentDirectory(filePath) { if (!filePath) return ""; // 标准化路径 var path = normalizePath(filePath.toString()); // 找到所有斜杠和反斜杠的位置 var slashPositions = []; for (var i = 0; i < path.length; i++) { if (path[i] === '/' || path[i] === '\\') { slashPositions.push(i); } } // 如果没有斜杠或只有一个斜杠,返回空字符串 if (slashPositions.length < 2) { return ""; } // 获取倒数第二个斜杠的位置 var secondLastSlash = slashPositions[slashPositions.length - 2]; return path.substring(0, secondLastSlash + 1); } function normalizePath(path) { if (path.startsWith("file:///")) { return path.substring(8); } else if (path.startsWith("file://")) { return path.substring(7); } else if (path.startsWith("qrc:/")) { return path.substring(4); } return path; } function checkInfoRGBX(i,dir) { var filePath = imageModel.get(i).source var parentPath = getParentDirectory(filePath) var fileName = splitFilePath(filePath).fileName if(dir === "/Info/") fileName += ".txt"// need confirm else if(dir === "/Rgb/") fileName += ".jpg" // need confirm else if(dir === "/X/") fileName += ".x"// need confirm //console.info(parentPath+dir+fileName) return { Info: fileManage.isFileExist(parentPath+dir+fileName), Rgb: fileManage.isFileExist(parentPath+dir+fileName), X: fileManage.isFileExist(parentPath+dir+fileName) }; } // Component to add more functionalities (like back button, etc.) can be added here } 如何在Linux环境下,利用QT5.6的QML实现imageGridView内每个元素的缩放,通过双指缩放,但是不能与imageGridView内元素的MouseArea事件冲突
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值