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. Help with assigning struct to class member with models
Forum Updated to NodeBB v4.3 + New Features

Help with assigning struct to class member with models

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 260 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
    SafaAlfulaij
    wrote on 5 Oct 2018, 07:35 last edited by
    #1

    Hello.
    First of all, sorry if this question may be stupid.

    I am working on my first Qt app and came across a problem that I am not sure how to fix/name it.
    Basically, I have a file that I use an external library to parse, and store the returned data class in a Storage class. This class is used by one of my models.
    The issue is, I am not sure how to init the returned class to be a class member. Since the model is used, it request the rowCount before even I get anything assigned correctly, which causes a crash sometimes (maybe because of pointers and memory violation) in <vector> std library.

    I apologize again if this is a stupid question, or even not a Qt specific question.
    Thanks.

    Minimal concept code:

    // external library
    struct FileData{
        // etc etc
        std::map<std::string, std::string> styles;
        std::vector<FileEvent> events;
    };
    
    
    // The Storage class for getting/setting/etc
    class Storage : public QObject
    {
    public:
        Storage();
    
        bool loadFile(const QString& filePath) { // called by mainWindow
            FileParser fileParser; // external library's function
            // do file parse checking
            if (ok) {
                m_filePath = filePath;
                m_data = fileParser.data(); // data function returens a const struct FileData
                return true;
            }
            return false;
        }
    
        int eventCount() {
            return static_cast<int>(m_data->events.size());
        }
    
        FileEvent eventObj(int num);
    
    private:
        QString m_filePath;
        FileData *m_data; // external
    };
    
    
    // The model to be used in Views and to work with the data
    class EventsModel : public QAbstractTableModel
    {
        Q_OBJECT
    
    public:
        explicit EventsModel(QObject *parent = nullptr);
    
        void setStorage(Storage *storage) {
            beginResetModel();
            m_storage = storage;
            endResetModel();
        }
    
        int rowCount(const QModelIndex &parent = QModelIndex()) const override {
            return m_storage->eventCount();
        }
    
        // columnCount and data and headerData etc.
    
    private:
        Storage *m_storage;
    };
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 5 Oct 2018, 07:41 last edited by
      #2

      in the constructor add m_storage(nullptr) to the initialiser list and change rowCount to:

      int rowCount(const QModelIndex &parent = QModelIndex()) const override {
              return m_storage ? m_storage->eventCount() : 0;
          }
      

      "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
      4

      1/2

      5 Oct 2018, 07:35

      • Login

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