Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Help with assigning struct to class member with models

    General and Desktop
    2
    2
    153
    Loading More Posts
    • 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
      SafaAlfulaij last edited by

      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 Reply Quote 0
      • VRonin
        VRonin last edited by

        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 Reply Quote 4
        • First post
          Last post