• Qt 获取QGraphicsScene的缩略图即导出到图片


    是应用了他的render函数,render的作用是:

    Renders the source rect from scene into target, using painter. This function is useful for capturing the contents of the scene onto a paint device, such as a QImage (e.g., to take a screenshot), or for printing with QPrinter. For
    If source is a null rect, this function will use sceneRect() to determine what to render. If target is a null rect, the dimensions of painter's paint device will be used.
    The source rect contents will be transformed according to aspectRatioMode to fit into the target rect. By default, the aspect ratio is kept, and source is scaled to fit in target.
     
    代码如下:
    1         //get thumbnail
    2         QImage image(130 * mSceneSize.width()/ mSceneSize.height(),130 ,QImage::Format_ARGB32);   
    3         QString pngName = currentPageID+"_scene.png";
    4         QPainter painter(&image);
    5         painter.setRenderHint(QPainter::Antialiasing);
    6         scene->render(&painter);   
    7         bool saveSuccess =  image.save(pngName);  
    8         Q_ASSERT(saveSuccess == true);
    有两个注意事项:
    • 如果你把QImage image(130 * mSceneSize.width()/ mSceneSize.height(),130 ,QImage::Format_ARGB32);   写成QImage image。无论如何image都是无法save成功的。因为image没有初始化。
    • 如果你的scene里有的item/widget的坐标位于目前可显示的外面,意思是你的scene现在大小是100*100,但是有一个item的坐标位于100*101,那么调用scene->render时会有ASSERT错误:
      ---------------------------
      Microsoft Visual C++ Debug Library
      ---------------------------
      Debug Error!
       
      Program: ...Win32DebugMaker.exe
      Module: 5.0.1
      File: globalqglobal.cpp
      Line: 1951
       
      ASSERT: "!item->d_ptr->itemDiscovered" in file graphicsviewqgraphicsscenebsptreeindex.cpp, line 343
       
      (Press Retry to debug the application)
      ---------------------------
      Abort   Retry   Ignore   
      ---------------------------
      你当然可以忽略掉他,在release模式下是没有此类问题的。
     
    当然了你用QGraphicsView的render方法也可以获得view的缩略图。
  • 相关阅读:
    2020最新JavaScript开发必须知道的41个技巧,你会几个?
    Vue项目上线要做哪些优化?面试必学
    javascript 关于dom节点操作的增删改查
    uniapp 上拉加载下拉刷新
    移动WEB适配布局
    微信小程序中封装网络请求方法
    react兄弟组件传值
    【python】Mutilindex多层索引的取值
    【python】通过Mutilindex生成二维表数据
    【python】python vs Excel ( 如何通过loc与iloc函数处理Excel数据)
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14419200.html
Copyright © 2020-2023  润新知