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. [SOLVED] Subclass QAbstractTableModel to handle SQLite tables
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Subclass QAbstractTableModel to handle SQLite tables

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 3.2k 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.
  • L Offline
    L Offline
    lukeQt
    wrote on 2 Oct 2014, 16:05 last edited by
    #1

    Hi,

    First time posting so bare with me. I have already created QAbstractTableModel and QStyledItemDelegate classes and am getting better with model/view programming. I am working with PyQt4. How do I connect my custom model to handle SQLite tables now? I know there is QSqlTableModel, but I have already spent a considerable amount of time creating my own custom table model. How can I use my model with SQLite tables? Also how is the best way to reuse a model for different views? I have been creating a new model for every view which doesn't seem like the best approach. What do I need to add to support foreign key support to my custom table model? I want to be able to insert a record into a database table from my model and delete a record from the database. What other methods do I need?

    QAbstactTableModel methods include:
    flags
    rowCount
    columnCount
    headerData
    insertRows
    data
    setData

    QStyledItemDelegate methods inculde:
    createEditor
    commitAndCloseEditor
    setEditorData
    setModelData
    sizeHint

    Thank you in advance.
    Luke

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Oct 2014, 20:27 last edited by
      #2

      Hi and welcome to devnet,

      If you don't want to use QSqlTableModel you'll have to use QSqlQuery. QSqlDatabase to connect to your database.

      As for the multiple views with your model, just re-use the model.

      @
      MyModel *model = new MyModel;
      QTableView *tableView = new QTableView;
      tableView.setModel(model);
      QListView *listView = new QListView;
      listView.setModel(model)
      // etc…
      @

      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
      • L Offline
        L Offline
        lukeQt
        wrote on 3 Oct 2014, 14:32 last edited by
        #3

        Thank you SGaist for your help. I do have one more question. I want to have a table view on the left and a tableview on the right. My goal is to show all the rows of the database on the left tableview. On the right tableview I want to have a long form. Right now I achieve this goal by having two models. Ideally if they select a row in the left model it updates the form model on the right? Rather then use two models for this would you use QDataMapper for this? I want the user to be able to edit either place and have the information updated in the database.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Oct 2014, 22:01 last edited by
          #4

          QDataWidgetMapper sounds better if you are creating a widget to edit your database fields

          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
          • L Offline
            L Offline
            lukeQt
            wrote on 15 Oct 2014, 14:58 last edited by
            #5

            That is what I thought and thank you again SGaist.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Oct 2014, 22:30 last edited by
              #6

              You're welcome !

              If that answers your question, please update the thread title prepending [solved] so other forum users may know as solution has been found :)

              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
              • L Offline
                L Offline
                lukeQt
                wrote on 16 Oct 2014, 14:18 last edited by
                #7

                Hi SGaist,

                Without sounding like a complete noob how do I add tag for solved?

                Thank you,
                Luke Kaim

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 16 Oct 2014, 14:26 last edited by
                  #8

                  That's not noob at all, that thing is a bit hidden.

                  Just edit your first post and update the title

                  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
                  • L Offline
                    L Offline
                    lukeQt
                    wrote on 16 Oct 2014, 14:49 last edited by
                    #9

                    And I was thinking there would be a button for this. lol. I updated the title.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 16 Oct 2014, 21:18 last edited by
                      #10

                      It's been asked and it's a known demand. Maybe for the next version of the forum :)

                      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

                      • Login

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