Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Example of subclassing QSqlTableModel?
Forum Updated to NodeBB v4.3 + New Features

Example of subclassing QSqlTableModel?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on 10 Aug 2013, 20:03 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Aug 2013, 20:23 last edited by
      #2

      Hi,

      You could always take a look at QSqlRelationalTableModel

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

      mytablemodel.cpp
      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      2/2

      10 Aug 2013, 20:23

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved