Results 1 to 6 of 6

Thread: QTableWidget: How to "select none" with SingleSelection mode?

  1. #1
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget: How to "select none" with SingleSelection mode?

    If QTableWidget is set to SingleSelection, then I can NOT unselect a row by clicking outside of any row/column. Which means, as long as I selected a row, there's always a row selected, no matter where I click;

    The "ExtentedSelection" allows to "select none" by clicking a blank area in the TableWidget, but it also allows multi-selection by holding the CTRL/SHIFT key, which is not what I want

    Is there a way to make the TableWidget single selection which still allows "deselect a row"?

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget: How to "select none" with SingleSelection mode?

    You can set the index to invalid like:

    ui->tableWidget->setCurrentCell(-1,-1);

  3. #3
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget: How to "select none" with SingleSelection mode?

    Quote Originally Posted by qlands View Post
    You can set the index to invalid like:

    ui->tableWidget->setCurrentCell(-1,-1);
    How do you know when to call this method? Seems clicking on a blank area doesn't trigger any signals...

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget: How to "select none" with SingleSelection mode?

    You said:

    If QTableWidget is set to SingleSelection, then I can NOT unselect a row by clicking outside of any row/column. Which means, as long as I selected a row, there's always a row selected, no matter where I click;
    So, if you want to unselect the current selected item you set the index to invalid (-1,-1).

    How do you know when to call this method?
    When I want to unselect any selected item....Like from a button that says "Unselect all"..

  5. #5
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget: How to "select none" with SingleSelection mode?

    Quote Originally Posted by qlands View Post
    When I want to unselect any selected item....Like from a button that says "Unselect all"..
    ummm...that's not an elegant UX. Actually in other frameworks (like .Net), users doesn't have to click a button to clear the selection, they just clicks a blank area of the table widget, the selection gets cleared automatically.

  6. #6
    Join Date
    Jan 2012
    Location
    Spain
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget: How to "select none" with SingleSelection mode?

    I was looking for that and have found a non complete a non elegant solution by adding to my QTreeView subclass widget this method:

    void MyWidgetClass::mousePressEvent(QMouseEvent *event)
    {
    QItemSelectionModel *seleccion = selectionModel();
    seleccion->clear();

    // MyWidgetClass inherits from QTreeView, replace QTreeView
    QTreeView::mousePressEvent(event);
    }


    I think this works for single selection because after clearing the selection, the propagation to QTreeView::mousePressEvent makes a new selection when you click on a cell. I have not tried with multiple selection, but I think in that case it will not work.

    Also there is another problem, my widget is q QTreeView, and when I collapse/expand a branch I also loose the selection (since QTreeView::mousePressEvent does not make a new selection in that cases).

    Has anyone the right solution?


    Added after 29 minutes:


    I have found a more accurate (and elegant ) solution:

    void mousePressEvent(QMouseEvent *event)
    {
    QModelIndex item = indexAt(event->pos());
    QTreeView::mousePressEvent(event);
    if (!item.isValid())
    clearSelection();
    }


    Now it keeps selection when expanding/collapsing branches, but unselects when clicking out of the items.
    Last edited by uatek; 18th January 2012 at 08:25.

Similar Threads

  1. Postgres, "driver not load" in debug mode
    By ithanoss in forum Newbie
    Replies: 5
    Last Post: 6th April 2011, 21:34
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Replies: 1
    Last Post: 4th February 2009, 01:53
  4. Replies: 1
    Last Post: 8th January 2009, 13:36
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

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.