[Qt-interest] QListView selection model

phil prentice philp.cheer at talktalk.net
Tue Apr 21 13:41:36 CEST 2009


Hi everyone again

I asked a question yesterday regarding selections inside a derived from 
QListView object.  I find QT absolutely brilliant..however sometimes I am not 
confident that the way that I am achieving something is the correct way.  In 
this case what I have appears to work OK up to now, except I think I still 
need to intergrate it into the QItemSelectionModel.  I am hoping that the 
virtual function selectionCommand() will determine if an item is added to the 
selection model??

Anyway the following code enables me to detect if the mouse is selected over 
an item in a list. But it only detects it if the mouse is selected in a 
certain y-range of coordinates in the item.  To extend the current selected 
items I have to use  keyPressEvent & keyReleaseEvent to detect ctl/alt 
modifiers.  Is what I have done look reasonable or is there a much better 
way??  

Thanks for your help

Phil

QItemSelectionModel::SelectionFlags BottomFacilityListView::selectionCommand
                ( const QModelIndex & index, const QEvent *event) const
{
  /*
    If item is present under mouse & we have a mouse button press then check
    if mouse is positioned inside facility arrow. Also check if ctrl+Alt is
    pressed for adding selections to the current selection list.
  */
  if ((index != QModelIndex()) && (event->type() == QEvent::MouseButtonPress))
  {
    const QMouseEvent *mouseEvent = static_cast<const QMouseEvent*>(event);
    printf("x=%d,y=%d\n", mouseEvent->x(), mouseEvent->y());
    printf("Key=%d\n",m_addSelection);
    if((mouseEvent->y() > 20) && (mouseEvent->y() < 40))
    {
      if(!m_addSelection)
      {
        /* Clear all selections. */
      }
      return QItemSelectionModel::ToggleCurrent;
				 /* Add to currrent selection list. */
    }
  }
  else
  {
    return QItemSelectionModel::NoUpdate;
  }
}

void BottomFacilityListView::keyPressEvent( QKeyEvent *event )
{
  m_addSelection = (event->modifiers() ==
                        (Qt::ControlModifier | Qt::AltModifier))?true:false;
}
void BottomFacilityListView::keyReleaseEvent( QKeyEvent *event )
{
  m_addSelection = (event->modifiers() ==
                        (Qt::ControlModifier | Qt::AltModifier))?true:false;
}



On Monday 20 April 2009 20:28, phil prentice wrote:
> Hi
>   I have derived a class from QListView.  I control the output of the view
> using a delegate.  Each item consists of various icon data, text and an
> arrow.  I would like to select one or more of these items using a
> QItemSelectionModel.  The first problem that I have is that I only want the
> selection to be considered if the mouse lies inside of the arrow, so I need
> to check the mouse event for its location(when in the QLIstView widget).
>
> Should I consider using....
> QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand
> ( const QModelIndex & index, const QEvent * event = 0 ) const  [virtual
> protected]
> ????
>
> If so then I have another question and that is for multiple selections
> (keyboard = ctr+alt) would I then check something like keypressevent inside
> of selectionCommand()?
>
> Or am I totaly going up the wrong garden path???
>
> Thanks for your help
>
> Phil
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest



More information about the Qt-interest-old mailing list