Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView: Select One or None - close, but no cigar.
Forum Updated to NodeBB v4.3 + New Features

QTreeView: Select One or None - close, but no cigar.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LiamMaru
    wrote on last edited by
    #1

    So, I'm keen to make a QTreeView select a single index to pick an item, or select nothing by clicking empty space. I found this very helpful hint:

    "http://stackoverflow.com/questions/2761284/is-it-possible-to-deselect-in-a-qtreeview-by-clicking-off-an-item
    ":http://stackoverflow.com/questions/2761284/is-it-possible-to-deselect-in-a-qtreeview-by-clicking-off-an-item

    Which got me pretty far. My equivalent method in C++ is:

    @
    void DeselectableTreeView::mousePressEvent(QMouseEvent* event)
    {
    this->clearSelection();
    QTreeView::mousePressEvent(event);

    if(!this->selectedIndexes().count())
    {
    emit selection_cleared();
    }
    }
    @

    However, adding the if(!this->selectedIndexes().count()){...} line causes windows to trigger a breakpoint:

    HEAP[LMGM.exe]: Invalid address specified to RtlValidateHeap( 00210000, 02798DE8 )

    Now, keep in mind, this signal isn't connected to anything. the code is simply there. If I remove the code, the application runs as expected. Any idea's as to how to solve this, what I've done wrong, or a more desirable way to implement the desired behavior?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      LiamMaru
      wrote on last edited by
      #2

      A slight addendum: I need to know when 'nothing' is selected, hence the signal. As a work around, I'm picking up on the escape key which is nice (and will stay) but I'd really like the ability to de-select by clicking empty space.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        I would change the logic into something like

        @
        QModelIndex index = indexAt(event->pos());
        if (index.isValid()) {
        selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
        } else {
        clearSelection();
        }
        @

        By the way, you can connect a slot the QItemSelecitonModel::selectionChanged signal (the QISM on the view is available through selectionModel()).

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved