Results 1 to 3 of 3

Thread: Rendering a QQuickItem into a QImage (Qt6)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Location
    Walchwil
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Rendering a QQuickItem into a QImage (Qt6)

    Hi Qt Specialist,

    I'm really desperate to make it work, I try different approaches but nothing works like expected. And most of the examples found by Googling concern Qt5.

    First the Use Case: I have to do a color picker into a complex 2D scene, this scene is constructed with QSGNodes tree inside a QQuickItem. Of course, to make it more difficult, I have to pick the color without the drawing overlays and I should be able to pick outside of the item bounds.

    I try this:
    1. I create a simplified QQuickItem
    2. try to bind it to an offscreen window (setParentItem & QQuickRenderControl)
    3. add a render target (fromPaintDevice, I have no clue how to transform any 'kind' of textures into a QImage)
    3. resize
    4. render with polishItems, beginFrame, ....
    5. check the image

    I get this error:
    QQuickWindow: No render target (neither swapchain nor custom target was provided)

    Here is the code using Vulkan:
    Qt Code:
    1. QVulkanInstance* inst = new QVulkanInstance;
    2. inst->create();
    3.  
    4. _render_ctrl = new QQuickRenderControl;
    5.  
    6. // this window will never be shown on-screen
    7. _off_screen_window = new QQuickWindow(_render_ctrl);
    8. _off_screen_window->setVulkanInstance(inst);
    9.  
    10. if (!_render_ctrl->initialize())
    11. qWarning() << "failed to init";
    12.  
    13. _image = new QImage(300, 200, QImage::Format_RGBA8888_Premultiplied);
    14. _image->fill(qRgb(255,255,0));
    15.  
    16. _off_screen_window->setRenderTarget(QQuickRenderTarget::fromPaintDevice(_image));
    17.  
    18. _off_screen_item->setParentItem(_off_screen_window->contentItem());
    19. _off_screen_item->setSize(QSize(300,200));
    20. _off_screen_item->setVisible(true);
    21.  
    22. _off_screen_window->resize(300, 200);
    23. //_off_screen_window->show();
    24.  
    25. _render_ctrl->polishItems();
    26. _render_ctrl->beginFrame();
    27. _render_ctrl->sync();
    28. _render_ctrl->render();
    29. _render_ctrl->endFrame();
    30.  
    31. _image->save("xxx.png"); // nothing draw just the original image
    To copy to clipboard, switch view to plain text mode 

    Could someone explain to me how in Qt6 I could render offscreen a QQuickItem in a QImage? Or at least into a texture and how to obtain an average color from the extracted texture?

    Is the call QQuickWindow::setGraphicsApi(QSGRendererInterface: :Vulkan); compatible with the offscreen QQuickWindow? The Qt examples and documentation do not really cover this case.

    The only solution, for now, I'm using (slow - no shaders to help - and need to duplicate the rendering) is the QPainter over a QImage.

    Br,
    Bilbon

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rendering a QQuickItem into a QImage (Qt6)

    The docs claim you could redirect rendering in Vulkan to VkImage:

    Qt Code:
    1. QQuickRenderTarget rt = QQuickRenderTarget::fromVulkanImage(vulkanImage, VK_IMAGE_LAYOUT_PREINITIALIZED, pixelSize);
    2. quickWindow->setRenderTarget(rt);
    To copy to clipboard, switch view to plain text mode 

    Then you can either sample that image or try to convert it to some other image API (e.g. QImage)
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: Rendering a QQuickItem into a QImage (Qt6)

    Rendering a QQuickItem into a QImage in Qt6 can be achieved by redirecting rendering in Vulkan to a VkImage using QQuickRenderTarget. Have you tried using the suggested approach with QQuickRenderTarget::fromVulkanImage, and if so, what issues are you encountering in your implementation?

    Additionally, have you considered other methods or libraries to achieve your color picking in a complex 2D scene, or are you focused on solving this within the Qt framework?

Similar Threads

  1. QQuickItem draw text in C++
    By ecspertiza in forum Qt Quick
    Replies: 1
    Last Post: 21st November 2016, 15:55
  2. Replies: 1
    Last Post: 22nd December 2014, 18:42
  3. Extract QImage from QQuickItem
    By oberlus in forum Qt Quick
    Replies: 4
    Last Post: 20th September 2014, 11:42
  4. Replies: 8
    Last Post: 7th November 2012, 20:20
  5. Problem rendering QImage from Phonon Widget
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 12th July 2008, 16:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.