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. [SOLVED] QTreeView - getting the current item via the model index
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 15.8k 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.
  • D Offline
    D Offline
    Dolphin
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AlekseyOk
        wrote on last edited by
        #3

        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).

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dolphin
          wrote on last edited by
          #4

          Thanks guys - it was a typo, doh!

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dolphin
              wrote on last edited by
              #6

              Very high tech!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                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/

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dolphin
                  wrote on last edited by
                  #8

                  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);
                  }
                  }
                  @

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Dolphin
                    wrote on last edited by
                    #9

                    :-)

                    @ 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);
                    

                    }
                    }
                    }@

                    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