如何在QML(Qt)上以图像的形式显示SQLite BLOB数据?
我的代码如下 名称-作为文本字段 照片-作为水滴数据如何在QML(Qt)上以图像的形式显示SQLite BLOB数据?,qt,sqlite,qml,blob,Qt,Sqlite,Qml,Blob,我的代码如下 名称-作为文本字段 照片-作为水滴数据 class SqlQueryModel: public QSqlQueryModel { Q_OBJECT QHash<int,QByteArray> *hash; public: explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent) { hash = new QHash<int,QByteArray
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; }
};
QML中的绑定
ListView {
id: someListView
model: SqlContactModel {}
delegate: ItemDelegate {
text: Name
Image {
id: Photo
source: ???
}
}
}
如何在QML(Qt)上以图像形式显示SQLite BLOB数据?您有三个选项:
QQuickImageProvider
QImage
,并编写一个可以显示它的自定义项QQuickPaintedItem
派生类,类似于
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中导入SomeModuleName 1.0
并在中使用SomeTypeName
而不是图像
,通过绑定到项目的image属性的模型返回的QImage
,我想您需要我来看看它的一些示例。对于哪个选项?对于每个比较效果的选项,我在QQuickImageProvider上找到了一些示例,但其他的没有。最后一个选项有整个Wikipedia页面,我相信还有很多其他的页面。对于第二个选项,最简单的方法是使用QImage
属性创建QQuickPaintedItem
派生类
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()));
}