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. QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 )
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 )

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 851 Views 2 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.
  • S Offline
    S Offline
    Stephan Lorenzen
    wrote on last edited by
    #1

    Dear Forum,

    I created a QTreeView which I can fill with several items as children of rootItem. When I then insert a child to the first child of the tree (so the child is root->(0,0)->(0,0)), I get an error message on the screen:

    QAbstractItemModel::endInsertRows:  Invalid index ( 2 , 0 ) in model QAbstractItemModel(0x5564d96a0150)
    

    When I insert other children, e.g. root->(0,0)->(1,0) or root->(1,0)->(0,0), this error does not appear. After inserting the child, I select the newly created row. That also works for all other children, but not for root->(0,0)->(0,0). There, I end up with a "half-way" selection (see picture): the child is "a bit selected" but the parent row, root->(0,0), stays fully selected. Is that a bug or a my mistake?

    qt6.png
    The relevant code pieces are

    QModelIndex TreeModel::addKurs(Kurs* k)
    {
    	QModelIndex idx = index(k->semester());
    
    	beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount());
    	k->semester()->addKurs(k);
    	endInsertRows();
    
    	emit(layoutChanged());
    
    	QModelIndex idx2 = createIndex(k->row(), 0, k);
    	
    	return idx2;
    }
    
    
    void HagenForum::rightClickTree()
    {
    	TreeItem *source = treeModel()->treeItem(treeView()->currentIndex());
    	if (source->type() == TreeItem::Type::Semester)
    	{
    		Semester *sem = reinterpret_cast<Semester*>(source);
    		SemDialog sd(sem, this);
    
    		if (sd.exec() == QDialog::DialogCode::Accepted)
    		{
    			QModelIndex idx = treeModel()->addKurs(sd.kurs());
    			QTreeView *tv = treeView();
    
    			//must select with delay, otherwise parent Semester will be selected
    			QTimer::singleShot(100, [tv, idx]()
    			{
    				tv->setCurrentIndex(idx);
    			});
    		};
    	}
    }
    
    

    I double checked that k->semester()->childCount is correct (0 before insertion, 1 after insertion). Where does that illegal index (2,0) come from??

    Any help is appreciated!

    Christian EhrlicherC Pl45m4P 2 Replies Last reply
    0
    • S Stephan Lorenzen

      Dear Forum,

      I created a QTreeView which I can fill with several items as children of rootItem. When I then insert a child to the first child of the tree (so the child is root->(0,0)->(0,0)), I get an error message on the screen:

      QAbstractItemModel::endInsertRows:  Invalid index ( 2 , 0 ) in model QAbstractItemModel(0x5564d96a0150)
      

      When I insert other children, e.g. root->(0,0)->(1,0) or root->(1,0)->(0,0), this error does not appear. After inserting the child, I select the newly created row. That also works for all other children, but not for root->(0,0)->(0,0). There, I end up with a "half-way" selection (see picture): the child is "a bit selected" but the parent row, root->(0,0), stays fully selected. Is that a bug or a my mistake?

      qt6.png
      The relevant code pieces are

      QModelIndex TreeModel::addKurs(Kurs* k)
      {
      	QModelIndex idx = index(k->semester());
      
      	beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount());
      	k->semester()->addKurs(k);
      	endInsertRows();
      
      	emit(layoutChanged());
      
      	QModelIndex idx2 = createIndex(k->row(), 0, k);
      	
      	return idx2;
      }
      
      
      void HagenForum::rightClickTree()
      {
      	TreeItem *source = treeModel()->treeItem(treeView()->currentIndex());
      	if (source->type() == TreeItem::Type::Semester)
      	{
      		Semester *sem = reinterpret_cast<Semester*>(source);
      		SemDialog sd(sem, this);
      
      		if (sd.exec() == QDialog::DialogCode::Accepted)
      		{
      			QModelIndex idx = treeModel()->addKurs(sd.kurs());
      			QTreeView *tv = treeView();
      
      			//must select with delay, otherwise parent Semester will be selected
      			QTimer::singleShot(100, [tv, idx]()
      			{
      				tv->setCurrentIndex(idx);
      			});
      		};
      	}
      }
      
      

      I double checked that k->semester()->childCount is correct (0 before insertion, 1 after insertion). Where does that illegal index (2,0) come from??

      Any help is appreciated!

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Stephan-Lorenzen said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

      Where does that illegal index (2,0) come from??

      Don't know but I would set a breakpoint on QAbstractItemModel::endInsertRows() and see where it really comes from. What does e.g. k->semester()->addKurs(k); do?

      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
      1
      • S Stephan Lorenzen

        Dear Forum,

        I created a QTreeView which I can fill with several items as children of rootItem. When I then insert a child to the first child of the tree (so the child is root->(0,0)->(0,0)), I get an error message on the screen:

        QAbstractItemModel::endInsertRows:  Invalid index ( 2 , 0 ) in model QAbstractItemModel(0x5564d96a0150)
        

        When I insert other children, e.g. root->(0,0)->(1,0) or root->(1,0)->(0,0), this error does not appear. After inserting the child, I select the newly created row. That also works for all other children, but not for root->(0,0)->(0,0). There, I end up with a "half-way" selection (see picture): the child is "a bit selected" but the parent row, root->(0,0), stays fully selected. Is that a bug or a my mistake?

        qt6.png
        The relevant code pieces are

        QModelIndex TreeModel::addKurs(Kurs* k)
        {
        	QModelIndex idx = index(k->semester());
        
        	beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount());
        	k->semester()->addKurs(k);
        	endInsertRows();
        
        	emit(layoutChanged());
        
        	QModelIndex idx2 = createIndex(k->row(), 0, k);
        	
        	return idx2;
        }
        
        
        void HagenForum::rightClickTree()
        {
        	TreeItem *source = treeModel()->treeItem(treeView()->currentIndex());
        	if (source->type() == TreeItem::Type::Semester)
        	{
        		Semester *sem = reinterpret_cast<Semester*>(source);
        		SemDialog sd(sem, this);
        
        		if (sd.exec() == QDialog::DialogCode::Accepted)
        		{
        			QModelIndex idx = treeModel()->addKurs(sd.kurs());
        			QTreeView *tv = treeView();
        
        			//must select with delay, otherwise parent Semester will be selected
        			QTimer::singleShot(100, [tv, idx]()
        			{
        				tv->setCurrentIndex(idx);
        			});
        		};
        	}
        }
        
        

        I double checked that k->semester()->childCount is correct (0 before insertion, 1 after insertion). Where does that illegal index (2,0) come from??

        Any help is appreciated!

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Stephan-Lorenzen

        Which line produces the error message? Where does it stop?

        One thing I've noticed, is that you have this block:

        beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount());
        k->semester()->addKurs(k);
        endInsertRows();
        

        The documentation states:

        An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.

        If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
        I'm wondering why you use this begin and end functions, when you dont implement insertRows().

        I really dont understand, what you are doing anyway. Everything looks a bit "fishy"...
        Especially the line between begin and end:
        k->semester()->addKurs(k);


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        Christian EhrlicherC S 2 Replies Last reply
        0
        • S Offline
          S Offline
          Stephan Lorenzen
          wrote on last edited by
          #4

          Hi Christian,

          k->semester()->addKurs(Kurs *k) just appends the new child to the vector of children.

          What do you mean with a breakpoint, some debugger examining that part of the code? That's kind of embarassing, but I have no experience with debuggers, I usually just insert hundreds of

          std::cerr << "I am line 467 of gobbledigoo()" << std::endl;
          

          in my code for that purpose...

          I just found out that the same behavior also appears "one level below", i.e. if I add root->(0,0)->(0,0)->(0,0), I get error messages

          QAbstractItemModel::endInsertRows:  Invalid index ( 1 , 2 ) in model QAbstractItemModel(0x557156028710)
          QAbstractItemModel::endInsertRows:  Invalid index ( 1 , 0 ) in model QAbstractItemModel(0x557156028710)
          

          other "third generation"-children behave as they should.

          1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Stephan-Lorenzen

            Which line produces the error message? Where does it stop?

            One thing I've noticed, is that you have this block:

            beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount());
            k->semester()->addKurs(k);
            endInsertRows();
            

            The documentation states:

            An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.

            If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
            I'm wondering why you use this begin and end functions, when you dont implement insertRows().

            I really dont understand, what you are doing anyway. Everything looks a bit "fishy"...
            Especially the line between begin and end:
            k->semester()->addKurs(k);

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

            Which line produces the error message? Where does it stop?

            e.g. with QT_FATAL_WARNINGS or qInstallMessageHandler() or search in the Qt sources and set the breakpoint directly there.

            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
            0
            • Pl45m4P Pl45m4

              @Stephan-Lorenzen

              Which line produces the error message? Where does it stop?

              One thing I've noticed, is that you have this block:

              beginInsertRows(idx, k->semester()->childCount(), k->semester()->childCount());
              k->semester()->addKurs(k);
              endInsertRows();
              

              The documentation states:

              An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.

              If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
              I'm wondering why you use this begin and end functions, when you dont implement insertRows().

              I really dont understand, what you are doing anyway. Everything looks a bit "fishy"...
              Especially the line between begin and end:
              k->semester()->addKurs(k);

              S Offline
              S Offline
              Stephan Lorenzen
              wrote on last edited by
              #6

              @Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

              If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
              I'm wondering why you use this begin and end functions, when you dont implement insertRows().

              Then I do not quite understand the concept of Qt. I looked at some examples, and insertRows() usually does nothing besides calling beginInsertRows(), appending something to a QVector and calling endInsertRows() afterwards -- exactly the same that I do in addKurs(), as far as I understand. So, I thought I did not need an extra function doing that but can also do the same three lines in TreeModel::addKurs().

              What is the difference between calling insertRows() and writing the code in addKurs()?

              Pl45m4P 1 Reply Last reply
              0
              • S Stephan Lorenzen

                @Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

                If addKurs() works as your insertRows() implementation, the line between begin and end is invalid or may cause issues.
                I'm wondering why you use this begin and end functions, when you dont implement insertRows().

                Then I do not quite understand the concept of Qt. I looked at some examples, and insertRows() usually does nothing besides calling beginInsertRows(), appending something to a QVector and calling endInsertRows() afterwards -- exactly the same that I do in addKurs(), as far as I understand. So, I thought I did not need an extra function doing that but can also do the same three lines in TreeModel::addKurs().

                What is the difference between calling insertRows() and writing the code in addKurs()?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @Stephan-Lorenzen said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

                What is the difference between calling insertRows() and writing the code in addKurs()?

                Of course you can write your own "API" to handle your model / data.
                But you should really debug your code to check your "live" values and to see what's going on. It's hard to say how your data / model and indices really behave while inserting.

                If you use QtCreator you can set a breakpoint by clicking next to the number of your line of code. A red dot appears and the debugger stops there, so you can check your vars and result.

                k->semester()->addKurs(k)
                

                This looks like some sort of parent/child "circle". Semester is a part of Kurs but then you add the same Kurs k to your Semester. Probably k gets re-parented, depending on what Semester::addKurs() does.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                Christian EhrlicherC 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @Stephan-Lorenzen said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

                  What is the difference between calling insertRows() and writing the code in addKurs()?

                  Of course you can write your own "API" to handle your model / data.
                  But you should really debug your code to check your "live" values and to see what's going on. It's hard to say how your data / model and indices really behave while inserting.

                  If you use QtCreator you can set a breakpoint by clicking next to the number of your line of code. A red dot appears and the debugger stops there, so you can check your vars and result.

                  k->semester()->addKurs(k)
                  

                  This looks like some sort of parent/child "circle". Semester is a part of Kurs but then you add the same Kurs k to your Semester. Probably k gets re-parented, depending on what Semester::addKurs() does.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Pl45m4 said in QAbstractItemModel::endInsertRows: Invalid index ( 2 , 0 ):

                  This looks like some sort of "circle". Semester is a part of Kurs but then you add the same Kurs k to your Semester.

                  That's also my idea - therefore the hint with the debugger to see the stack trace.

                  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
                  0
                  • S Offline
                    S Offline
                    Stephan Lorenzen
                    wrote on last edited by
                    #9

                    No, Semester is the parent of Kurs. Semester has a vector of Kurs*, and each Kurs has a poinzter to its parent, a Semester.

                    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