Results 1 to 4 of 4

Thread: Extracting custom QStandardItem from QSortFilterProxyModel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Extracting custom QStandardItem from QSortFilterProxyModel

    Dear All,
    I'm writing an application which displays data in a QListView. I've extended QStandardItem to use for the rows in the QStandardItemModel that the list depends on. I've done this in order to provide an additional ID field for each item displayed. However, when I come to cast the extended QStandardItem when the user clicks on the item, I am unable to extract the ID. I'm new to Qt coming from a Java background so I may be missing something very obvious. The code is below.


    Qt Code:
    1. // from the SAXHandler that populates the QListView
    2. bool AllPlacesXMLHandler::startElement(const QString &namespaceURI,
    3. const QString &localName,
    4. const QString &qName,
    5. const QXmlAttributes &attributes)
    6. {
    7.  
    8.  
    9. if (qName == "place") {
    10. QString name, county, description;
    11.  
    12. for( int i=0; i<attributes.count(); i++ ){
    13. if( attributes.localName( i ) == "name" ){
    14. name = attributes.value( i );
    15. }else if( attributes.localName( i ) == "county" ){
    16. county = attributes.value( i );
    17. }else if( attributes.localName( i ) == "description" ){
    18. description = attributes.value( i );
    19. }
    20. }
    21.  
    22. NTStandardListItem* item1 = new NTStandardListItem();
    23. item1->setText(name + " " + county + " " + description);
    24. item1->setPlaceNameID(name);
    25. iStandardModel->appendRow(item1);
    26. }
    27.  
    28. return true;
    29. }
    30.  
    31.  
    32. //from the class displaying the QListView - I swap the QStandardItemModel for a QSortFilterProxyModel (proxyModel) in order to allow filtering of the list
    33. void AllPlacesWindow::populateListView()
    34. {
    35. //GetDisplayInfo *displayInfo = new GetDisplayInfo();
    36. AllPlacesXMLHandler handler;
    37.  
    38. iStandardModel = handler.readFile();
    39. ui->allPlacesListView->adjustSize();
    40. this->proxyModel->setSourceModel(iStandardModel);
    41. ui->allPlacesListView->setModel(proxyModel);
    42. ui->allPlacesListView->setSelectionMode(QAbstractItemView::SingleSelection);
    43. ui->allPlacesListView->show();
    44.  
    45. }
    46.  
    47. //here is the connect
    48.  
    49. connect(ui->allPlacesListView , SIGNAL(clicked(const QModelIndex &)) , this , SLOT(displayNTPlaceInfo(const QModelIndex &)));
    50.  
    51. //here is the method called when a user clicks on an item in the list
    52. void AllPlacesWindow::displayNTPlaceInfo(QModelIndex index)
    53. {
    54. NTStandardListItem var = ui->allPlacesListView->model()->data(index , Qt::DisplayRole).value<NTStandardListItem>();
    55. qDebug() << "displayNTPlaceInfo :: " << var.getPlaceNameID() << " index :: " << index.data(Qt::DisplayRole);
    56. }
    To copy to clipboard, switch view to plain text mode 


    It's the last line that's the issue - ar.getPlaceNameID() doesn't return anything.

    Any suggestions?

    Thanks in advance

    Angus
    Last edited by Lykurg; 20th January 2011 at 09:23. Reason: missing [code] tags

Similar Threads

  1. Extracting text from QTableWidgetItem
    By bizmopeen in forum Newbie
    Replies: 3
    Last Post: 1st September 2009, 17:28
  2. Get custom data from qstandarditem?
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 11th August 2009, 03:24
  3. Replies: 12
    Last Post: 5th July 2009, 16:03
  4. Extracting files from a zip file
    By Althor in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2009, 10:39
  5. Extracting current rotation
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2007, 11:29

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.