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,
{
if (qName == "place") {
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->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);
}
// 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);
}
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
Bookmarks