How to show on QML (Qt) from SQLite BLOB data as image?

社会主义新天地 提交于 2019-12-01 22:10:36

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

class QImageItem : 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; }
    void setImage(const QImage &image);

    void paint(QPainter *painter) Q_DECL_OVERRIDE;

private:
    QImage m_image;
};

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

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

void QImageItem::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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!