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. QModelIndex returns wrong InternalPointer
Forum Updated to NodeBB v4.3 + New Features

QModelIndex returns wrong InternalPointer

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.2k 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.
  • M Offline
    M Offline
    me3ntal
    wrote on 9 Aug 2022, 08:56 last edited by
    #1

    I have an own Qt-Model class derieved from QAbstractTableModel. By background data structure consists of Row, Column and Cell classes. So every Row has n Cells whose can bound to their belonging Column. The Cell carries the actual Grid-field-value, Background-Color, Font, etc...

    My index-Method looks like this:

    QModelIndex TableModel::index(int row, int column, const QModelIndex& parent) const
    {
        if (!hasIndex(row, column, parent))
            return QModelIndex();
    
        // modelData is my background data-structure-class
        Cell* cell = modelData->Rows.at(row)->Cells.at(column); // retreiving the cell
    
        if (cell != Q_NULLPTR)
            return createIndex(row, column, cell); // Storing the Cell-object in every index
    
        return QModelIndex();
    }
    

    Now, when the Grid accesses data I have the data-method like this:

    QVariant TableModel::data(const QModelIndex& index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        Cell* cell = static_cast<Cell*>(index.internalPointer()); // retreiving the Cell
        // it works when I say
        // Cell* cell = modelData->Rows.at(index.row()).Cells.at(index.column());
        switch (role)
        {
            case Qt::DisplayRole:     
                ...                                           
                // return the value
                break;
            case Qt::DecorationRole: return cell->Column->Icon;                                  
            case Qt::BackgroundRole: return cell->Column->BackgroundColor;                       
            case Qt::TextAlignmentRole: return static_cast<qint32>(cell->Column->TextAlignment); 
        }
        return QVariant();
    }
    

    My problem is, that inside the data-function the internal-pointer (static_cast<Cell*>(index.internalPointer())) always returns the same Cell (row 0, column 0) and not the Cell according to the row and column coordinate. Because of this my grid has created all rows and columns but their cell-value is always the same because it only uses the Cell from row 0 and column 0 (example below to visualize the problem).
    Retreiving the Cell* via modelData->Rows.at(index.row()).Cells.at(index.column()) works though, but I really have to use the internalPointer because I would like to extend my model with hierarchy (parent-child-relations like a tree) or use the actual Cell* elsewhere.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Aug 2022, 18:43 last edited by
      #2

      Hi,

      Did you base your model on the Simple Tree Model example ?

      Did you test your model using the Qt Model Tester class ?

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

      M 1 Reply Last reply 10 Aug 2022, 06:20
      0
      • S SGaist
        9 Aug 2022, 18:43

        Hi,

        Did you base your model on the Simple Tree Model example ?

        Did you test your model using the Qt Model Tester class ?

        M Offline
        M Offline
        me3ntal
        wrote on 10 Aug 2022, 06:20 last edited by me3ntal 8 Oct 2022, 07:07
        #3

        @SGaist
        I tired the ModelTester and it didn't return anything wrong.

        My model is bit different from the Simple Tree Model Example. There it uses TreeItems with Columns and stores the TreeIem* in the internalPointer(). For me, I create a Row and store every data in Cells. The cells can be assigned to a Column where I store additional properties. When I'd like to get a specific entry from the Model (a ModelIndex so to speak) I take the index.row() and the index.column() and receive my Cell with this "coordinates" (Cell* cell = Rows.at(index.row()).Cells.at(index.column())). I'd like to store this Cell* inside the internalPointer() of the index. My guess is that something goes wrong whilst storing the pointer, although the corresponding Cell* is correct.

        Update:
        When I store my Row* in the internalPointer and get the respective Cell with static_cast<Row*>(index.internalPointer()).Cells.at(index.column()) it does work, but it is not really what I want, because for every row in the ModelIndex it would store the same Row*.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Aug 2022, 19:43 last edited by
          #4

          One silly question but: did you check the pointer that you store in your index method ?

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

          M 1 Reply Last reply 11 Aug 2022, 06:30
          0
          • S SGaist
            10 Aug 2022, 19:43

            One silly question but: did you check the pointer that you store in your index method ?

            M Offline
            M Offline
            me3ntal
            wrote on 11 Aug 2022, 06:30 last edited by
            #5

            @SGaist
            For every Row I create a unique Cell*. In my index-function I receive this pointer (created before) like this: Cell* cell = modelData->Rows.at(row)->Cells.at(column). This is fine, it is returning the right Cell corresponding to the given row and column.
            Or do you mean something else?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Aug 2022, 18:29 last edited by
              #6

              I just realized something: I am wondering whether you might be hitting one of Python's memory handling mechanism.

              Did you check whether you are getting the same index object again and again ?

              What happens if you explicitly use the index method ?

              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
              1
              • M Offline
                M Offline
                me3ntal
                wrote on 4 Jan 2023, 13:55 last edited by me3ntal 1 Apr 2023, 13:59
                #7

                @SGaist
                Okay your guess was pretty good tbh.

                I changed data() to use index() and not the passed QModelIndex-parameter instead

                const QModelIndex& idx = this->index(index.row(), index.column(), index.parent());
                Cell* cell = static_cast<Cell*>(index.internalPointer());
                

                So I am retreiving the corresponding QModelIndex-reference from my index()-method and not from the passed parameter of the data()-method. And this works.

                I compared the addresses of the QModelIndexes from index() and from the parameter of data() and they are obviously different.
                But when I compare the addresses of the internal pointers of both indexes, they are the same. Why can I retreive the Cell* only with a "fresh" reference coming from my index()-method and not with the passed parameter when their stored internalPointer() are the same?

                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