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. QTableView and list of structs
Forum Updated to NodeBB v4.3 + New Features

QTableView and list of structs

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.7k Views 2 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.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by
    #1

    I see https://doc.qt.io/qt-5/sql-presenting.html but is SQL. I search maximal simple model with list of structs.

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

      Hi,

      Create a subclass of QAbstractTableModel.

      The row count being the length of your list
      The column count the number of elements in your struct
      In the data method return the various elements of your struct based on the column number. The struct itself being selected based on the row number.

      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
      3
      • A Offline
        A Offline
        AndrzejB
        wrote on last edited by
        #3

        I have created simple model:
        https://gist.github.com/borneq/89673fb3d8603b35117232bd9ae078e0
        how to simply expand functionality , to user can:
        have list of any struct
        QList<structA> listA;
        BTableModel model
        model.setList(listA)

        I must use templates, like:
        BTableModel<structA> model ?

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

          Hi
          Even if you use template to let model accept any struct/type.
          How will it know which member variables to to use for row/col values ?

          say we have

          struct MyData {
          string name;
          int age;
          }

          the model cannot know it should use name / age so
          templates alone cannot make it very generic.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            AndrzejB
            wrote on last edited by
            #5

            Two solutions:

            1. Make modelA for structA and modelB for struct B
              2.simpler: model uses QList of QStringList, user convert structA and structB to QList of QStringList
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              yes that could work.
              Option 3 would to expect the struct to have a certain interface.
              Since ultimately any data from model is QVariant, you could make it a bit generic
              with

              struct MyData {
              string name;
              int age;
              QVariant getData(int col);
              }
              

              and model would use getData to well get the data for a given col.

              1 Reply Last reply
              3
              • A Offline
                A Offline
                AndrzejB
                wrote on last edited by
                #7

                Next problem:
                I have intereface:

                #pragma once
                #include <QVariant>
                
                class IVariantable
                {
                public:
                    virtual QVariant getData(int col) = 0;
                };
                
                

                and

                struct Mystruct: public IVariantable
                

                but I want list.
                For QList<IVariantable> will be error. Must be QList<IVariantable*> and I must create new Mystruct and add to list?

                mrjjM 1 Reply Last reply
                0
                • A AndrzejB

                  Next problem:
                  I have intereface:

                  #pragma once
                  #include <QVariant>
                  
                  class IVariantable
                  {
                  public:
                      virtual QVariant getData(int col) = 0;
                  };
                  
                  

                  and

                  struct Mystruct: public IVariantable
                  

                  but I want list.
                  For QList<IVariantable> will be error. Must be QList<IVariantable*> and I must create new Mystruct and add to list?

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

                  @AndrzejB
                  Hi
                  yes you showed
                  QList<structA> listA;
                  model.setList(listA)
                  so i assumed Qlist would be used.

                  Well if you want polymorph function and use an abstract base class, yes
                  it has to be pointers. Its more flexible but care must be taken with
                  life span of the Mystruct in the list.

                  1 Reply Last reply
                  2
                  • A Offline
                    A Offline
                    AndrzejB
                    wrote on last edited by AndrzejB
                    #9

                    https://gist.github.com/borneq/a953f6019c6694dfe66451adcfc29914
                    But is both: interface and template. Without template I can't do vector QVector<IVariantable*>
                    Is solution: use only list or vector <IVariantable*>, not vector of derived pointers

                    jsulmJ 1 Reply Last reply
                    0
                    • A AndrzejB

                      https://gist.github.com/borneq/a953f6019c6694dfe66451adcfc29914
                      But is both: interface and template. Without template I can't do vector QVector<IVariantable*>
                      Is solution: use only list or vector <IVariantable*>, not vector of derived pointers

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @AndrzejB said in QTableView and list of structs:

                      use only list or vector <IVariantable*>, not vector of derived pointers

                      I'm not sure I understand: it's actually the same.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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