[SOLVED] QTreeView - getting the current item via the model index



  • My task is to create an XML editor. I have inherited from QStandardItem and QTreeView -> I have myStandardItems in myTreeView.

    Now I want to insert a new row. m_model->itemFromIndex() gives me a QStandardItem which I cast to a myStandardItems to get at the additional info I need to determine if I can do this action (I can only add a new row under a specific type of parent so myStandardItem has a m_type enum). This is not working because all I am getting back is the QStandardItem data, m_type and my other info have been lost - I know I have the correct item because the text is correct when I am stepping through the code.

    I tried this same principle with QTreeWidget and QTreeWidgetItem (i.e. use derived classes then cast the result of myTreeView::currentItem() to a myTreeWidgetItem) and m_type is available to me with the correct value. Unfortunately, QTreeWidget is not what I need for the specialised 'drag and drop' and only having one editable column functionality.

    Any pointers on how to get my derived item out of the model please?



  • It is difficult to answer this. First of all, a look to more code could heölp. How do you store the items in the model? How is your model build?



  • You can try to store additional info in user's data field (i.e. "QStandartItem::data(...)":http://qt-project.org/doc/qt-4.8/qstandarditem.html#data).



  • Thanks guys - it was a typo, doh!

    On a side note - how do I promote this to solved????



  • [quote author="Dolphin" date="1352885041"]
    On a side note - how do I promote this to solved????[/quote]

    You can edit your first post and preprend "Solved" to the title.



  • Very high tech!



  • Unfortunately there is no other way to mark a thread as "Solved", However suggestions have been made and it may reflect soon . You can check for the updates and test on "QnA Testing Area":http://qt-project.org/forums/viewforum/46/



  • Just thought I was missing a button or menu option :-)

    PS I have a simplified version of my code below which gives me 4 top level rows each containing 4 rows. The problem is how to delete the inner rows? Nothing I have tried will remove an "AA" row and some of the options will not remove any "One" rows either. It seems like this should be obvious but no joy!

    @ for(int row(0); row < 4; row++)
    {
    QStandardItem* parent = new QStandardItem("One");
    m_model->appendRow(parent);

    for(int row2(0); row2 < 4; row2++)
    {
    QStandardItem* item = new QStandardItem("AA");
    parent->appendRow(item);
    }
    }
    @



  • :-)

    @ QModelIndex index = m_view->currentIndex();
    if(index.isValid())
    {
    CMenuTreeRow *current = (CMenuTreeRow *) m_model->itemFromIndex(index);
    if(current)
    {
    int currentRow = current->row();
    CMenuTreeRow *parent = (CMenuTreeRow *) current->parent();
    if(parent)//we are at top of tree - do not delete!
    {
    QModelIndex parentIndex = parent->index();

    m_model->removeRow(currentRow, parentIndex);
    

    }
    }
    }@


Log in to reply
 

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