Results 1 to 7 of 7

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

  1. #1
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

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

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &in

    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?

  3. #3
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &in

    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..

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &in

    The correct version is:
    Qt Code:
    1. connect(userTreeView, SIGNAL(clicked(const QModelIndex&)),this, SLOT(getSelectedUser(const QModelIndex&)));
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &in

    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;
    Last edited by mekos; 5th December 2007 at 23:29.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &in

    Did you convert the index to the standard item by using the appropriate method of the model?

  7. #7
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Unable to get the QString from a QTreeView (using : clicked(const QModelIndex &in

    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

Similar Threads

  1. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.