Results 1 to 4 of 4

Thread: Extracting custom QStandardItem from QSortFilterProxyModel

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Extracting custom QStandardItem from QSortFilterProxyModel

    Hi,

    you could use QStandardItemModel::itemFromIndex() and cast it your item type, but even better is to use QStandardItem::setData() when creating your item and retrieve the id back using QStandardItem::data(). Thus you may be able to skip your item subclass.


    Lykurg

    P.s.: Please use the [code] tags next time.

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

    Default Re: Extracting custom QStandardItem from QSortFilterProxyModel

    Hi Lykurg,
    thanks for the reply.
    1) How would I perform the cast? I'm new to Qt and rusty on C++!
    2) I initially tried the QStandardItem::setData() approach, however the QListView then displayed the data rather than the string that I'd invoked setText() with. They're two separate things in this application. Does one override the other?
    3) I see that you are a Nokia Certified Qt Developer. Could you advise me on what courses / books to study in order to achieve this accreditation too?

    Thanks

    Angus

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Extracting custom QStandardItem from QSortFilterProxyModel

    Quote Originally Posted by angusrose View Post
    1) How would I perform the cast? I'm new to Qt and rusty on C++!
    E.g. static_cast<>(). See http://www.cplusplus.com/doc/tutorial/typecasting/ for more information.

    2) I initially tried the QStandardItem::setData() approach, however the QListView then displayed the data rather than the string that I'd invoked setText() with. They're two separate things in this application. Does one override the other?
    Can't check it right now, but for the text you want to be displayed use the Qt::DisplayRole and for the other data use Qt::UserRole as an argument for setData(). For a more precise answer you have to provide some code which don't work for you.
    3) I see that you are a Nokia Certified Qt Developer. Could you advise me on what courses / books to study in order to achieve this accreditation too?
    On the website for the certification they recommend some books and also search the forum. We have some thread about that issue. My favorite is still the book from Johan Thelin: Foundations of Qt Development.

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.