PDA

View Full Version : Extracting custom QStandardItem from QSortFilterProxyModel



angusrose
20th January 2011, 09:59
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.



// 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::SingleSelectio n);
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

Lykurg
20th January 2011, 10:22
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.

angusrose
21st January 2011, 12:10
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

Lykurg
21st January 2011, 17:18
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.