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. model view: How to tied up data form view with underlying data

model view: How to tied up data form view with underlying data

Scheduled Pinned Locked Moved Solved General and Desktop
model-viewcustom typemapping
5 Posts 3 Posters 949 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.
  • A Offline
    A Offline
    AlexandruToma
    wrote on 2 Feb 2021, 15:18 last edited by
    #1

    Hello, I have some problem understanding model/view architecture, maybe is someone with free time to explain some things to me, please.
    I have one table view activityView and all data inserted by the user in this table view is store into a c++ vector from another class( my own custom type, vector<activity>). I have edit, add and delete button. So i don't figure it out when user select one activity from the table to edit it how to tied up to the same activity from the stored data vector<activity>. At the moment i have an implementation with the same index(if user chose first row of the table, the first element of the vector is edited), it's not ok. I know it can be better but I don't know how.
    Give me all links that you have and you think I need to read(I am here to learn).

    1 Reply Last reply
    0
    • A AlexandruToma
      2 Feb 2021, 18:14

      Yes I have a custom model. And this is my data function
      QVariant TableModel::data(const QModelIndex& index, int role) const
      {
      if (!index.isValid() || role != Qt::DisplayRole) {
      return QVariant();
      }

      const auto& teacher = mContext.GetTeacherByIndex(index.row());

      switch (index.column()) {
      case 0:
      return QString::fromStdString((teacher->GetFirstName()));
      case 1:
      return QString::fromStdString((teacher->GetLastName()));
      default:
      break;
      }
      }

      The problem is: if i sort teachers by name in view, then would have different index.row(). So when i get teacher by index i would get another teacher

      J Offline
      J Offline
      JonB
      wrote on 2 Feb 2021, 21:08 last edited by JonB 2 Mar 2021, 19:38
      #4

      @AlexandruToma
      No, you will impose sorting via a QSortFilterProxyModel on top of your model if sorting is wanted. View will access that. Your indexes here will be unaffected. That is why a proxy model is nice :)

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 2 Feb 2021, 15:35 last edited by mrjj 2 Feb 2021, 15:37
        #2

        Hi
        So do you have your own custom model ?

        Normally in
        QVariant Model::data(const QModelIndex &index, int role)
        we use index.row take from the "source"

        Like here
        https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.html

        where they expose a Contact class via a custom TableModel

        1 Reply Last reply
        2
        • A Offline
          A Offline
          AlexandruToma
          wrote on 2 Feb 2021, 18:14 last edited by AlexandruToma 2 Feb 2021, 19:16
          #3

          Yes I have a custom model. And this is my data function
          QVariant TableModel::data(const QModelIndex& index, int role) const
          {
          if (!index.isValid() || role != Qt::DisplayRole) {
          return QVariant();
          }

          const auto& teacher = mContext.GetTeacherByIndex(index.row());

          switch (index.column()) {
          case 0:
          return QString::fromStdString((teacher->GetFirstName()));
          case 1:
          return QString::fromStdString((teacher->GetLastName()));
          default:
          break;
          }
          }

          The problem is: if i sort teachers by name in view, then would have different index.row(). So when i get teacher by index i would get another teacher

          J 1 Reply Last reply 2 Feb 2021, 21:08
          0
          • A AlexandruToma
            2 Feb 2021, 18:14

            Yes I have a custom model. And this is my data function
            QVariant TableModel::data(const QModelIndex& index, int role) const
            {
            if (!index.isValid() || role != Qt::DisplayRole) {
            return QVariant();
            }

            const auto& teacher = mContext.GetTeacherByIndex(index.row());

            switch (index.column()) {
            case 0:
            return QString::fromStdString((teacher->GetFirstName()));
            case 1:
            return QString::fromStdString((teacher->GetLastName()));
            default:
            break;
            }
            }

            The problem is: if i sort teachers by name in view, then would have different index.row(). So when i get teacher by index i would get another teacher

            J Offline
            J Offline
            JonB
            wrote on 2 Feb 2021, 21:08 last edited by JonB 2 Mar 2021, 19:38
            #4

            @AlexandruToma
            No, you will impose sorting via a QSortFilterProxyModel on top of your model if sorting is wanted. View will access that. Your indexes here will be unaffected. That is why a proxy model is nice :)

            1 Reply Last reply
            2
            • A Offline
              A Offline
              AlexandruToma
              wrote on 4 Feb 2021, 16:56 last edited by AlexandruToma 2 Apr 2021, 21:53
              #5
              This post is deleted!
              1 Reply Last reply
              0

              1/5

              2 Feb 2021, 15:18

              • Login

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