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. [SOLVED]QTableModel crashes on headerData

[SOLVED]QTableModel crashes on headerData

Scheduled Pinned Locked Moved Unsolved General and Desktop
qabstracttablemmodel-view
10 Posts 2 Posters 3.9k 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.
  • R Offline
    R Offline
    RDiGuida
    wrote on last edited by RDiGuida
    #1

    I am trying to implement a really simple Table Model inheriting from a QAbstractTableModel which looks like this

    #include <QAbstractTableModel>
    #include <QStringList>
    
    class ViewModel : public QAbstractTableModel
    {
        Q_OBJECT
    public:
        ViewModel(QList<QStringList>* mat, QStringList* cnames, QStringList* rnames, QObject *parent=0);
        virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
        virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
        virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
        virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
    
    signals:
    
    public slots:
    
    private:
        QStringList* colnames;
        QStringList* rownames;
        QList<QStringList>* sourceMat;
    };
    

    Implemented as

    ViewModel::ViewModel(QList<QStringList>* mat, QStringList* cnames, QStringList* rnames,QObject *parent)
        :QAbstractTableModel(parent)
    {
        if(mat->count()!=rnames->count() || mat->at(0).count()!=cnames->count())
            return;
    
        sourceMat = mat;
        colnames = cnames;
        rownames = rnames;
    }
    
    int ViewModel::rowCount(const QModelIndex &parent ) const
    {
        Q_UNUSED(parent);
        return sourceMat->count();  //last line is empty
    }
    
    int ViewModel::columnCount(const QModelIndex &parent ) const
    {
        Q_UNUSED(parent);
        return sourceMat->at(0).count();
    }
    
    QVariant ViewModel::headerData(int section,
                Qt::Orientation orientation, int role) const
    {
        if (role != Qt::DisplayRole)
                return QVariant();
    
        qDebug() << orientation << " " << section;
    
        if (orientation == Qt::Horizontal)
            return colnames->at(section);
        else
            return rownames->at(section);
    }
    

    I think the problem is in the headerData as the rownames and colnames are correct the first times the function is called but when the function gets called for a second iteration (I have no idea why) the rownames and colnames are not accessible anymore. Also - the data function is never reached.

    The model is called with a single column and 24 rows and attached to a QTableView

    ViewModel* mod_meta = new ViewModel(mat_meta,&cnamMet,data.rnames);
    
    QTableView* tview = new QTableView;
    
    tview->setModel(mod_meta);
    QGridLayout* gl = new QGridLayout;
    gl->addWidget(tview,0,0);
    
    delete frame->layout();   // delete previous layout 
    frame->setLayout(gl);
    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @RDiGuida said:
      Hi, is cnamMet and data.rnames part of a class so that they remain valid
      for the life time of the Model?

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RDiGuida
        wrote on last edited by
        #3

        Thanks, that was the problem! I converted the private members into QStringList rather than QStringList*

        mrjjM 1 Reply Last reply
        1
        • R RDiGuida

          Thanks, that was the problem! I converted the private members into QStringList rather than QStringList*

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

          @RDiGuida
          hi
          you made the
          QStringList* colnames;
          QStringList* rownames;
          in ViewModel
          to
          QStringList colnames;
          QStringList rownames;
          ?

          1 Reply Last reply
          1
          • R Offline
            R Offline
            RDiGuida
            wrote on last edited by
            #5

            Yes, and I passed the pointer dereferenced.

            mrjjM 1 Reply Last reply
            0
            • R RDiGuida

              Yes, and I passed the pointer dereferenced.

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

              @RDiGuida
              Ok. it is fine. as you long as you are aware that its
              most likely a copy the model have then.
              so if you change cnamMet or data.rnames in main program, the model
              still uses the original ones. (its copy)

              R 1 Reply Last reply
              0
              • mrjjM mrjj

                @RDiGuida
                Ok. it is fine. as you long as you are aware that its
                most likely a copy the model have then.
                so if you change cnamMet or data.rnames in main program, the model
                still uses the original ones. (its copy)

                R Offline
                R Offline
                RDiGuida
                wrote on last edited by
                #7

                @mrjj I know thanks, the only thing that puzzles me is that it works if I keep sourceMat as pointer while it should have the same problems of rownames and colnames.

                mrjjM 1 Reply Last reply
                0
                • R RDiGuida

                  @mrjj I know thanks, the only thing that puzzles me is that it works if I keep sourceMat as pointer while it should have the same problems of rownames and colnames.

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

                  @RDiGuida said:
                  Ok. super.
                  Me too.
                  In this line
                  ViewModel* mod_meta = new ViewModel(mat_meta,&cnamMet,data.rnames);
                  the variables cnamMet and data.rnames. where are they declared? (lives) ?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RDiGuida
                    wrote on last edited by
                    #9

                    cnamMet is declared in the same function where the model is initialised while data.rnames is declared in another class which is initialised outside of the function

                    mrjjM 1 Reply Last reply
                    0
                    • R RDiGuida

                      cnamMet is declared in the same function where the model is initialised while data.rnames is declared in another class which is initialised outside of the function

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

                      @RDiGuida
                      Ok, so cnamMet would run out of scope and be deleted (when function ended)
                      but sounds like data.rnames should have been working.
                      Well if a copy on creation time works for you, its a wrap :)

                      1 Reply Last reply
                      0

                      • Login

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