首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

QML+opencv实现ui动态查看图片和闪退问题解决

  • 25-02-19 09:41
  • 2051
  • 8626
blog.csdn.net

近期学习使用QML和制作一些UI界面,需要动态的进行图片显示指定文件夹内的图片。CSDN有一些其他推荐跟着写了写,发现还是有些坑存在。特此记录。

 参考了一些方法,比如

踩坑记录:QML加载图片资源_qml 怎么读取图片的exif-CSDN博客

静态加载无法实现实时改变显示的图片内容。

qml中动态加载图片(Image与QQuickImageProvider)-CSDN博客

我们需要使用

QQuickImageProvider
The QQuickImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML.
继承QQuickImageProvider类来实现qml中动态加载图片的方法.

目录

paintitem.h

paintitem.cpp  

main函数注册类到qml:

qml文件中:

第一个坑: 使用方法

第二个坑:内存溢出:

最后成功运行


paintitem.h

其中我还需要一些对图片的处理,使用了opencv库,有同样需要可以参考。

  1. #define PAINTITEM_H
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. class PaintItem : public QQuickPaintedItem
  11. {
  12.     Q_OBJECT
  13. public:
  14.     explicit PaintItem(QQuickItem *parent = nullptr);
  15.     cv::Mat get_cutted_cvimage(const cv::Mat& image, int weight_start, int weight_end, int height_start, int height_end);
  16.     Q_INVOKABLE void show_image_test(int imageIndex, qreal width, qreal height);
  17. public slots:
  18.     void updateImage(const QImage &image);
  19. protected:
  20.     void paint(QPainter *painter) override;
  21. private:
  22.     QImage m_imageThumb;
  23.     QString folderPath = "/image";
  24.     int defaultImageIndex = 1;
  25. };
  26. extern PaintItem *ptr_paint_image;
  27. #endif // PAINTITEM_H

paintitem.cpp  

  1. #include "paintitem.h"
  2. #include
  3. #include
  4. PaintItem::PaintItem(QQuickItem *parent) : QQuickPaintedItem(parent)
  5. {
  6. }
  1. //更新图片
  2. void PaintItem::updateImage(const QImage &image)
  3. {
  4. m_imageThumb = image;
  5. update(); //这是库函数
  6. }
  7. //重要函数,qml中显示图片是用该函数
  8. void PaintItem::paint(QPainter *painter)
  9. {
  10. if (!m_imageThumb.isNull()) {
  11. QRectF targetRect = boundingRect();
  12. painter->drawImage(targetRect, m_imageThumb, m_imageThumb.rect()); // Draw without scaling again
  13. }
  14. else
  15. QuaeroDebug() << "invalied painted image --- NULL";
  16. }
  1. //这个函数是qml中调用,实现图片的格式转换等主要工作。其中get_cutted_cvimage是我的图像处理函数,删掉即可。(已删除)
  2. void PaintItem::show_image_test(int imageIndex, qreal width, qreal height)
  3. {
  4. try{
  5. QString fileName = QString("%1/cell%2.png").arg(folderPath).arg(imageIndex);
  6. cv::Mat image = cv::imread(fileName.toStdString());
  7. if (image.empty()) {
  8. QuaeroDebug() << "invalied painted image --- NULL";
  9. return;
  10. }
  11. // 如果 width 或 height 为 -1,则使用图像的原始尺寸
  12. if (width == -1 || height == -1) {
  13. width = image.cols;
  14. height = image.rows;
  15. }
  16. cv::Mat resizedImage;
  17. cv::resize(image, resizedImage, cv::Size(width, height), 0, 0, cv::INTER_AREA);
  18. cv::cvtColor(resizedImage, resizedImage, cv::COLOR_BGR2RGB);
  19. QImage qImage((const uchar*)resizedImage.data, resizedImage.cols, resizedImage.rows, resizedImage.step, QImage::Format_RGB888);
  20. updateImage(qImage);
  21. }
  22. catch(const std::exception& err)
  23. {
  24. QuaeroDebug() << err.what();
  25. }
  26. }

main函数注册类到qml:

几个关键部分

  1. QQmlApplicationEngine* engine = new QQmlApplicationEngine();
  2. ptr_paint_image = new PaintItem();
  3. qmlRegisterType("PaintItem",1,0,"PaintItem");
  4. engine->rootContext()->setContextProperty("paint_image_show",ptr_paint_image);
  5. const QUrl url(QStringLiteral("qrc:/main.qml"));
  6. QObject::connect(engine, &QQmlApplicationEngine::quit, &app, &QGuiApplication::quit);
  7. engine->load(url);

qml文件中:

第一个坑: 使用方法

注意PaintItem,用他定义的实例ID来显示加载图片。

  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.5
  3. import QtQml.Models 2.15
  4. import PaintItem 1.0
  5. import "./ctrl_item"
  6. //省略页面其他,以及定义property int image_index: 1
  7. Rectangle {
  8. id: rectangle_opencv_lightimage_show
  9. border.color: $main_color
  10. border.width: 2
  11. anchors.top: parent.top
  12. anchors.topMargin: 80
  13. anchors.bottom: parent.bottom
  14. //anchors.bottomMargin: 20
  15. anchors.left: parent.left
  16. width: rectangle_opencv_lightimage_show.height
  17. color: $main_color
  18. radius:2
  19. PaintItem {
  20. id: paintItemDelegate //从这里进行下面的函数操作才可以渲染出图片
  21. anchors.fill: parent
  22. }
  23. MultiPointTouchArea {
  24. id: imagshow_mousearea
  25. anchors.fill: parent
  26. onPressed: {
  27. }
  28. }
  29. }//rectangle_opencv_lightimage_show
  30. M_button {
  31. id: button_last_pic
  32. anchors.top: rectangle_datahandle .bottom
  33. anchors.topMargin: 20
  34. anchors.left: rectangle_datahandle.left
  35. anchors.leftMargin: 50
  36. bt_width: 180
  37. bt_height: 56
  38. img_source: $icon_base + "insert_up.png"
  39. bt_txt: qsTr("last_pic")
  40. onClickedLeft: {
  41. if(image_index === 1)
  42. image_index = 5;
  43. else
  44. image_index --;
  45. paintItemDelegate.show_image_test(image_index,rectangle_opencv_lightimage_show.width-50,rectangle_opencv_lightimage_show.height-50);
  46. console.log("image_is:",image_index);
  47. }
  48. }
  49. Component.onCompleted: {
  50. paintItemDelegate.show_image_test(image_index, rectangle_opencv_lightimage_show.width-50, rectangle_opencv_lightimage_show.height-50);
  51. }

第二个坑:内存溢出:

发生了很奇怪的现象,就是我进入显示页面第一次点击就会跳出,如果退出再进入,就会正常轮流显示,很奇怪。最后解决方法:减小尺寸。如果直接给控件宽度和高度的尺寸,会溢出。必须减去一些值才可以正常显示,我这里给的是-50之后正常显示。
 paintItemDelegate.show_image_test(image_index, rectangle_opencv_lightimage_show.width-50, rectangle_opencv_lightimage_show.height-50);

最后成功运行

注:本文转载自blog.csdn.net的Xuchus的文章"https://blog.csdn.net/Xuchus/article/details/144540412"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

130
用户体验设计
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top