Results 1 to 2 of 2

Thread: CheckBox and selection in QTableView

  1. #1
    Join Date
    Sep 2006
    Qt products

    Question CheckBox and selection in QTableView


    I have a QTableView with the selection set to:
    m_table->setSelectionBehavior(QAbstractItemView::SelectRow s);
    m_table->setSelectionMode(QAbstractItemView::ExtendedSelec tion);

    the associated model contains elements with the Qt::CheckStateRole role and flags
    set to:
    flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable;

    When the user clicks on a checkbox it all nicely changes state and all. However is also changes the selection. ie if the user checks on a box of an item in a non selected row, that row becomes selected.

    I tried overridding the EditorEvent in a QItemDelegate() but the selection change happens before it reaches that code.

    Any idea how I can prevent the selection from being changed when the user click in the check box (but work as before if clicked outside) ?


  2. #2
    Join Date
    Feb 2006
    Oslo, Norway
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: CheckBox and selection in QTableView

    QAbstractItemView updates the selection in mouse event handlers. You will have to override at least mousePressEvent() and prevent QAbstractItemView from handling the event if mouse was pressed over the checkbox. You will most propably have to add some checks in mouseReleaseEvent() too, otherwise the selection behaviour might get broken.

    Here's the code for detecting if a mouse event occurs over item's checkbox:
    Qt Code:
    1. QModelIndex idx = indexAt(event->pos());
    2. if (idx.isValid())
    3. {
    4. opt.QStyleOption::operator=(viewOptions());
    5. opt.rect = visualRect(idx);
    6. QRect rect = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
    7. if (rect.contains(event->pos()))
    8. {
    9. // pressed/released over the checkbox...
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi


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.