Results 1 to 2 of 2

Thread: the question about define my own selection behavior

  1. #1
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default the question about define my own selection behavior

    Hi ,all

    Recently, I use tableview to make an alarm window which ,obviously, show the alarms in in the scada system.

    The requirement is: If the operator is permitted by his privilege to ack the alarm in the alarm window,he can select the alarm, if not, he cannot select.

    I inherit QTableView and rewrite the function
    Qt Code:
    1. QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QModelIndex &index, const QEvent *event) const
    To copy to clipboard, switch view to plain text mode 
    the code is like this:
    Qt Code:
    1. QItemSelectionModel::SelectionFlags CAckTableView::selectionCommand ( const QModelIndex & index, const QEvent * event) const
    2. {
    3. if (the operator does not have privilege){
    4. return QItemSelectionModel::NoUpdate | QItemSelectionModel::current;
    5. }
    6.  
    7. return QTableView::selectionCommand(index,event);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    Then I found the selcet event QEvent::MouseButtonPress act
    properly,but QEvent::MouseMove is very strange.

    The selection behavior of QEvent::MouseMove is depend on the last item the mouse pointer on. That is hard to describe clearly. Finally I probably find the problem in the qt source code. In
    Qt Code:
    1. void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
    To copy to clipboard, switch view to plain text mode 
    ,
    call
    Qt Code:
    1. setSelection(selectionRect, command);
    To copy to clipboard, switch view to plain text mode 
    ,

    the parameter is
    Qt Code:
    1. QPoint topLeft;
    2. if (d->selectionMode != SingleSelection)
    3. topLeft = d->pressedPosition - d->offset();
    4. else
    5. topLeft = bottomRight;
    6. QPoint bottomRight = event->pos();
    7. QRect selectionRect = QRect(topLeft, bottomRight);
    8.  
    9. QModelIndex index = indexAt(bottomRight);
    10. QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
    To copy to clipboard, switch view to plain text mode 

    In coclusion the problem is: the selection of MouseMove event is depend on whether the operator has the privilge on the last item the mouse pointed. The privilege cannot work!!

    I think the problem is in the
    Qt Code:
    1. void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
    To copy to clipboard, switch view to plain text mode 
    .
    Did anyone meet such problem? Please give me a clue! Thank you!
    Jerry

  2. #2
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: the question about define my own selection behavior

    You don't need to modify the selection model. You can tell the view which items can be selected by using the Qt::ItemIsSelectable flag in your data model.

    Inherit your model and overwrite QAbstractItemModel::flags:

    Qt Code:
    1. Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
    2. {
    3. if (/* user is allowd to select index.row() */)
    4. return QAbstractTableModel::flags(index) | Qt::ItemIsSelectable;
    5. else
    6. return QAbstractTableModel::flags(index) & ~Qt::ItemIsSelectable;
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to shentian for this useful post:

    calmspeaker (22nd June 2009)

Similar Threads

  1. Pre app.exec() behavior question
    By sgmurphy19 in forum Qt Programming
    Replies: 3
    Last Post: 15th September 2008, 22:02
  2. odd double widgets
    By jhearon in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2008, 18:18
  3. QTreeView: selection behavior upon selected item removal
    By Pieter from Belgium in forum Qt Programming
    Replies: 6
    Last Post: 11th July 2007, 16:00
  4. QListWidget selection behavior as in Windows XP
    By Levon Nikoghosyan in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 13:11
  5. QListWidget selection behavior
    By Arthur in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2006, 14:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.