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. Software development challenges around index creation for data models

Software development challenges around index creation for data models

Scheduled Pinned Locked Moved Unsolved General and Desktop
data modelssoftware designindex creationalgorithmsintegers
26 Posts 3 Posters 6.2k 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.
  • E elfring
    10 Oct 2018, 07:12

    You can if you want.

    How would you map a data model to Qt programming interfaces when each level within a hierarchy should correspond to a specific class?

    K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 10 Oct 2018, 09:01 last edited by
    #13

    @elfring said in Software development challenges around index creation for data models:

    How would you map a data model to Qt programming interfaces when each level within a hierarchy should correspond to a specific class?

    Explain what you mean by that.

    Read and abide by the Qt Code of Conduct

    E 1 Reply Last reply 10 Oct 2018, 09:18
    1
    • E elfring
      10 Oct 2018, 07:12

      You can if you want.

      How would you map a data model to Qt programming interfaces when each level within a hierarchy should correspond to a specific class?

      V Offline
      V Offline
      VRonin
      wrote on 10 Oct 2018, 09:03 last edited by VRonin 10 Oct 2018, 09:14
      #14

      In this case I'd use a generic item that supports QVariant and store the class in it as data.
      An alternative is to have an item with a void* (or a common base class if available) containing the class instance and an int that keeps track of what type that pointer is holding so it can be dynamic_casted when needed

      "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
      1
      • K kshegunov
        10 Oct 2018, 09:01

        @elfring said in Software development challenges around index creation for data models:

        How would you map a data model to Qt programming interfaces when each level within a hierarchy should correspond to a specific class?

        Explain what you mean by that.

        E Offline
        E Offline
        elfring
        wrote on 10 Oct 2018, 09:18 last edited by
        #15

        Explain what you mean by that.

        Will the following hierarchy example help for a better common understanding of a possible data model?

        1. Directories contain files.
        2. Text files can contain several lines.
        3. Text lines contain characters.

        Which classes would you like to use then in your software application?

        K 1 Reply Last reply 10 Oct 2018, 09:33
        0
        • V Offline
          V Offline
          VRonin
          wrote on 10 Oct 2018, 09:22 last edited by
          #16

          I'd use the QVariant approach (so both QStandardItemModel and the one above should work) you can store directories and files as QUrls to the local path and text lines as QString

          "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

          E 1 Reply Last reply 10 Oct 2018, 09:34
          0
          • E elfring
            10 Oct 2018, 09:18

            Explain what you mean by that.

            Will the following hierarchy example help for a better common understanding of a possible data model?

            1. Directories contain files.
            2. Text files can contain several lines.
            3. Text lines contain characters.

            Which classes would you like to use then in your software application?

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 10 Oct 2018, 09:33 last edited by
            #17

            The model infra is abstract enough so you can attach whatever you want to it. What you're describing is (kind of) a file system model with additional tweaks so you can subclass it and implement the last part. Or you can start from scratch and implement your own if you like.

            For my current project I use the model simply as a proxy to an abstract class that holds the data (due to various reasons), so you can do that either if you like. The options are limitless ...

            Read and abide by the Qt Code of Conduct

            E 1 Reply Last reply 10 Oct 2018, 11:03
            0
            • V VRonin
              10 Oct 2018, 09:22

              I'd use the QVariant approach (so both QStandardItemModel and the one above should work) you can store directories and files as QUrls to the local path and text lines as QString

              E Offline
              E Offline
              elfring
              wrote on 10 Oct 2018, 09:34 last edited by
              #18

              I'd use the QVariant approach

              I am occasionaly trying to avoid the data transfer by such a generic class.

              Will it make sense to apply an other software design composition?

              • Can it make sense to map even simple hierarchy levels to separate model classes?
              • Should relationships between model instances be expressed separately?
              1 Reply Last reply
              0
              • K kshegunov
                10 Oct 2018, 09:33

                The model infra is abstract enough so you can attach whatever you want to it. What you're describing is (kind of) a file system model with additional tweaks so you can subclass it and implement the last part. Or you can start from scratch and implement your own if you like.

                For my current project I use the model simply as a proxy to an abstract class that holds the data (due to various reasons), so you can do that either if you like. The options are limitless ...

                E Offline
                E Offline
                elfring
                wrote on 10 Oct 2018, 11:03 last edited by
                #19

                What you're describing is (kind of) a file system model

                Did anybody try to represent data as a file system for model variants besides the usage of the class “QFileSystemModel”?

                with additional tweaks so you can subclass it and implement the last part.

                I am trying again to clarify corresponding software development possibilities.

                For my current project I use the model simply as a proxy to an abstract class that holds the data …

                This design approach sounds very promising. How should data accesses be redirected to the existing container object here?

                K 1 Reply Last reply 12 Oct 2018, 05:28
                0
                • E elfring
                  10 Oct 2018, 11:03

                  What you're describing is (kind of) a file system model

                  Did anybody try to represent data as a file system for model variants besides the usage of the class “QFileSystemModel”?

                  with additional tweaks so you can subclass it and implement the last part.

                  I am trying again to clarify corresponding software development possibilities.

                  For my current project I use the model simply as a proxy to an abstract class that holds the data …

                  This design approach sounds very promising. How should data accesses be redirected to the existing container object here?

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 12 Oct 2018, 05:28 last edited by kshegunov 10 Dec 2018, 05:31
                  #20

                  @elfring said in Software development challenges around index creation for data models:

                  This design approach sounds very promising. How should data accesses be redirected to the existing container object here?

                  MyModel::MyModel(QObject * parent)
                      : QAbstractTableModel(parent), dataSource(nullptr)
                  {
                  }
                  
                  void MyModel::setDataSource(MyDataSource * source)
                  {
                      if (dataSource)  {
                          QObject::disconnect(this, nullptr, dataSource, nullptr);
                          QObject::disconnect(dataSource, nullptr, this, nullptr);
                      }
                  
                      dataSource = source;
                  
                      QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
                      QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);
                  }
                  
                  QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
                  {
                      if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section >= dataSource->columnCount())
                          return QVariant();
                  
                      Q_ASSERT(dataSource);
                      return dataSource->columnName(section);
                  }
                  
                  int MyModel::rowCount(const QModelIndex &) const
                  {
                      Q_ASSERT(dataSource);
                      return dataSource->rowCount();
                  }
                  
                  int MyModel::columnCount(const QModelIndex &) const
                  {
                      Q_ASSERT(dataSource);
                      return dataSource->columnCount();
                  }
                  
                  QVariant MyModel::data(const QModelIndex & index, int role) const
                  {
                      Q_ASSERT(dataSource);
                  
                      if (!index.isValid() || role != Qt::DisplayRole)
                          return QVariant();
                  
                      return dataSource->value(index.row(), index.column());
                  }
                  

                  and of course the corresponding interface:

                  
                  class MyDataSource : public QObject
                  {
                      Q_OBJECT
                      Q_DISABLE_COPY(MyDataSource)
                  
                  public:
                      MyDataSource(QObject * = nullptr);
                  
                      virtual int rowCount() const = 0;
                      virtual int columnCount() const = 0;
                      virtual QString columnName(int) const = 0;
                      virtual QVariant value(int, int) const = 0;
                  
                      virtual QString format(const QVariant &, int) const;
                  
                  signals:
                      void changed();
                  
                      void dataChangeStarted();
                      void dataChangeFinished();
                  
                  protected slots:
                      virtual void reloadData() = 0;
                  };
                  

                  Read and abide by the Qt Code of Conduct

                  E V 2 Replies Last reply 12 Oct 2018, 06:32
                  0
                  • K kshegunov
                    12 Oct 2018, 05:28

                    @elfring said in Software development challenges around index creation for data models:

                    This design approach sounds very promising. How should data accesses be redirected to the existing container object here?

                    MyModel::MyModel(QObject * parent)
                        : QAbstractTableModel(parent), dataSource(nullptr)
                    {
                    }
                    
                    void MyModel::setDataSource(MyDataSource * source)
                    {
                        if (dataSource)  {
                            QObject::disconnect(this, nullptr, dataSource, nullptr);
                            QObject::disconnect(dataSource, nullptr, this, nullptr);
                        }
                    
                        dataSource = source;
                    
                        QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
                        QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);
                    }
                    
                    QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
                    {
                        if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section >= dataSource->columnCount())
                            return QVariant();
                    
                        Q_ASSERT(dataSource);
                        return dataSource->columnName(section);
                    }
                    
                    int MyModel::rowCount(const QModelIndex &) const
                    {
                        Q_ASSERT(dataSource);
                        return dataSource->rowCount();
                    }
                    
                    int MyModel::columnCount(const QModelIndex &) const
                    {
                        Q_ASSERT(dataSource);
                        return dataSource->columnCount();
                    }
                    
                    QVariant MyModel::data(const QModelIndex & index, int role) const
                    {
                        Q_ASSERT(dataSource);
                    
                        if (!index.isValid() || role != Qt::DisplayRole)
                            return QVariant();
                    
                        return dataSource->value(index.row(), index.column());
                    }
                    

                    and of course the corresponding interface:

                    
                    class MyDataSource : public QObject
                    {
                        Q_OBJECT
                        Q_DISABLE_COPY(MyDataSource)
                    
                    public:
                        MyDataSource(QObject * = nullptr);
                    
                        virtual int rowCount() const = 0;
                        virtual int columnCount() const = 0;
                        virtual QString columnName(int) const = 0;
                        virtual QVariant value(int, int) const = 0;
                    
                        virtual QString format(const QVariant &, int) const;
                    
                    signals:
                        void changed();
                    
                        void dataChangeStarted();
                        void dataChangeFinished();
                    
                    protected slots:
                        virtual void reloadData() = 0;
                    };
                    
                    E Offline
                    E Offline
                    elfring
                    wrote on 12 Oct 2018, 06:32 last edited by
                    #21

                    and of course the corresponding interface:

                    Will your data source provide homogenous items for this model?

                    K 1 Reply Last reply 12 Oct 2018, 06:45
                    0
                    • E elfring
                      12 Oct 2018, 06:32

                      and of course the corresponding interface:

                      Will your data source provide homogenous items for this model?

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 12 Oct 2018, 06:45 last edited by kshegunov 10 Dec 2018, 06:45
                      #22

                      @elfring said in Software development challenges around index creation for data models:

                      Will your data source provide homogenous items for this model?

                      Do you mean the data types? If so, then no, the data types are different for the different columns of the table.

                      Read and abide by the Qt Code of Conduct

                      E 1 Reply Last reply 12 Oct 2018, 07:11
                      0
                      • K kshegunov
                        12 Oct 2018, 06:45

                        @elfring said in Software development challenges around index creation for data models:

                        Will your data source provide homogenous items for this model?

                        Do you mean the data types? If so, then no, the data types are different for the different columns of the table.

                        E Offline
                        E Offline
                        elfring
                        wrote on 12 Oct 2018, 07:11 last edited by
                        #23

                        …, the data types are different for the different columns of the table.

                        Will your data source work without hierarchies then?

                        K 1 Reply Last reply 12 Oct 2018, 09:11
                        0
                        • K kshegunov
                          12 Oct 2018, 05:28

                          @elfring said in Software development challenges around index creation for data models:

                          This design approach sounds very promising. How should data accesses be redirected to the existing container object here?

                          MyModel::MyModel(QObject * parent)
                              : QAbstractTableModel(parent), dataSource(nullptr)
                          {
                          }
                          
                          void MyModel::setDataSource(MyDataSource * source)
                          {
                              if (dataSource)  {
                                  QObject::disconnect(this, nullptr, dataSource, nullptr);
                                  QObject::disconnect(dataSource, nullptr, this, nullptr);
                              }
                          
                              dataSource = source;
                          
                              QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
                              QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);
                          }
                          
                          QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
                          {
                              if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section >= dataSource->columnCount())
                                  return QVariant();
                          
                              Q_ASSERT(dataSource);
                              return dataSource->columnName(section);
                          }
                          
                          int MyModel::rowCount(const QModelIndex &) const
                          {
                              Q_ASSERT(dataSource);
                              return dataSource->rowCount();
                          }
                          
                          int MyModel::columnCount(const QModelIndex &) const
                          {
                              Q_ASSERT(dataSource);
                              return dataSource->columnCount();
                          }
                          
                          QVariant MyModel::data(const QModelIndex & index, int role) const
                          {
                              Q_ASSERT(dataSource);
                          
                              if (!index.isValid() || role != Qt::DisplayRole)
                                  return QVariant();
                          
                              return dataSource->value(index.row(), index.column());
                          }
                          

                          and of course the corresponding interface:

                          
                          class MyDataSource : public QObject
                          {
                              Q_OBJECT
                              Q_DISABLE_COPY(MyDataSource)
                          
                          public:
                              MyDataSource(QObject * = nullptr);
                          
                              virtual int rowCount() const = 0;
                              virtual int columnCount() const = 0;
                              virtual QString columnName(int) const = 0;
                              virtual QVariant value(int, int) const = 0;
                          
                              virtual QString format(const QVariant &, int) const;
                          
                          signals:
                              void changed();
                          
                              void dataChangeStarted();
                              void dataChangeFinished();
                          
                          protected slots:
                              virtual void reloadData() = 0;
                          };
                          
                          V Offline
                          V Offline
                          VRonin
                          wrote on 12 Oct 2018, 08:57 last edited by
                          #24

                          @kshegunov said in Software development challenges around index creation for data models:

                          QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
                          QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);

                          I know you can do better than this mate! I know you have to create and connect a gazillion signals but at least you don't build everything from scratch

                          "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

                          K 1 Reply Last reply 12 Oct 2018, 09:10
                          0
                          • V VRonin
                            12 Oct 2018, 08:57

                            @kshegunov said in Software development challenges around index creation for data models:

                            QObject::connect(dataSource, &MyDataSource::dataChangeStarted, this, &MyModel::beginResetModel);
                            QObject::connect(dataSource, &MyDataSource::dataChangeFinished, this, &MyModel::endResetModel);

                            I know you can do better than this mate! I know you have to create and connect a gazillion signals but at least you don't build everything from scratch

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 12 Oct 2018, 09:10 last edited by kshegunov 10 Dec 2018, 09:14
                            #25

                            @VRonin said in Software development challenges around index creation for data models:

                            I know you can do better than this mate! I know you have to create and connect a gazillion signals but at least you don't build everything from scratch

                            Actually I can't in this case. I know you want me to emit the row/columns changed and the ***Inserted/***Deleted signals, but it simply isn't applicable in this particular case.

                            PS.
                            Here the data comes from a long JOIN of tables and I'd rather emit modelReset after I had processed all the peculiarities, than to do a fetch from the database on each displayed cell/row.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • E elfring
                              12 Oct 2018, 07:11

                              …, the data types are different for the different columns of the table.

                              Will your data source work without hierarchies then?

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 12 Oct 2018, 09:11 last edited by
                              #26

                              @elfring said in Software development challenges around index creation for data models:

                              Will your data source work without hierarchies then?

                              I don't follow. This is a table model, there's no hierarchy here.

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0

                              22/26

                              12 Oct 2018, 06:45

                              • Login

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