PDA

View Full Version : is it possible to get current item selected from itemClicked(QmodelIndex temp)



aurora
3rd January 2012, 06:11
Hi i have a problem...In my tree widget i wanted to know the selected (actually checked) item index
for that i used signal

connect(ui->treeWidget_FilesSelected,SIGNAL(clicked(QModelInde x)),this,SLOT(GetSelectedSignal(QModelIndex)));



Now i wanted along with index i needed current item too, for that one more signal i got

connect(ui->treeWidget_FilesSelected,SIGNAL(itemClicked(QTreeW idgetItem*,int)),this,SLOT(GetSelectedSignalItm(QT reeWidgetItem*,int)));

but i wanted a signal to get index and current item at a same time....how can i do that?
please help me...i tried in manual but didnt understand how can i get both index and current item at same time...

ChrisW67
3rd January 2012, 08:24
To be clear, the current item, which is generally changed as the result of clicking, and the selection are two different things. Whether a checkable item is checked or not has no relationship to either the current item or the selection.

In your clicked signal handling slot:

If you want the value of the item clicked on, or the state of its check box, then just use the index to fetch it using QModelIndex::data(). Use Qt::CheckStateRole, Qt::DisplayRole etc.
If you want a pointer to the item clicked on then use QTreeWidget::itemFromIndex(). From this you can inspect the item's checkState().
If you want the current list of items selected (not checked) in the widget then you can call QTreeWidget::selectedItems().

aurora
3rd January 2012, 09:12
Thank u Chris for your reply....
sorry to say that i didnt get exactly how should i have to try to get the check state...
i tried as below....i think as i'm new to Qt i'm doing some silly mistake... Please help me to sort it out...

The signal i declared...

connect(ui->treeWidget_FilesSelected,SIGNAL(clicked(QModelInde x)),this,SLOT(GetSelectedSignal(QModelIndex)));



In the slot i'm trying to get the index of checked child of the tree....



void MainWindow::GetSelectedSignal(QModelIndex temp)
{

bool check;
check=temp(Qt::CheckState());
int t=temp.row();

if (check)
{

LSignalForScript.append(QString::number(t));
LSignalForScript.removeDuplicates();

}
else
{
LSignalForScript.removeOne(QString::number(t));

}

}


I'm getting compilation error saying that...
"no match for call to (QModelIndex)(Qt::CheckState)"

ChrisW67
4th January 2012, 00:44
Qt::CheckState state = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
switch (state) {
case Qt::Checked:
// do stuff
break;
case Qt::Unchecked:
// do other stuff
break;
case Qt::PartiallyChecked:
// you might have these too
break;
}

This only tells you the check state of the item the user clicked on, not any others in the tree. If you want the check state of everything in the tree then you need to iterate over the entire tree to look at each.