Example of subclassing QSqlTableModel?

  • I am new to Qt (coming from iOS programming) and am doing an image viewer app as a learning project.

    I have a QListView backed by a QSqlTableModel, and instead of images names, I want to display thumbnails. I've seen "here":http://www.qtcentre.org/threads/37895-How-to-show-images-from-database-as-icons-in-QListView that one way to do it is to subclass QSqlTableModel and override the data() method. I understand this conceptually and have read "Model Subclassing Reference":http://www.trinitydesktop.org/docs/qt4/model-view-model-subclassing.html and also searched around but have not found a barebones example of, for instance, how to implement the constructor in my subclass. Most examples assume that one knows this most basic thing (I guess).

    Can someone post (or point me to) a barebones example .h and .cpp showing how to subclass QSqlTableModel?

  • Lifetime Qt Champion


    You could always take a look at QSqlRelationalTableModel

    Constructor example:
    class MyTableModel : public QSqlTableModel
    explicit MyTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ); // the same arguments as QSqlTableModel

    MyTableModel::MyTableModel(QObject * parent, QSqlDatabase db) :
    QSqlTableModel(parent, db)
    // the rest of your code

    Subclassing in Qt is exactly the same as for any other C++ class.

    But before going this way, are you getting your images from the database ?
    You might also consider implementing a QStyledItemDelegate to show what you want rather than modify the model

    Hope it helps

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.