Results 1 to 6 of 6

Thread: [SOLVED] QTableWidget: column switch with tab key -- how?

  1. #1
    Join Date
    Apr 2007
    Location
    Toruń, POLAND
    Posts
    24
    Thanks
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default [SOLVED] QTableWidget: column switch with tab key -- how?

    Hello,

    I have persistent editor (QTextEdit) opened in each cell and now I would like to be able to just switch the column (I have 2 columns) when the user presses tab key.
    setCurrentCell(currentRow(),1-currentColumn());

    To do so, I reimplemented eventFilter in QTableWidget and I wait for tab key. There are two problems:
    1) I cannot turn off TabKeyNavigation in QTableWidget
    2) my code works and then it is ignored, like this: works, works, works, ignored, ignored, works, works, works, ignored, ignored, works...

    The result is totally bizarre -- you get 3 times column switch, then "normal" navigation twice, then column switch x3 and so on.

    I checked what event I get, and it appears I get key releases all the time, but not presses. Also, it looks like this (inside eventFilter):

    ( press, release, ) x 3, release, release, press, ...

    How can tab key be released twice in a row without pressing?

    Tab handling part of evenFilter:
    Qt Code:
    1. if (key_event->key()==Qt::Key_Tab ||
    2. key_event->key()==Qt::Key_Backtab)
    3. {
    4. if (_event->type()==QEvent::KeyPress)
    5. setCurrentCell(currentRow(),currentColumn()==1?0:1);
    6. _event->accept();
    7. return true; // event handled
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance for enlightening me :-)

    have a nice day, bye
    Last edited by macias; 12th August 2007 at 12:07. Reason: solved

  2. #2
    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: QTableWidget: column switch with tab key -- how?

    I suggest subclassing QTableWidget and reimplementing QWidget::focusNextPrevChild():
    Qt Code:
    1. bool MyTableWidget::focusNextPrevChild(bool next)
    2. {
    3. Q_UNUSED(next);
    4. setCurrentCell(currentRow(), 1 - currentColumn());
    5. return true;
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Apr 2007
    Location
    Toruń, POLAND
    Posts
    24
    Thanks
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget: column switch with tab key -- how?

    Thank you, strange, but this method is not even called. And it should (?) because it is virtual so it is ok to add my own implementation of it.

    In other words -- those changes are transparent.

    have a nice day, bye

  4. #4
    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: QTableWidget: column switch with tab key -- how?

    See the attached example.

    EDIT: Ahh, now I noticed the problem. QTableWidget::focusNextPrevChild() does not get called when in editing mode.
    Attached Files Attached Files
    J-P Nurmi

  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: QTableWidget: column switch with tab key -- how?

    Alright, I have attached an example which works in both, editing and non-editing modes. Reimplementing QAbstractItemView::closeEditor() does the trick in editing mode:
    Qt Code:
    1. void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint)
    2. {
    3. if (hint == QAbstractItemDelegate::EditNextItem && currentColumn() == 1)
    4. hint = QAbstractItemDelegate::EditPreviousItem;
    5. else if (hint == QAbstractItemDelegate::EditPreviousItem && currentColumn() == 0)
    6. hint = QAbstractItemDelegate::EditNextItem;
    7. QTableWidget::closeEditor(editor, hint);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    J-P Nurmi

  6. The following 2 users say thank you to jpn for this useful post:

    macias (12th August 2007), theRain (30th December 2014)

  7. #6
    Join Date
    Apr 2007
    Location
    Toruń, POLAND
    Posts
    24
    Thanks
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget: column switch with tab key -- how?

    Wow, this was something :-) I would never guess it -- quite not intuitive. Thank you.

Similar Threads

  1. QTableWidget stretch a column other than the last one
    By roleroz in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2015, 06:35
  2. QTableWidget first column always visible
    By topino in forum Qt Programming
    Replies: 13
    Last Post: 30th December 2014, 08:06
  3. changing column width of QTableWidget
    By juliarg in forum Newbie
    Replies: 4
    Last Post: 22nd March 2007, 15:51
  4. Replies: 6
    Last Post: 13th October 2006, 14:40
  5. QTableWidget column and row sizes
    By Arthur in forum Qt Programming
    Replies: 4
    Last Post: 27th January 2006, 11:03

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.