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. qt tree view select parent row only
Forum Updated to NodeBB v4.3 + New Features

qt tree view select parent row only

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k 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.
  • T Offline
    T Offline
    tony67
    wrote on last edited by SGaist
    #1

    Hi All,
    hope someone can help me out for this. Sorry if my terminology is incorrect my basic QT is self taught, so I might misuse.
    Basically I have a tree as follows ( say 3 objects )

    OBJECT A
    ---------------a
    ---------------b
    OBJECT B
    -----------------a
    -----------------b
    OBJECT C
    -----------------a
    -----------------b

    If my tree view is not expanded and I try to find what OBJECT is selected I just highlight and get the row ( see below ) so OBJECT A returns 0 B 1 C 2. However if I expand A,B,C and say read OBJECT C sub object a it returns 0 rather than 2. How can I make the subobject return the "master" row? Thanks.
    How I read now

    QModelIndexList returned_rown;
    QItemSelectionModel *select = ui->treeView->selectionModel();
    
    returned_row = select->selectedRows();
    /*no row selected*/
    if( returned_row.size( ) == 0 ) return;
    
    unsigned int row = returned_row.at( 0 ).row( );
    

    [edit: add missing coding tags: 3 `before and after the code SGaist]

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vince Golubic
      wrote on last edited by Vince Golubic
      #2

      Determining the 'line' selected in 'Item selection' can get quite involved so here's one quick example that might that help you can build upon. One thing I do when using QTreeView is the following.
      My apologies if my braces are misaligned (this editor tool is cumbersome).

      First, when an QTreeview Item is selected this 'event' is called:

      First, I define the following in my TreeViewTestClass class (*.h file) :
      void setDataChanged(const QItemSelection&, const QItemSelection&); // for SIGNAL/SLOT

      Then, in the implemented code (*.cpp file) I do the following :

      TreeViewTestClass::setDataChanged ( const QItemSelection &old, const QItemSelection &new)
      {
      QString selectedText[10]; // 10 column QTreeView Table (example)
      QString showString [10];
      QList<QModelIndex> list;
      int n=0;

      // First get 'current index' for the TreeView widget:
      const QModelIndex index = ui->treeView->selectionModel()->currentIndex();

      // Then, create and test the selection model to see if it's valid.
      if ( index.model() != NULL)
      if ( index.isValid() )
      {
      list = ui->treeView->selectionModel()->selectedIndexes();
      if (list.size() !=0) {
      selectedText[0]= ui->treeView->selectionModel()->selectedIndexes().at(0).data(Qt::DisplayRole).toString();
      // assumes 'n' is the TreeView 'key'
      n = ui->treeView->selectionModel()->selectedIndexes().at(1).data(Qt::DisplayRole).toInt();

      	// -- find out hierarchy level of the selected item
      	int hierarchyLevel =1;
      	QModelIndex seekRoot = index;
      	while(seekRoot.parent() !=QModelIndex())
      	{
      		seekRoot = seekRoot.parent();
      		hierarchyLevel++;
      	} 
          showString[0] = QString("%1, Level %2").arg(selectedText[0]).arg(hierarchyLevel);  
          qDebug()<< "Print out line item selected" <<n;
          qDebug()<< "Text = "<< showString[0];
         }
      

      }
      }

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tony67
        wrote on last edited by
        #3

        Thankyou

        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