I am having sequence problem



  • Following sthe post https://forum.qt.io/topic/85736/looking-for-a-sample-example-in-which-qtreewidget-at-left-side-qsplitter-and-qstackedwidget-on-right-side/3

    One more question regarding the sequnce of displayed children

       QModelIndex tempIdx  = mdl->index(0,0);
        mdl->setData(tempIdx,"Parent1");
        mdl->setData(tempIdx,m_stacked->addWidget(QLabel("A"),Qt::UserRole);
        ////////////////////////////////////////
        ///////////////////////////////////////////
        tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"Child1");
        mdl->setData(tempIdx,m_stacked->addWidget(new QTextEdit("Child1",this)),Qt::UserRole);
    
    
        mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildOfChidl1");
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);
    
        mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildAOfChidl1");
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);
    
       mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
        mdl->setData(tempIdx,"ChildBOfChidl1");
        QTableWidget* tempTable1 = new QTableWidget(2,2,this);
        tempTable1->model()->setData(tempTable1->model()->index(0,0),"ChildBOfChild1 Cell 0,0");
        tempTable1->model()->setData(tempTable1->model()->index(1,0),"ChildBOfChild1 Cell" ,0");
        mdl->setData(tempIdx,m_stacked->addWidget(tempTable1),Qt::UserRole);
    

    In my case sequence is a issue ChildBOfChild1 is displayed before ChildAOfChild1 why ?

    [Added code tags ~kshegunov]


  • Moderators

    @Qt-Enthusiast said in I am having sequence problem:

    mdl->insertRow(0,tempIdx);
    QModelIndex tempIdx = mdl->index(0,0,tempIdx);

    1. you are always inserting at position 0
    2. you are overwriting/redefine the tempIdx variable multiple times


  • replace

    mdl->insertRow(0,tempIdx);
        QModelIndex tempIdx = mdl->index(0,0,tempIdx);
    

    with

    const int rowCnt = mdl->rowCount(tempIdx);
    mdl->insertRow(rowCnt ,tempIdx);
    tempIdx = mdl->index(rowCnt ,0,tempIdx);
    

    to insert at the bottom instead of at the top


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.