Results 1 to 6 of 6

Thread: How to return pixels from QPixmap to QML

  1. #1
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to return pixels from QPixmap to QML

    Hello, How would one return Image bits to QML from C++?

    I have this code in c++:
    ....
    Qt Code:
    1. QImage image = m_pixmap.copy(sx, sy, sw, sh).toImage();
    2.  
    3. image = image.convertToFormat(QImage::Format_ARGB32);
    4. QByteArray bytes((char *) image.bits(), image.byteCount());
    5.  
    6. return ?????
    To copy to clipboard, switch view to plain text mode 
    How would I package up the QByteArray properly and return the data to QML in rgba format, so it can be manipulated on the QML side?

    Thanks

  2. The following 3 users say thank you to cmessineo for this useful post:

    ebirdseystew (18th December 2013)

  3. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to return pixels from QPixmap to QML

    Have a look at QQuickImageProvider


    And if you want to edit image,, you better have the manipulation code in C++ and call that from QML... dont keep processing on QML side.

  4. The following 3 users say thank you to aamer4yu for this useful post:

    ebirdseystew (18th December 2013)

  5. #3
    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: How to return pixels from QPixmap to QML

    Quote Originally Posted by cmessineo View Post
    Hello, How would one return Image bits to QML from C++?

    I have this code in c++:
    ....
    Qt Code:
    1. QImage image = m_pixmap.copy(sx, sy, sw, sh).toImage();
    2.  
    3. image = image.convertToFormat(QImage::Format_ARGB32);
    4. QByteArray bytes((char *) image.bits(), image.byteCount());
    5.  
    6. return ?????
    To copy to clipboard, switch view to plain text mode 
    How would I package up the QByteArray properly and return the data to QML in rgba format, so it can be manipulated on the QML side?

    Thanks
    Qt Code:
    1. class ImageManipulator : public QObject {
    2. public:
    3. ImageManipulator(QObject *parent = 0) : QObject(parent) {}
    4. void setImage(const QImage &img) { m_image = img; }
    5. QImage image() const { return m_image; }
    6.  
    7. Q_INVOKABLE QColor pixel(int x, int y) const { return QColor(m_image.pixel(x, y)); }
    8. Q_INVOKABLE void setPixel(int x, int y, QColor c) { m_image.setPixel(x, y, c); }
    9. private:
    10. QImage m_image;
    11. };
    To copy to clipboard, switch view to plain text mode 
    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.


  6. The following 3 users say thank you to wysota for this useful post:

    ebirdseystew (18th December 2013)

  7. #4
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to return pixels from QPixmap to QML

    I don't want to provide an image I just want to provide an array of pixels that can be updated on the qml side, then I will send the pixels back to c++ and update the image. I just don't know how to send the initial image pixel array to qml.

    I'm updating the Qt Labs Canvas control

    https://qt.gitorious.org/qt-labs/qml...17e36883c8a03:

    The getImageData and putImageData functions were never implemented for the context2d class.

    Thanks

  8. The following 3 users say thank you to cmessineo for this useful post:

    ebirdseystew (18th December 2013)

  9. #5
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to return pixels from QPixmap to QML

    I was able to figure it out in case anyone needs to this. I had to use a QVariantList, and I'm not sure why but the image.bits() came out in BGRA format so I had to switch it around in the loop.

    Qt Code:
    1. QImage image = m_pixmap.copy(sx, sy, sw, sh).toImage();
    2. image = image.convertToFormat(QImage::Format_ARGB32);
    3.  
    4. uchar *bits = image.bits();
    5.  
    6. QVariantList pixels;
    7.  
    8. for (int i = 0; i < image.byteCount();)
    9. {
    10. pixels << bits[i+2] << bits[i+1] << bits[i] << bits[i+3];
    11. i += 4;
    12. }
    13.  
    14. return pixels;
    To copy to clipboard, switch view to plain text mode 

  10. The following 3 users say thank you to cmessineo for this useful post:

    ebirdseystew (18th December 2013)

  11. #6
    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: How to return pixels from QPixmap to QML

    Quote Originally Posted by cmessineo View Post
    I don't want to provide an image I just want to provide an array of pixels that can be updated on the qml side, then I will send the pixels back to c++ and update the image.
    That's a totally inefficient approach for QML. Don't try to mimic APIs that are not meant for a particular environment.


    I'm updating the Qt Labs Canvas control

    https://qt.gitorious.org/qt-labs/qml...17e36883c8a03:

    The getImageData and putImageData functions were never implemented for the context2d class.
    The Canvas element is part of QtQuick 2. It has getImageData and putImageData implemented. Bear in mind using these functions in QtQuick 2 probably triggers a slow code path in Canvas though.
    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.


  12. The following 3 users say thank you to wysota for this useful post:

    ebirdseystew (18th December 2013)

Similar Threads

  1. Replies: 1
    Last Post: 2nd January 2013, 10:48
  2. Replies: 4
    Last Post: 28th August 2008, 14:13
  3. Replies: 1
    Last Post: 21st August 2008, 08:44
  4. QPixmap replace white pixels by blacks
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2008, 06:12
  5. Replies: 5
    Last Post: 9th April 2007, 15:26

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.