Important: Please read the Qt Code of Conduct -

Help with assigning struct to class member with models

  • 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.

    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
        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 =; // 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);
        QString m_filePath;
        FileData *m_data; // external
    // The model to be used in Views and to work with the data
    class EventsModel : public QAbstractTableModel
        explicit EventsModel(QObject *parent = nullptr);
        void setStorage(Storage *storage) {
            m_storage = storage;
        int rowCount(const QModelIndex &parent = QModelIndex()) const override {
            return m_storage->eventCount();
        // columnCount and data and headerData etc.
        Storage *m_storage;

  • 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;

Log in to reply