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. QAbstractTableModel: Storing information that is not shown by the QTableView

QAbstractTableModel: Storing information that is not shown by the QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 6 Posters 934 Views
  • 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.
  • S Offline
    S Offline
    Smeeth
    wrote on last edited by Smeeth
    #1

    I have followed the Address Book Example to create my own TableModel and TableView.

    My implementation is very similar to this example, except each entry in the table is a Patient struct that has an ID associated with it. This ID is not shown the TableView, but when modifications are made, I use the ID to save the changes in the filesystem.

    This is the MyPatient struct

    struct MyPatient
    {
        qlonglong id;
        QString firstName;
        QString lastName;
        QString dob;
        QString sex;
        QString height;
        QString weight;
        QString dateModified;
    
        bool operator==(const MyPatient &other) const
        {
            return id == other.id;
        }
    };
    

    Here is the body of my addPatient() method to add a patient to the table:

    insertRows(0, 1, QModelIndex());
    
    QModelIndex index = this->index(0, 0, QModelIndex());
    setData(index, patient.firstName, Qt::EditRole);
    index = this->index(0, 1, QModelIndex());
    setData(index, patient.lastName, Qt::EditRole);
    index = this->index(0, 2, QModelIndex());
    setData(index, patient.dob, Qt::EditRole);
    index = this->index(0, 3, QModelIndex());
    setData(index, patient.sex, Qt::EditRole);
    index = this->index(0, 4, QModelIndex());
    setData(index, patient.height, Qt::EditRole);
    index = this->index(0, 5, QModelIndex());
    setData(index, patient.weight, Qt::EditRole);
    index = this->index(0, 6, QModelIndex());
    setData(index, patient.dateModified, Qt::EditRole);
    

    With the current implementation, when I call the data() method to retrieve a patient, the ID field is 0, because the table does not store that value.

    How can I store this ID value in the table without showing it as its own column in the TableView?

    I have tried added another two lines to the addPatient method:

        index = this->index(0, 7, QModelIndex());
        setData(index, patient.id, Qt::EditRole);
    

    But the setData detects the index value as invalid, as the columnCount of the table is 7 as that is how many columns should appear in the QTableView.

    Thank you, please let me know if there is anything in my question I can clarify.

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You can store the data in a user Role. ( for any existing column)
      setData(index, patient.id, Qt::UserRole);
      or you could have it as a real column
      and simply hide it. (in the view)
      TableView->setColumnHidden(7, true);

      JonBJ fcarneyF 2 Replies Last reply
      5
      • mrjjM mrjj

        Hi
        You can store the data in a user Role. ( for any existing column)
        setData(index, patient.id, Qt::UserRole);
        or you could have it as a real column
        and simply hide it. (in the view)
        TableView->setColumnHidden(7, true);

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        I had assumed the intended way is QTableView::hidden(). This allows the column to appear naturally in the model and be manipulated as such. I'm trying to figure what @mrjj 's alternative of storing it in a user role adds up to....

        mrjjM 1 Reply Last reply
        0
        • JonBJ JonB

          I had assumed the intended way is QTableView::hidden(). This allows the column to appear naturally in the model and be manipulated as such. I'm trying to figure what @mrjj 's alternative of storing it in a user role adds up to....

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB
          Hi
          Well its just like Qt::EditRole, but not shown by the View. However can be used
          by delegates etc.

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            You can store the data in a user Role. ( for any existing column)
            setData(index, patient.id, Qt::UserRole);
            or you could have it as a real column
            and simply hide it. (in the view)
            TableView->setColumnHidden(7, true);

            fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @mrjj said in QAbstractTableModel: Storing information that is not shown by the QTableView:

            Hi
            You can store the data in a user Role. ( for any existing column)
            setData(index, patient.id, Qt::UserRole);
            or you could have it as a real column
            and simply hide it. (in the view)
            TableView->setColumnHidden(7, true);

            Can QTableView use the roleNames interface that TableView in QML requires? Or are QTableView and TableView very different?

            C++ is a perfectly valid school of magic.

            mrjjM 1 Reply Last reply
            0
            • fcarneyF fcarney

              @mrjj said in QAbstractTableModel: Storing information that is not shown by the QTableView:

              Hi
              You can store the data in a user Role. ( for any existing column)
              setData(index, patient.id, Qt::UserRole);
              or you could have it as a real column
              and simply hide it. (in the view)
              TableView->setColumnHidden(7, true);

              Can QTableView use the roleNames interface that TableView in QML requires? Or are QTableView and TableView very different?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @fcarney
              Hi
              I dont know if thats the same between QWidgets and QML
              https://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                @smiffy Might be silly question but did you check that your patient.id value is valid ?

                @fcarney What do you have in mind with QTableView and the custom roles ?

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

                fcarneyF 1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  @smiffy Might be silly question but did you check that your patient.id value is valid ?

                  @fcarney What do you have in mind with QTableView and the custom roles ?

                  fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #8

                  @SGaist said in QAbstractTableModel: Storing information that is not shown by the QTableView:

                  @fcarney What do you have in mind with QTableView and the custom roles ?

                  When I tried to create an interface for both a QTableView and a TableView it seemed like I had to implement different things to get them to work. I was wondering if there was a common way to interface to either set of views. If the answer is just "they are different" I am fine with that.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #9

                    Until 5.12, they are different. However, you can map the column/role names in the data function and you're good to go for both situations.

                    [edit: Fixed missing Qt version precision SGaist]

                    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
                    2
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      I have followed the Address Book Example to create my own TableModel

                      If you are creating your custom model, why don't you just store the MyPatient items directly and just reimplement data() to return the field you prefer?

                      @fcarney said in QAbstractTableModel: Storing information that is not shown by the QTableView:

                      When I tried to create an interface for both a QTableView and a TableView it seemed like I had to implement different things to get them to work.

                      TL;DR: from Qt 5.12 onward QTableView and TableView can be used with almost no change in the interface

                      This is currently changing. The QML tableview used to be basically multiple listviews squashed together, it's now evolving https://blog.qt.io/blog/2018/08/29/tableview/ and a treeview is also in the pipeline.

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      3

                      • Login

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