PDA

View Full Version : Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &index)



mekos
5th December 2007, 21:38
Hello Everyone..

i've created a QStandardItemModel named userModel that goes to userTreeView and a slot: getSelectedUser(const QModelIndex &index),which listens to the default signal: clicked(const QModelIndex &index).This is the implementation of the getSelectedUser function:
----------------------------------------------------------------------------------
void MainWindow::getSelectedUser(const QModelIndex &index)
{
QStandardItem *user=userModel->itemFromIndex(index);
}
---------------------------------------------------------------------------------

..and the connection in the MainWindow:

---------------------------------------------------------------------------------
connect(userTreeView, SIGNAL(clicked(QModelIndex)),this, SLOT(getSelectedUser(QModelIndex)));
---------------------------------------------------------------------------------
the signal works fine and the index takes the column and row of the clicked point but the QStandardItem *user gives 0x0 in the debugger..

I would appreciate any help.

(p.s in the end the getSelectedUser function will have to retun a QString -now is void for debugging-..so if anyone knows how to get the QString right away instead of the QStandardItem, it would be great)

Thanks

marcel
5th December 2007, 21:44
I am pretty sure that the signal is clicked(const QModelIndex&).
You can return a QString by getting the QStandardItem::text().

Edit: what does index.isValid() say?

mekos
5th December 2007, 21:49
Hello

i've tried :
connect(userTreeView, SIGNAL(clicked(const QModelIndex&)),this, SLOT(getSelectedUser(const QModelIndex&)));

and


connect(userTreeView, SIGNAL(clicked(const QModelIndex&)),this, SLOT(getSelectedUser(QModelIndex)));


the QStandardItem *user sees 0x0 for both of these slots..i cant understand how the index return the row/column that clicked but user gets 0x0..

marcel
5th December 2007, 21:53
The correct version is:


connect(userTreeView, SIGNAL(clicked(const QModelIndex&)),this, SLOT(getSelectedUser(const QModelIndex&)));


Do you have by any chance a QStandardItem class member named "user"?
Where do you see that user is NULL?

mekos
5th December 2007, 22:13
ok i've changed the signal the way you told..

1)no i don't have a QStandardItem class member with the name "user"
2)i still see the 0x0 in the debugging for both adress and value of user

edit:index.isValid() returns true;

wysota
5th December 2007, 22:31
Did you convert the index to the standard item by using the appropriate method of the model?

mekos
5th December 2007, 22:41
Hello wysota

if you mean something like:

QString user =userModel->itemFromIndex(index)->data().toString();

i did but the programm crashes with QSting user Value=unable to evaluate value(debugger output).

if you mean something else please post the code because i'm really new to Qt.

Thanks