×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    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.

    Code:
    // from the SAXHandler that populates the QListView
    bool AllPlacesXMLHandler::startElement(const QString &namespaceURI,
                                           const QString &localName,
                                           const QString &qName,
                                           const QXmlAttributes &attributes)
    {
    
    
                if (qName == "place") {
                    QString name, county, description;
    
                    for( int i=0; i<attributes.count(); i++ ){
                        if( attributes.localName( i ) == "name" ){
                             name = attributes.value( i );
                       }else if( attributes.localName( i ) == "county" ){
                             county = attributes.value( i );
                       }else if( attributes.localName( i ) == "description" ){
                             description = attributes.value( i );
                       }
                    }
    
                    NTStandardListItem* item1 = new NTStandardListItem();
                    item1->setText(name + " " + county + " " + description);
                    item1->setPlaceNameID(name);
                    iStandardModel->appendRow(item1);
                 }
    
             return true;
    }
    
    
    //from the class displaying the QListView - I swap the QStandardItemModel for a QSortFilterProxyModel (proxyModel) in order to allow filtering of the list
    void AllPlacesWindow::populateListView()
    {
        //GetDisplayInfo *displayInfo = new GetDisplayInfo();
        AllPlacesXMLHandler handler;
    
        iStandardModel = handler.readFile();
        ui->allPlacesListView->adjustSize();
        this->proxyModel->setSourceModel(iStandardModel);
        ui->allPlacesListView->setModel(proxyModel);
        ui->allPlacesListView->setSelectionMode(QAbstractItemView::SingleSelection);
        ui->allPlacesListView->show();
    
    }
    
    //here is the connect
    
    connect(ui->allPlacesListView , SIGNAL(clicked(const QModelIndex &)) , this , SLOT(displayNTPlaceInfo(const QModelIndex &)));
    
    //here is the method called when a user clicks on an item in the list
    void AllPlacesWindow::displayNTPlaceInfo(QModelIndex index)
    {
        NTStandardListItem var = ui->allPlacesListView->model()->data(index , Qt::DisplayRole).value<NTStandardListItem>();
        qDebug() << "displayNTPlaceInfo :: " << var.getPlaceNameID() << " index :: " << index.data(Qt::DisplayRole);
    }
    It's the last line that's the issue - ar.getPlaceNameID() doesn't return anything.

    Any suggestions?

    Thanks in advance

    Angus
    Last edited by divanov; 2011-01-20 at 11:10. Reason: adding code tag

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Extracting custom QStandardItem from QSortFilterProxyModel

    Replace Qt::DisplayRole with Qt::UserRole
    Code:
    NTStandardListItem var = ui->allPlacesListView->model()->data(index, Qt::DisplayRole).value<NTStandardListItem>();

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Re: Extracting custom QStandardItem from QSortFilterProxyModel

    Thanks. I've tried that but with no luck I'm afraid.

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Extracting custom QStandardItem from QSortFilterProxyModel

    Take into account that you should use QAbstractItemModel::setData(index, id, Qt::UserRole) at some point.

Similar Threads

  1. QSortFilterProxyModel question
    By Olorin5800 in forum Qt
    Replies: 5
    Last Post: 2010-09-20, 12:45
  2. delete memory from QStandardItem
    By super_juanjo in forum Qt
    Replies: 1
    Last Post: 2010-08-30, 09:24
  3. Extracting from a Record
    By Noor Aldoja in forum Mobile Java General
    Replies: 1
    Last Post: 2009-05-16, 12:14
  4. Replies: 2
    Last Post: 2008-06-13, 19:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×