PDA

View Full Version : How to get index number for selected item in QtreeWidget



starlays
6th September 2016, 20:47
I have found this (http://www.qtcentre.org/threads/33214-current-index-of-the-sleected-item-in-QTreeWidget) that it is related to my problem but I dont know how he manage to get
currentIndex()

Can someone help me please with a small POC.
Thank you in advance.

ChrisW67
6th September 2016, 21:53
The current cell,


QModelIndex index = treewidget->currentIndex();
// or
QTreeWidgetItem *item = treeWidget->currentItem();

Or, the selected item(s)


QModelIndexList indexes = treewidget->selectionModel()->selectedIndexes();
// or
QList<QTreeWidgetItem *>items = treeWidget->selectedItems();

starlays
11th September 2016, 10:27
Thank you for your answer. Forgive me but I'm still trying to get the selected index item :(

I have tried this with a slot:



void MainWindow::on_treeWidget_clicked(const QModelIndex &index)
{
QMessageBox::information(this,"Info:",*index);
}


and I get this error:



C:\test\mainwindow.cpp:21: note: 'const QModelIndex' is not derived from 'const QGenericMatrix<N, M, T>'
QMessageBox::information(this,"Info:",*index);
^


I'm trying to learn, please bear with me, trial and error as I cant find up until now documentation for absolute beginner :(.

anda_skoa
11th September 2016, 11:26
The third argument of QMessagBox::information() is a QString, QModelIndex is a different class and there is no immedate conversion from that to QString.

The index carries row and column information, as well as another index for the parent node in the tree.
If you want for format that into a QString, you'll have to write a helper function that does that.

Cheers,
_

starlays
11th September 2016, 12:17
The third argument of QMessagBox::information() is a QString, QModelIndex is a different class and there is no immedate conversion from that to QString.

The index carries row and column information, as well as another index for the parent node in the tree.
If you want for format that into a QString, you'll have to write a helper function that does that.

Cheers,
_

Sorry, I'm still failing in finding a way to get the index of the currently clicked item from the qtreeWidget and display it somehow. To fuzzy for me at the moment, I simply don't know how to do it, this framework is a bad start for nobbies like me.
How can I get the index of the clicked item of the qtreeWidget and display it, it is frustrating at the moment.

Thank you.

anda_skoa
11th September 2016, 13:56
If you want the item that has been clicked, connect to the QTreeWidget::itemClickked() signal.

Cheers,
_

starlays
11th September 2016, 15:20
If you want the item that has been clicked, connect to the QTreeWidget::itemClickked() signal.

Cheers,
_


Thank you, I have found this method before I have started the question, If it was that simple to me then what is the point of asking....



void MainWindow::on_treeWidget_itemClicked(QTreeWidgetI tem *item, int column)
{
// What to put here to get the index?
}

anda_skoa
11th September 2016, 15:51
Can you rephrase what you are actually looking for?

You have solutions for getting the item that was clicked and the model index that was clicked.
The former is part of the "item widget" API of QTreeWidget, the latter is part of the "abstract view" API.

Do you need the item and the index at the same time?

Cheers,
_

starlays
11th September 2016, 16:15
Can you rephrase what you are actually looking for?

You have solutions for getting the item that was clicked and the model index that was clicked.
The former is part of the "item widget" API of QTreeWidget, the latter is part of the "abstract view" API.

Do you need the item and the index at the same time?

Cheers,
_

I want to get the index as a int of the row the user had clicked.
The user clicks on the row from the tree view widget and I want to be able to determine the index of that row and after that I want to use that index to manipulate a tab-view widget.

The name of the item doesn't matter, only the index of the row from the tree-view widget on witch the user has clicked.

I have tried with to trigger an action when the user clicks at one of the rows from the qtreewidget and get the index of what the user has clicked like so:


void MainWindow::on_treeWidget_itemClicked(QTreeWidgetI tem *item, int column)
{
QModelIndex index = item->treeWidget()->currentIndex();
}


I think it is not correct what I'm doing here and I don't know how to solve this problem how to get the index of the user selected row when a user clicks on the row from the qtreewidget. Hope I manage to explain correctly what I want to achieve now.

d_stranz
11th September 2016, 18:20
I want to get the index as a int of the row the user had clicked.

I think there are two problems with your understanding of QTreeWidget and QModelIndex.

First, the QTreeWidget is a tree, not a list. Imagine the structure of a real tree. Starting at the root and trunk of the, it branches into a set of big limbs. Each of these big limbs splits into smaller limbs, and the limbs into branches, and so on, until you finally arrive at the leaves at the ends of the smallest branches.

If you consider any leaf in the tree, what "row" is it in? You can't say, because the tree is hierarchical.

However, you could say, "It is the fifth leaf on the second branch of the fourth limb off of the main trunk". This makes sense - if you assume there is some built-in order of branches. You start at the trunk, find the fourth limb (say moving clockwise from North). Once you find that, go to its second branch. The leaf you want is the fifth one.

If you translate this into programming terms, there is an ownership or parent/child relationship between all of the branches and leaves of the tree. The fifth leaf has a "parent", th second branch. The second branch has a "parent" in the fourth limb. The fourth limb's parent is the trunk (or root).

This is the structure used to represent a Qt tree in QTreeWidget or QTreeView. The QModelIndex is Qt's way of telling you exactly where you are in the tree. It works together with the QAbstractItemModel (which holds the information displayed in the tree) to help you navigate around the tree.

So, I think your second problem of understanding is that the "row" in QModelIndex is not the count of items displayed in the tree view counting from the top of the display. It refers to the "nth" child of whatever the QModelIndex's parent is. It is not an absolute position in the tree, it is relative to its parent.

In addition to each node in a Qt tree having one or more rows, each of these rows can have one or more columns in it (think of the tree in a file browser - for each file or directory, there is a list of attributes - name, date modified, size, permissions, etc.). These are the columns of the given row.

A QModelIndex therefore tells you what row you are in the tree with respect to the item's parent and what column you are in the row counting from the left. If you want to find your absolute position in the tree, you have to go up the tree by asking for the parent of your item until you finally reach the top (root) of the tree, which is an "invalid" QModelIndex (QModelIndex::isValid() returns false).

Finally, I think you misunderstand how QMessageBox works. It isn't a general purpose "print" statement that displays anything in a message dialog box. It displays a QString. If what you want to show is not immediately convertible to a QString (like the string literal "This is my message"), then you have to format what you want to display into a QString for display.

In your case, something like this:



void MainWindow::on_treeWidget_clicked(const QModelIndex &index)
{
QString message = QString( "Item at model index row %1, column %2 clicked" ).arg( index.row() ).arg( index.column() );
QMessageBox::information(this,"Info:", message );
}

// or using the other slot

void MainWindow::on_treeWidget_itemClicked(QTreeWidgetI tem *item, int column)
{
QModelIndex index = item->treeWidget()->currentIndex();

QString message = QString( "Item at model index row %1, column %2 clicked" ).arg( index.row() ).arg( index.column() );
QMessageBox::information(this,"Info:", message );

}


By the way, QTreeWidgetItem "hides" the multiple columns in each row. A QTreeWidgetItem represents the entire row at a certain level in the tree. So you can ask it for its columnCount() and get the data in a particular column. QModelIndex is more general - it knows it has siblings and children, but it can't tell you how many.

starlays
11th September 2016, 20:31
@d_stranz Interesting overview for me now, this is quality explanation for me. Thank you, I have manage to understand some things better now and I will play more with what I have learned now and I will try to learn more out of it.

Thank you guys, help and lessons appreciated, hope I can payback somehow.