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. table model with 3d input
Forum Updated to NodeBB v4.3 + New Features

table model with 3d input

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 434 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.
  • mhsgoudM Offline
    mhsgoudM Offline
    mhsgoud
    wrote on last edited by
    #1

    photo_2023-12-08_19-41-29.jpg

    I need a table model which allows me to update a table with X-Y-Z and F (value at 3d grid points) as in the photo.

    Is there any of the table models in qt that I can use? If there is an example, I really appreciate you send me the links.
    Thanks

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

      Hi,

      Looks like a combination of QListView and QTableView. You can use QStandardItemModel for both. Or depending on what you want to do with the data create your own QAbstractTableModel and QAbstractListModel.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      mhsgoudM 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Looks like a combination of QListView and QTableView. You can use QStandardItemModel for both. Or depending on what you want to do with the data create your own QAbstractTableModel and QAbstractListModel.

        mhsgoudM Offline
        mhsgoudM Offline
        mhsgoud
        wrote on last edited by mhsgoud
        #3

        @SGaist said in table model with 3d input:

        QAbstractListModel.

        Thanks, so I can have a different number of table rows/cols for different values of z?

        I tried to subclass QAbstractTableModel but seems I have a segmentation fault

        Matrix model:

        MatrixModel::MatrixModel(int numRows, int numColumns, double* data)
            : m_numRows(numRows),
              m_numColumns(numColumns),
              m_data(data)
        {
        }
        
        int MatrixModel::rowCount(const QModelIndex& parent ) const
        {
            return m_numRows;
        }
        
        int MatrixModel::columnCount(const QModelIndex& parent ) const
        {
            return m_numColumns;
        }
        
        QVariant MatrixModel::data(const QModelIndex& index, int role ) const
        {
            if (!index.isValid() || role != Qt::DisplayRole)
                return QVariant();
        
            // Return the data to which index points.
            return m_data[index.row() * m_numColumns + index.column()];
        }
        
        

        and Mainwindow

        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
        
        
            // Create a matrix.
            const int numRows = 3;
            const int numColumns = 1;
            double matrix[numRows][numColumns];
            for (int i = 0; i < numRows; ++i)
                for (int j = 0; j < numColumns; ++j)
                    matrix[i][j] = i + j;
        
            MatrixModel model(numRows, numColumns, (double*)matrix);
        
        
            ui->tableView->setModel(&model);
        
        
        }
        

        it runs if ui->tableView->setModel(&model); is commented!

        JonBJ 1 Reply Last reply
        0
        • mhsgoudM mhsgoud

          @SGaist said in table model with 3d input:

          QAbstractListModel.

          Thanks, so I can have a different number of table rows/cols for different values of z?

          I tried to subclass QAbstractTableModel but seems I have a segmentation fault

          Matrix model:

          MatrixModel::MatrixModel(int numRows, int numColumns, double* data)
              : m_numRows(numRows),
                m_numColumns(numColumns),
                m_data(data)
          {
          }
          
          int MatrixModel::rowCount(const QModelIndex& parent ) const
          {
              return m_numRows;
          }
          
          int MatrixModel::columnCount(const QModelIndex& parent ) const
          {
              return m_numColumns;
          }
          
          QVariant MatrixModel::data(const QModelIndex& index, int role ) const
          {
              if (!index.isValid() || role != Qt::DisplayRole)
                  return QVariant();
          
              // Return the data to which index points.
              return m_data[index.row() * m_numColumns + index.column()];
          }
          
          

          and Mainwindow

          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
          
          
              // Create a matrix.
              const int numRows = 3;
              const int numColumns = 1;
              double matrix[numRows][numColumns];
              for (int i = 0; i < numRows; ++i)
                  for (int j = 0; j < numColumns; ++j)
                      matrix[i][j] = i + j;
          
              MatrixModel model(numRows, numColumns, (double*)matrix);
          
          
              ui->tableView->setModel(&model);
          
          
          }
          

          it runs if ui->tableView->setModel(&model); is commented!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @mhsgoud
          MatrixModel model is a local variable. You cannot set a view's model to a local variable, since the view persists but the model it points to is destroyed. I would not expect it to crash on the ui->tableView->setModel(&model); statement itself, but on the } following it which ends the function. During the exit from the function all local variables get destroyed. At some point here, or hereafter, the view tries to access a model which no longer exists. Give the model the same scope/lifetime as the view.

          Further in a similar vein, unless the Model constructor copies the matrix parameter to it (does it?) it will be using/pointing to the double matrix[][] array and that is a local variable too. That will leave the model still pointing onto the stack for its matrix data after that stack area has been released/invalidated. Don't make matrix a local variable, unless MatrixModel constructor takes a permanent copy of it.

          Finally, as a stylistic note passing/writing (double*)matrix is considered bad style in C++. Do not use C-style casts any longer. If you must cast, use static_cast<double *>(matrix) here.

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Beside the very good point provide by @JonB, you should design your data structure in such a way that it also supports 3 dimensions.

            One of the first thing to make clear is whether all your matrices have the same size. In any cases, it should be your data structure that provides the information for the model to use. As for managing the third dimension, you should add an API to the model that allows to set the z value and then trigger a refresh of the model when it has changed so that the view can update themselves.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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