Skip to content Skip to sidebar Skip to footer

How To Show On Qml (qt) From Sqlite Blob Data As Image?

My code is like below. Name - as TEXT field, Photo - as BLOB data class SqlQueryModel: public QSqlQueryModel { Q_OBJECT QHash *hash; public: expli

Solution 1:

You have three options:

  1. let the model hand out some ID and use that with a QQuickImageProvider
  2. let the model hand out a QImage and write a custom item that can display that
  3. let the model hand out the image data as a data URI

For (2) the simplest solution is a QQuickPaintedItem derived class, something like this

classQImageItem : public QQuickPaintedItem
{
    Q_OBJECT
    Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged)public:
    explicit QImageItem(QQuickItem *parent = Q_NULLPTR) : QQuickPaintedItem(parent) {}

    QImage image()const{ return m_image; }
    voidsetImage(const QImage &image);

    voidpaint(QPainter *painter) Q_DECL_OVERRIDE;

private:
    QImage m_image;
};

voidQImageItem::setImage(const QImage &image){
    m_image = image;
    emit imageChanged();
    update();

    setImplicitWidth(m_image.width());
    setImplicitHeight(m_image.height());
}

voidQImageItem::paint(QPainter *painter){
    if (m_image.isNull()) return;

    painter.drawImage(m_image.scaled(width(), height()));
}

Register as usual with qmlRegisterType<QImageItem>("SomeModuleName", 1, 0, "SomeTypeName") and in QML import SomeModuleName 1.0 and use SomeTypeName in instead of Image, with the QImage returned by the model bound to the item's image property

Post a Comment for "How To Show On Qml (qt) From Sqlite Blob Data As Image?"