Important: Please read the Qt Code of Conduct -

Building media database

  • Hello,

    I'd like to ask the more experienced developers for an advice regarding my player. I've written a music player in PySide (Python 3.3 + QML 1.0) that I use on my tablet with Windows 7 in car. It works quite well according to my needs nut I'd like to develop it further. But I need to make a decision in which direction should I go. I'd like to switch to Qt5/QML 2 and also port my player to Android. To do this, I need to leave Python.

    I decided to use Python because I know it and I'm totally unexperienced in C/C++. I think I could move most of the code to JS functions inside the QML. But the main part the player uses is the media database. I use a Python code, which seeks local filesystem, reads file information using MediaInfo library and stores the information in SQLite database.

    AFAIK I can not access the local filesystem fro QML/JS, is this right? What would be the easiest way to get a list of all directories, subdirectories and files to the QML part of my application? Would it be a good idea to use the Audio element to read information about the media files or would it be too slow and I should rather stay with MediaInfo for this and rewrite the Python part to C/C++?

    I will be grateful for any advice which will help me to choose the right direction.

  • I have wrote a number of media center thingys in qt/qml. Most of the time I use Sqlite to also store some of the info. I can read and write to these databases using QtQuick.LocalStorage in QML with some nifty JS work. I also tend to use QtMultimedia for my players. With there functions(start() stop() ect) I am writing a Huge Media center atm that uses Mythtv as a backend. Made a couple of tutorials also on youtube about QtMultimedia also . Along with gathering metadata and what not. I tend to Use and LastFM and TVDB for gathering info about media items. I like to Use XmlListModel to gather the information (check out youtube links) then insert that data into tables if they don't have that info.

    Other things that I have used that have helped me out.

    I made a plugin for using mysql sqlite postgre ect to make models from the data in the tables
    I also use Like QFile QDir for reading, creating and writing to files/dirs via c++ to qml
    I also find myself using things like QStandardPaths in C++ to setup things like syncing and what not to correct places/file paths, and scanning media, Like on start up from correct Dirs.

    Links of Useful stuff
    "QMultiMedia Audio":
    "XmlListModel ":
    "SqlDataBase QML":
    some tutorials
    "intro to qtmultimedia":
    "using metadata":
    "alpha stuff for Myth qml":

  • Hi bobweaver,

    thanks a lot for your advices and links. So I'll definitely have to learn some C++ to do the media scanning. I need to find a good tutorial on this. I always found some plain C guides, which claimed they are about C++. If I do something I want to do it right. Do you know any good tutorial which I could follow? If it also mentioned QtCreator as IDE, it would be great. :-)

  • as far as media scanning this is what I have done in the past (warning I am not a professional )

    Set up the dir that you want to scan via QStandardPaths::MoviesLocation
    and also make the user set there own if there is error in this &|| if they want more media dirs that are not /home/user/video or whatever the MoviesLocation is set as. You can do this via pop up. or how ever you want to implant that.

    Now there are a couple of ways that you can get metadata. You can use XmlListModel then text would be equal to the filename
    @XmlListModel {
    source = "" + text
    onStatusChanged: {
    if (status == XmlListModel.Ready) console.log("Loaded"){
    // Insert the Data Here to the table.


    That is just for TV stuff though you might want to write a script or use one that is out there already and alter it for your liking. 1st thing that comes to mind for me is.

    both are opensource and free to alter.

    Here is a example of me using in qml (,
    as you can see in that example I am not inserting anything into thte database but you could if you like.

    Ok so that takes care of default dirs. But what if the user has there own dirs that are like not under MoveLoacation? This is easy compared to what I thought it would be.

    1. QSqlDataBase. learn how to open and write to database. If you look at your source dir for qt. Under <src(qt5)> / <yourVersion(5.1.2)> / <your compiler (gcc)> / examples / sql

    you will see that there are plenty of examples that you can open the .pro file with qtcreator and hack at them.

    1. Get to know QDir and learn how to call on Dirs and read from them (files or dirs).

    2. QFile/QFileInfo/QString Use a foreach statement to run each file from your last function to get a list of all the movies then you can scan.

    void lookForFiles()
    QDir dir ;
    foreach (QFileInfo itm, dir.entryInfoList()) {
    if (itm.isFile() ){

    void getName(const QString &appFile)
    //Here is where we make are scanning happen and push to DB

    In you header File add Q_INVOKABLE to you functions so that you can call on them in QML. Like this

    Q_INVOKABLE void lookForFiles()

    Then in your main.cpp file or plugin file add the files that you have that are c++

    #include "somescanner.h"
    qmlRegisterType<SomeScanner>("MetaDataScanner", 1, 0, "Scanner");

    again there are other ways of doing things
    now in qml import your C++

    import MetaDataScanner 1.0

    id: scan
    Component.onCompleted: {
    scan.lookForFiles() // are Q_INVOKABLE

    There are a ton of video tutorials out there on how to use all these things that I have suggested.

    Some places to get tutorials

    check out voidrealms tutorials also on youtube. he has a playlist that is over 100 videos all about c++ qt

    I have also made some tutorials. all on QML though have not gotten to plugins. (my mic broke)

    Hope that this helps.

  • Thanks a lot for your great tips and ideas.

    I already looked at QDir and QFile and it seems to be pretty easy to use. I shouldn't have any problem with searching for files and directories.

    But what bothers me is the database building and reading. I know how to use SQL in a QML file, I've already programmed a small tool with DB in QML. But my player should be a music player in the first place with quite a huge database (ten thousands of songs).

    I can see 3 options:

    1. Read the directory in C++ and then send the list with file paths to QML. In QML open each file, read it's information (audio/video, metadata etc.) But I'm afraid it would be very slow.

    2. Use QMediaPlayer in C++ to scan file information. But again I'm afraid about the speed. Furthermore I don't know, how could QML access a database I create in C++. Is this possible at all?

    3. Use MediaInfo just like now, which would cause additional dependency. More important again I have the problem with accessing database I created in C++ from QML.

    I'll probably try to implement solution 1) and see if it's acceptable fast. Then I'll look into other possibilities. But I'm afraid i'll have to write all database related stuff in C++ anyway.

  • [quote author="jech" date="1380637460"]
    2) Use QMediaPlayer in C++ to scan file information. But again I'm afraid about the speed. Furthermore I don't know, how could QML access a database I create in C++. Is this possible at all?

    Maybe you would like to help with a simple plugin that I have been making.
    It makes QSqlQueryModel out of data in a database and then returns it to qml model. This can be used for sql mysql sqlite postgre or anything that is used in QSqlDatabase::addDatabase . Here is a Link to the plugin

    Things that need work.

    • Setting the Query in Qml better
    • Setting the connection to the database in a better way.
    • Having more Checks and tests to make sure querys are safe lol
    • settings for all the things in QSqlDatabase but in Qml
    • Make the Choice of Driver to be Enumeration. Like databaseType: SqlConnection.SQlite
    • Set Up count of the model to Notify in QML so there can be onCountChanged same for Status ect

    I have only used it for mysql I am sure that it could be used for other drivers though.
    Any help is great thanks. As I need it lol :)

  • I see you mentionned TMDB: I'm starting a Qt5-based tmdb access library, since I couldn't find one anywhere. It's currently at

    (if you read this in the far future and you can't find it, try

Log in to reply