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. Can't edit text in cell in a dynamic QTable model
Forum Updated to NodeBB v4.3 + New Features

Can't edit text in cell in a dynamic QTable model

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 975 Views 1 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by aha_1980
    #1

    Hello, I am in troubles with my model, I have created a model where I can dinamically add new rows, then I want to edit its text, so I created a vector in my class such as I can dinamically add or modify text, but I obtain the following error:

    ASSERT failure in QVector<T>::operator[]: "index out of range", file /Users/aaaa/Qt/5.11.1/clang_64/lib/QtCore.framework/Headers/qvector.h, line 436
    19:54:10: The program has unexpectedly finished.

    bool Table::setData(const QModelIndex & index, const QVariant & value, int role)
    {
         m_gridData = new QVector<QVector<QString>>;
    
        if (role == Qt::EditRole)
        {
            //save value from editor to member m_gridData
            (*m_gridData)[index.row()][index.column()]= value.toString();
            //for presentation purposes only: build and emit a joined string
            QString result;
            for(int row= 0; row < rows; row++)
            {
                for(int col= 0; col < columns; col++)
                {
                    result += (*m_gridData)[row][col];
    
                }
            }
            emit editCompleted(result);
            return true;
        }
        return false;
    }
    

    Why is my index out of range??

    Thank you!

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

      Hi
      The vector is empty so accessing it will crash.
      [index.row()][index.column()] can never be valid for empty vector.

      if you want to use [][], you should use
      https://doc.qt.io/qt-5/qvector.html#reserve
      https://doc.qt.io/qt-5/qvector.html#resize
      on both vectors.
      so they contain empty strings.
      Or use append to add the data.

      1 Reply Last reply
      3
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #3

        You mean change
        (*m_gridData)[index.row()][index.column()]= value.toString();

        By
        (*m_gridData)[row][column]= value.toString();???

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Again: you can't access to an index which is out-of-bounds and will get an asserting (in debug mode) or undefined behavior (in release mode) - see https://doc.qt.io/qt-5/qvector.html#operator-5b-5d

          "i must be a valid index position in the vector (i.e., 0 <= i < size())."

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          4
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Memory Leaks Everywhere

            Why are you doing m_gridData = new QVector<QVector<QString>>;?

            "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
            • J Offline
              J Offline
              jss193
              wrote on last edited by
              #6

              I tried this

              bool ModelTable::setData(const QModelIndex & index, const QVariant & value, int role)
              {
                  m_gridData = new QVector<QVector<QString>>;
              
              
                  if (role == Qt::EditRole)
                  {
                      //save value from editor to member m_gridData
                      (*m_gridData)[index.row()][index.column()]= value.toString();
              
                      //for presentation purposes only: build and emit a joined string
              
                      QString result;
                      for(int row= 0; row < rows; row++)
                      {
                          for(int col= 0; col < columns; col++)
                          {
                              //result += (*m_gridData)[row][col];
                              (*m_gridData).append(QVector<QString>()<<result);
              
                          }
                      }
                      emit editCompleted(result);
                      return true;
                  }
                  return false;
              }
              
              

              and Initialized the vector in constructor such as:

              ModelTable::ModelTable(QObject *parent):QAbstractTableModel (parent)
              {
               m_gridData = new QVector<QVector<QString>>;
              }
              

              and updated size such as:

              bool ModelTable::insertRows(int row, int count, const QModelIndex &parent){
                  beginInsertRows(parent,row,row+count-1);
                  rows = rows+count;
                  endInsertRows();
                  qDebug() << rows;
                   (*m_gridData)[rows][columns];//update value of matrix
                  return true;
              

              and still index out of range.

              I did this
              (*m_gridData)[index.row()][index.column()]= value.toString();
              to store the initial values of table into my m_gridData and update them as the programs is working.

              Thanks!

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                None of the above makes any sense.

                Why are you subclussing a model anyway? What is the problem with just using QStandardItemModel?

                "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
                0
                • J Offline
                  J Offline
                  jss193
                  wrote on last edited by
                  #8

                  Because I want it modified by adding new methods later associated with dynamic table. What I wish is to add dinamycally rows by pressing a button, then store data into cells and this data into a vector of vectors to be modified on execution time

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    Everything you described can be done by QStandardItemModel.
                    Something like:

                    QAbstractItemModel* model = new QStandardItemModel(this);
                    model->insertColumns(0,columns);
                    model->insertRows(0,rows);
                    model->setData(model->index(0,0),QStringLiteral("One String"));
                    model->setData(model->index(1,0),QStringLiteral("Another String"));
                    model->insertRow(1);
                    model->setData(model->index(1,0),QStringLiteral("Inserted String"));
                    

                    @jss193 said in Can't edit text in cell in a dynamic QTable model:

                    I want it modified by adding new methods later

                    You can always just subclass QStandardItemModel you don't need to start from scratch

                    "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
                    2
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      Besides i very much agree with @VRonin about using QStandardItemModel
                      Your current issue is that you use 2 vectors now

                      bool ModelTable::setData(const QModelIndex & index, const QVariant & value, int role)
                      {
                      m_gridData = new QVector<QVector<QString>>; // this is a new one, you add to
                      ...

                      so
                      the one you make
                      ModelTable::ModelTable(QObject *parent):QAbstractTableModel (parent)
                      {
                      m_gridData = new QVector<QVector<QString>>;

                      Might not be used at all and just leak memory or be used in
                      bool ModelTable::insertRows where its empty and still gives index out of bounds for that reason.

                      1 Reply Last reply
                      2

                      • Login

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