I have found this that it is related to my problem but I dont know how he manage to get
Can someone help me please with a small POC.
Thank you in advance.
I have found this that it is related to my problem but I dont know how he manage to get
Can someone help me please with a small POC.
Thank you in advance.
The current cell,
Or, the selected item(s)Qt Code:
// orTo copy to clipboard, switch view to plain text mode
Qt Code:
QModelIndexList indexes = treewidget->selectionModel()->selectedIndexes(); // or QList<QTreeWidgetItem *>items = treeWidget->selectedItems();To copy to clipboard, switch view to plain text mode
"We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.
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:
Qt Code:
{ }To copy to clipboard, switch view to plain text mode
and I get this error:
Qt Code:
C:\test\mainwindow.cpp:21: note: 'const QModelIndex' is not derived from 'const QGenericMatrix<N, M, T>' ^To copy to clipboard, switch view to plain text mode
I'm trying to learn, please bear with me, trial and error as I cant find up until now documentation for absolute beginner.
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.
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....
Qt Code:
{ // What to put here to get the index? }To copy to clipboard, switch view to plain text mode
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:
Qt Code:
{ }To copy to clipboard, switch view to plain text mode
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.
I think there are two problems with your understanding of QTreeWidget and QModelIndex.I want to get the index as a int of the row the user had clicked.
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:
Qt Code:
{ } // or using the other slot { }To copy to clipboard, switch view to plain text mode
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.
Last edited by d_stranz; 11th September 2016 at 18:27.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
@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.
Bookmarks