Results 1 to 15 of 15

Thread: Accessing the first data item from a IndexList.

  1. #1
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Accessing the first data item from a IndexList.

    Hello,

    I'm trying to get the data from the first cell of an IndexList that gets its data from a selection model, which in turn is called to pass said data into a QVariant variable. However, I'm unsure on how to get the first (or whichever) data item from said list. Instead, the data from the currently selected cell is transfered to my QVariant variable, which leads to problems.

    So say I have a row of items: NAME | DATE | NOTES

    And I want to edit this row (which gets its data from a linked list), so I click on the NOTES cell (whichever one), click my edit button that will get the data ASSOCIATED WITH THE "NAME" data from said linked list, change tabs, and present said data for editing. So basically, if I click on the NOTES or DATE cell, I want to be able to get data from their respective NAME cell.

    I was trying to do this (amongst other attempts), but miserably:
    Qt Code:
    1. QVariant target;
    2.  
    3.  
    4. selectionModel = ui->taskView->selectionModel();
    5. QModelIndexList editIndex = selectionModel->selectedRows();
    6.  
    7. if (!editIndex.isEmpty()) {
    8.  
    9. QMessageBox::information(this, tr("Prepare to Edit!"), tr("Checkpoint 1"));
    10.  
    11. target = (editIndex.first()).data(); //Should get the name data
    12.  
    13. QMessageBox::information(this, tr("Prepare to Edit!"), tr("\"%1\" - Checkpoint 2").arg(target.toString()));
    14.  
    15. tlink.find(target.toString(), name);
    16.  
    17. QMessageBox::information(this, tr("Prepare to Edit!"), tr("Checkpoint 3"));
    18.  
    19. QMessageBox::information(this, tr("Prepare to Edit!"), tr("\"%1\"'s task will now be edited.").arg(name));
    20. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for the help!
    - Bona

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    If I understand what you want to do correctly, this should get the "name" from a clicked row:
    Qt Code:
    1. selectionModel->data(editIndex.row(), 0)
    To copy to clipboard, switch view to plain text mode 
    Change the "0" to: "1" for "date"; "2" for "notes".

    EDIT: Correction, left out "first()"
    Qt Code:
    1. selectionModel->data(editIndex.first().row(), 0)
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 25th February 2010 at 17:22. Reason: Typed before thinking

  3. #3
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    Quote Originally Posted by norobro View Post
    If I understand what you want to do correctly, this should get the "name" from a clicked row:
    Qt Code:
    1. selectionModel->data(editIndex.row(), 0)
    To copy to clipboard, switch view to plain text mode 
    Change the "0" to: "1" for "date"; "2" for "notes".

    EDIT: Correction, left out "first()"
    Qt Code:
    1. selectionModel->data(editIndex.first().row(), 0)
    To copy to clipboard, switch view to plain text mode 
    No, that doesn't work. Data isn't a function of QItemSelectionModel.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    Then this should work:
    Qt Code:
    1. selectionModel->model()->data(editIndex.first().row(), 0)
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    With that, I get this error:
    K:/ProjectManagement/PMGUI/pmgui.cpp:171: error: no matching function for call to 'QAbstractItemModel::data(int, int) const'
    Here's the code:
    Qt Code:
    1. selectionModel = ui->taskView->selectionModel();
    2. QModelIndexList editIndex = selectionModel->selectedRows();
    3.  
    4. target = selectionModel->model()->data(editIndex.first().row(), 0);
    To copy to clipboard, switch view to plain text mode 

    Do I keep the first two lines I had before pasted above?

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    I apologize for the wrong answers. Suffice it to say I need to work on my reading comprehension (first answer) and I should look at the docs before answering (second answer).

    From the QAbstractItemModel docs:
    virtual QVariant data ( const QModelIndex & index, int role = Qt:: DisplayRole ) const = 0
    I think this will work:
    Qt Code:
    1. target = selectionModel->model()->data(editIndex.first());
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    Quote Originally Posted by Bonafide View Post
    Do I keep the first two lines I had before pasted above?
    Yes, keep those two lines.

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    My last post will just put you back to where you were.

    First use the name of the model you're using in your view (viewModelName) in lieu of "selectionModel->model()" which will work but makes the statement harder to read.

    Then to access your data you would need to use this statement:
    Qt Code:
    1. target = viewModelName->data(viewModelName->index(editIndex.first().row(),0))
    To copy to clipboard, switch view to plain text mode 

    "1" for "date"; "2" for "notes"

    Again sorry for the confusion.

  9. The following user says thank you to norobro for this useful post:

    Bonafide (26th February 2010)

  10. #9
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    Once again, that doesn't work..

    Now, I'm getting a runtime error, as seen in the picture below. The selectedRows() function, doesn't return the complete row of the index selectd in the model (unless you select multiple columns in a specific row, and then just gets the first data item from the first selected cell), so I'm not sure if that has something to do with the error.

    I've been trying to get this to work for quite some time today, but it's proving futile! I do appreciate the help, though, so don't think I don't!

    The model that is being utilized in the program is named "model."
    Qt Code:
    1. QVariant target;
    2.  
    3.  
    4. selectionModel = ui->taskView->selectionModel();
    5. QModelIndexList editIndex = selectionModel->selectedRows();
    6.  
    7. target = model->data(model->index(editIndex.first().row(), 0));
    To copy to clipboard, switch view to plain text mode 



    Edit: If I select the ENTIRE row, the code works, but that's not what I'm aiming for.
    Last edited by Bonafide; 26th February 2010 at 03:41.

  11. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    Will this work?
    Qt Code:
    1. QModelIndexList QItemSelectionModel::selectedIndexes () const
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to norobro for this useful post:

    Bonafide (26th February 2010)

  13. #11
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    No, I tried that. selectedIndexes() only returns the index of the currently selected cell I believe, at least that's what I've assumed from testing it.

  14. #12
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    Eureka!

    This code successfully selects the first column of a row from any selected cell in said row, and returns it to a variable.

    Qt Code:
    1. QVariant target;
    2.  
    3. selectionModel = ui->taskView->selectionModel();
    4.  
    5. QModelIndex in = selectionModel->currentIndex();
    6.  
    7. target = model->data(model->index(in.row(), 0));
    To copy to clipboard, switch view to plain text mode 

    Many thanks, norobro, for getting me on the right track! You will be "thanked!"

  15. #13
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Accessing the first data item from a IndexList.

    But if you have the index of the currently selected cell, you can get it's row, so it looks like "target = model->data(model->index(editIndex.first().row(), 0));" would then work.

    Edit: Glad you got it working.
    Last edited by norobro; 26th February 2010 at 04:07.

  16. #14
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    The problem was, selectedRows() or selectedIndexes() only returned the value of the selected cell(s); it does not return the indexes of all values in the row. So the QModelIndexList editIndex would only recieve the data from "NOTES" if it was the only cell selected. It would only work properly if the entire row was selected, thus returning the correct index and it's data. Otherwise, it only returned the data of the first index in editIndex, which may or may not of been what I wanted, and the rest of the code would then look for a value that never existed.

    With what I did, I simply got the index of the the cell, then from that index, get the row value of the index (passed in the "row" parameter of model-index(row, column), and then specificied the column I wanted.

  17. #15
    Join Date
    Feb 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing the first data item from a IndexList.

    But once again, thanks for getting me on the right track. Your usage of of the index() function of model and .row(), got the juices flowin'.

Similar Threads

  1. QTableView : accessing the data
    By marvaneke in forum Newbie
    Replies: 10
    Last Post: 30th March 2012, 11:31
  2. Multiple File Data accessing
    By hasnatzaidi in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 16:34
  3. QTableView - data() - item
    By starcontrol in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2008, 14:41
  4. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  5. accessing data in a QFile
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 16:25

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.