Results 1 to 2 of 2

Thread: [QT4 & XP] retrieve data from QTreeView's model

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default [QT4 & XP] retrieve data from QTreeView's model

    hi,

    in the following code table is a QTreeView based on a QStandardItemModel with 2 columns:

    Qt Code:
    1. connect( table, SIGNAL( doubleClicked(const QModelIndex&) ),
    2. this, SLOT( selection(const QModelIndex&) ) );
    To copy to clipboard, switch view to plain text mode 

    When the slot is activated, I would like to retrieve the selected data from the model like :
    Qt Code:
    1. void MainWindow::selection(const QModelIndex& idx)
    2. {
    3. QString col0 = model->data(QModelIndex(idx)).toString();
    4. }
    To copy to clipboard, switch view to plain text mode 
    This returns the selected item allright (col0 or col1 depending on the user's selection).
    But I need to retrieve data for both columns.
    How is that achieved ?
    Last edited by incapacitant; 2nd March 2006 at 12:44.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: [QT4 & XP] retrieve data from QTreeView's model

    You can "iterate" in your model by using QModelIndex methods parent(), child(), sibling()..
    One way to fetch data in both colums:
    Qt Code:
    1. QString col0 = model->data(idx.sibling(idx.row(), 0)).toString();
    2. QString col1 = model->data(idx.sibling(idx.row(), 1)).toString();
    To copy to clipboard, switch view to plain text mode 

    Edit: Btw, you don't need to store a pointer to the model as a member variable for this kind of purpose.
    You can reach the data by using only model indexes as well:
    Qt Code:
    1. QString col0 = idx.sibling(idx.row(), 0).data().toString();
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 2nd March 2006 at 13:19.

  3. The following user says thank you to jpn for this useful post:

    incapacitant (2nd March 2006)

Similar Threads

  1. A few queries about Model View Programming
    By montylee in forum Qt Programming
    Replies: 46
    Last Post: 2nd March 2009, 08:36
  2. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  3. Clearing data model
    By db in forum Newbie
    Replies: 6
    Last Post: 16th February 2008, 13:10
  4. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  5. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 17:30

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.