Results 1 to 14 of 14

Thread: QTableView, no selection rectangle

  1. #1
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView, no selection rectangle

    Hi,

    Is it possible to hide the selection rectangle for the table view items when the item is highlighted and selected ?

    I mean what I want is to select the cell and paint its background but I don't want to see the dotted selection rectangle around the cell.

    Sami

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    try this
    Qt Code:
    1. ...
    2. setSelectionMode(QAbstractItemView::NoSelection);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    or you can implement your own delegate and do not draw selection.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    Actually what I want is to select a row when I click on a cell, and I'm using setSelectionMode(QAbstractItemView::SingleSelectio n) and setSelectionBehavior(QAbstractItemView::SelectRows ).

    What I don't want is the dotted rectangle displayed when I click on a cell. It can be easily seen when selection mode is set to `NoSelection`. It does not selects the cell, but the active cell is shown with this rectangle.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    now it's clear. so, you need to implement your own delegate and don't draw dotten focus rect.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView, no selection rectangle

    Qt Code:
    1. view->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    alisami (24th December 2008)

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    Quote Originally Posted by jpn View Post
    Qt Code:
    1. view->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 
    what about keyboard navigation in this case? it will not work.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView, no selection rectangle

    What's the point of having focus if he wants to hide it?
    J-P Nurmi

  9. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    that's true. but I think he wants to have keyboard navigation I think.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #9
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    Quote Originally Posted by jpn View Post
    What's the point of having focus if he wants to hide it?
    that will do the trick but there is a minor bug ( I guess this is not a feature ), when focus is deactivated, when a cell is double clicked for editing, after the editing, the focus rectangle becomes active again.

    setting the item flags to un-editable mode fixes that problem.

    is there a way to set the table as read-only so that none of the items are editable?

  11. #10
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    Quote Originally Posted by spirit View Post
    that's true. but I think he wants to have keyboard navigation I think.
    Yeah actually I may need keyboard navigation. But I guess I can implement keyboard inputs to change the selected rows, without using the focus.

  12. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    do you think this is bet solution to implement navigation of treeView which is already works fine insted of creating custom delegate and write several lines of code? I don't thinks so.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #12
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    Quote Originally Posted by spirit View Post
    do you think this is bet solution to implement navigation of treeView which is already works fine insted of creating custom delegate and write several lines of code? I don't thinks so.
    can you describe which part I have to reimplement so that I can lose the focus rectangle?

    the main reason why I want to do such things is that, I want to implement an outlook-like message list, and I think the best way is to use QTableWidget. do I proceed in the right direction or do you have any other suggestion ?

  14. #13
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    example
    Qt Code:
    1. class MyDelegate: public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. virtual void drawFocus ( QPainter * /*painter*/, const QStyleOptionViewItem & /*option*/, const QRect & /*rect*/ ) const {}
    6. };
    7. ...
    8. QTableWidget *table = new QTableWidget(10, 10);
    9. table->setItemDelegate(new MyDelegate());
    10. for (int row = 0; row < 10; ++row) {
    11. for (int col = 0; col < 10; ++col) {
    12. table->setItem(row, col, item);
    13. }
    14. }
    15. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  15. The following 4 users say thank you to spirit for this useful post:

    alisami (25th December 2008), Brecht (7th March 2013), giantdragon (21st June 2011), matthieu (30th May 2012)

  16. #14
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView, no selection rectangle

    Quote Originally Posted by spirit View Post
    example
    Qt Code:
    1. class MyDelegate: public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. virtual void drawFocus ( QPainter * /*painter*/, const QStyleOptionViewItem & /*option*/, const QRect & /*rect*/ ) const {}
    6. };
    7. ...
    8. QTableWidget *table = new QTableWidget(10, 10);
    9. table->setItemDelegate(new MyDelegate());
    10. for (int row = 0; row < 10; ++row) {
    11. for (int col = 0; col < 10; ++col) {
    12. table->setItem(row, col, item);
    13. }
    14. }
    15. ...
    To copy to clipboard, switch view to plain text mode 
    Thanks a lot Spirit, for not giving up on me
    I finally got what I need.

    Sami

Similar Threads

  1. QSqlTableModel and QTableView selection problems
    By innerhippy in forum Qt Programming
    Replies: 5
    Last Post: 19th December 2008, 05:48
  2. QTableView selection problem
    By aiprober in forum Qt Programming
    Replies: 12
    Last Post: 9th June 2008, 11:55
  3. QTableView Row Selection
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 12th December 2007, 21:27
  4. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31
  5. How to paint a selection rectangle on a pixmap?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2006, 19:52

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.