PDA

View Full Version : double clicked on an item QTreeWidget



milli
6th May 2011, 22:01
I am using QTreeWidget on the MainWindow and items are added to the TreeWidget from a table of a database.The tree has four rows and two columns.When i double click either the root or children on the specific row of the tree,then displays another widget.I am doing this using QModelIndex object.I want only the root of the row to "goes to" the specified widget and not the children.

My code is:

//Double click on the treeWidget
void MainWindow::changeSWidget(QModelIndex index)
{
//row 0
case Tree::Computers://1
ui->stackedWidget->setCurrentWidget(comp);
break;
case Tree::Files://1
ui->stackedWidget->setCurrentWidget(file);
break;
}
connect(Tree,SIGNAL(loadWidget(QModelIndex)),this, SLOT(changeSWidget(QModelIndex)));

How can i find which item is clicked ??Doing this when i clicked on each item is called the slot display.
//double click on treewidget items
connect(ui->treeWidget,SIGNAL(itemDoubleClicked(QTreeWidgetIte m*,int)),this,SLOT(display(QTreeWidgetItem*,int))) ;

Is it better for me to use a model/view approach??
i hope you understand what i am saying..

tinysoft
7th May 2011, 10:10
How can i find which item is clicked ??

you can do this by checking the text of the item clicked :


void MainWindow::treeViewClicked(QModelIndex index)
{
if(index.data().toString()=="TableRecipts")
createTableReciptsModel();
}


or, if you have more than row with the same text , you can check both the text and the index :


else if(customersTreeValue=="totalBounty"&&index.row()==0)
{
totalBountyClicked();
}

i hope i was helpful .

milli
7th May 2011, 19:33
your answer helps me to understand how can i do what i wanted...In the treeWidget are stored primary keys of a table and with this piece of code i get the content of the column:
index.sibling(index.row(),2).data().toInt()

Thanks a lot again

tinysoft
7th May 2011, 21:49
u r welcome
i am glad that you solved the problem :)