c++ - Limit Qlistview to 1 selected item at most -


i try compel program select 1st item(and it) in qlistview if user's selection more 1 , includes 1st item. selectionmode flag multiselection, means user can select several items in listview.

below code:

void realplay::when_allchannel_selected     (const qitemselection &selected,      const qitemselection &deselected) {      // if selected same deseleted     //     return, not necessary     qmodelindexlist selectedlist = selected.indexes();     for(int =0 ; < selectedlist.length();++i)     {         if(!deselected.contains(selectedlist.at(i)))         {             break;         }         return;     }  // channelmodel                qstandarditemmodel // selectedchannels            qitemselectionmodel // ui->listview_channel        qlistview      //this first item want select     qmodelindex firstiteminchannelview =         channelmodel->indexfromitem(channelmodel->item(0));      if(selectedchannels->isselected(firstiteminchannelview)         && (selectedchannels->selectedindexes().length()>1))     {         selectedchannels->reset();         selectedchannels->select(firstiteminchannelview,\              qitemselectionmodel::select);         //return;     }      //.. } 

in constructor:

connect (selectedchannels,   signal(selectionchanged(const qitemselection &,const qitemselection &)),   this,   slot(when_allchannel_selected(const qitemselection &,const qitemselection &))); 

but code not work. deselects last selection user made before user selects 1st item instead of deselecting other items except 1st item. how can this?

additional questions:

  1. is index in qitemselection , qitemselectionmodel same index in qstandarditemmodel?

like if index of 1st item in qstandarditemmodel 0, if it's been selected, no matter sequence is, index still 0 in qitemselection , qitemselectionmodel. (it seems online material implies same..)

  1. it seems reset() method works. why can still see multiple selections in listview?

it turns out should use select() method of qitemselectionmodel rather reset() method. below code works.

if(selectedchannels->isselected(firstiteminchannelview) && (selectedchannels->selectedindexes().length()>1)) {     //selectedchannels->reset();     qmodelindex top = channelmodel->index(1,0);     qmodelindex bottom = channelmodel->index(channelmodel->rowcount()-1,0);     qitemselection selection(top, bottom);     selectedchannels->select(selection,qitemselectionmodel::deselect);     selectedchannels->select(firstiteminchannelview,qitemselectionmodel::select);     //return; } 

Comments