如何将 SQLite BLOB 数据中的 QML (Qt) 显示为图像?

How to show on QML (Qt) from SQLite BLOB data as image?(如何将 SQLite BLOB 数据中的 QML (Qt) 显示为图像?)
本文介绍了如何将 SQLite BLOB 数据中的 QML (Qt) 显示为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下.

名称 - 作为 TEXT 字段,

Name - as TEXT field,

照片 - 作为 BLOB 数据

Photo - as BLOB data

class SqlQueryModel: public QSqlQueryModel
{
    Q_OBJECT
    QHash<int,QByteArray> *hash;
public:
    explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent)
    {
        hash = new QHash<int,QByteArray>;
        hash->insert(Qt::UserRole,      QByteArray("Name"));
        hash->insert(Qt::UserRole + 1,  QByteArray("Photo"));
    }
    inline RoleNameHash roleNames() const { return *hash; }
};

选择数据

view = new QQuickView();
QSqlQueryModel *someSqlModel = new SqlQueryModel(this);
someSqlModel->setQuery("SELECT Name, Photo FROM some_table");
QQmlContext *context = view->rootContext();
context->setContextProperty("someSqlModel", someSqlModel);
view->setSource(QUrl("qrc:///MainView.qml"));
view->show();

QML 中的绑定

ListView {
    id: someListView
    model: SqlContactModel {}
    delegate: ItemDelegate {
        text: Name
        Image {
            id: Photo
            source: ???
        }
    }
}

如何将 SQLite BLOB 数据中的 QML (Qt) 显示为图像?

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

推荐答案

你有三个选择:

  1. 让模型分发一些 ID 并将其与 QQuickImageProvider
  2. 一起使用
  3. 让模型分发一个 QImage 并编写一个可以显示它的自定义项目
  4. 让模型将图像数据作为 数据 URI
  5. 分发
  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

对于(2),最简单的解决方案是QQuickPaintedItem派生类,类似这样

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()));
}

使用 qmlRegisterType("SomeModuleName", 1, 0, "SomeTypeName") 和 QML import SomeModuleName 1.0 像往常一样注册并使用 SomeTypeName 代替 Image,模型返回的 QImage 绑定到项目的 image 属性

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

这篇关于如何将 SQLite BLOB 数据中的 QML (Qt) 显示为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)
Android: Why is Room so slow?(Android:为什么Room这么慢?)
Remote Procedure call failed with sql server 2008 R2(使用 sql server 2008 R2 的远程过程调用失败)