qt tree view select parent row only
-
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 nowQModelIndexList 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]
-
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/SLOTThen, 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]; }
}
} -
Thankyou