PDA

View Full Version : how to use listView enevt



tsuibin
15th October 2009, 07:01
i use a listView
i select one line, is there have any event ?
if selection changed , is it can emit any event ?
how to use it ?

i find i function


void QAbstractItemView::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected ) [virtual protected slot]

This slot is called when the selection is changed. The previous selection (which may be empty), is specified by deselected, and the new selection by selected.

See also setSelection().

but, it's don't work..

my code


void MusicSettingsWidget::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )
{
qDebug()<<"selection changed !";
}

spirit
15th October 2009, 08:33
what do you want to achieve?

faldzip
15th October 2009, 08:33
you can use QItemSelectionModel which you can get pointer to with


QItemSelectionModel * QAbstractItemView::selectionModel () const


Edit:
Ok I checked and for me that slot:


void QAbstractItemView::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected ) [virtual protected slot]
in my view inherited from QListView is working fine. Is your MusicSettingsWidget derived from any view class (QAbstractItemView, QListView etc)?

tsuibin
15th October 2009, 10:47
I have solved this problem, qt has a clicked signal function that emitted when a mouse button is clicked. The item the mouse was clicked on is specified by index. The signal is only emitted when the index is valid.


connect(ui->listView_Group,SIGNAL(clicked(const QModelIndex &)),this,SLOT(selectionChanged(const QModelIndex &)));


void MusicSettingsWidget::selectionChanged(const QModelIndex & index)
{
if ( index.data().toString() != "all music" ){
musicSettings4a6ConfigFile->beginGroup("GroupList");
QStringList musicList;
musicList << musicSettings4a6ConfigFile->value(index.data().toString()).toString().split(";",QString::SkipEmptyParts);
musicSettings4a6ConfigFile->endGroup();
mp3ListModel->setStringList(musicList);
ui->listView->setModel(mp3ListModel);
ui->listView->selectAll();
}else{
mp3ListInit();
}
}

tsuibin
2nd November 2009, 03:45
connect(selectionGroupModel,\
SIGNAL(currentRowChanged( const QModelIndex & , const QModelIndex & )),\
this,\
SLOT(selectionChanged( const QModelIndex & , const QModelIndex & )));




void MusicSettingsWidget::selectionChanged( const QModelIndex & current, const QModelIndex & previous )
{
qDebug()<<"selectionChanged";

// qDebug()<<"select change!"<<index.data().toString();
if ( current.data().toString() != "all music" ){
musicSettings4a6ConfigFile->beginGroup("GroupList");
QStringList musicList;
qDebug()<<"current.data().toString()"<<current.data().toString();
QString musicNames;
musicNames = musicSettings4a6ConfigFile->value( current.data().toString() ).toString();
qDebug()<<"musicNames"<<musicNames;
musicList = musicNames.split(";",QString::SkipEmptyParts);
musicList.removeDuplicates();
musicSettings4a6ConfigFile->endGroup();
mp3ListModel->setStringList(musicList);
ui->listView->setModel(mp3ListModel);
ui->listView->selectAll();
}else{
mp3ListInit();
ui->listView->selectAll();
}
}