Qt6 topics merged with General

How to store an image inside the Qt project

  • Hi All,
    I am developing a Meego project in Qt.In my application i need to dynamically add picture files inside the application directory.How can i achieve this functionality.
    I am using SQLite in my project.SO from my point of view i have two options.
    1)Directoly storing the images in the database:
    2)Store only the name of the file in the database and keeping the file inside a directory(Say 'Images') in the application directory.

    Please clarify me which one is the better way and how can i achieve this.


  • Not sure what the problem is. You can use both ways you suggested.
    There is a deployment issue however. Your executable may go to /usr/bin but only root has permission to add files there. Also, how are you going to organise the clean up after a program crash?

  • To store images in a sqlite you should use BLOB (Binary Large Object)data type.


    1. Storing images in a database (probably you will be using SQlite,i suppose) is a slow process, compared to direct file access. How ever BLOB is the hint you need. To may first read the file and make it a Byte array, for example :

    @ QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) return;
    QByteArray imgByteArray = file.readAll();@

    You can store byte array like any other data, For example :
    @ QSqlQuery query;
    query.prepare("INSERT INTO ....

    You may retrieve like any other data and here is a sample ,how i used to create a QPixmap.

    @ QSqlQuery query2("SELECT ........
    QByteArray array = query2.value(0).toByteArray();
    QPixmap pixmap = QPixmap();

    Done :)

    2)Using a directory . You can store the file name as text in db. To avoid conflicts, i think its a good idea to use user space for the image directory than the application directory.

Log in to reply