Results 1 to 4 of 4

Thread: QTableView tab focus on cell widget

  1. #1
    Join Date
    May 2013
    Posts
    6
    Thanks
    2

    Default QTableView tab focus on cell widget

    Hello,

    I have setup a QTableView with my own delegate to create a cell widget. This widget contains two QDateTimeEdit elements. My problem if I tab through date/time values of first widget is that the focus leave the cell after last time value, close the editor and enter the next cell.

    How can I prevent this behavior and set focus to next QDateTimeEdit widget of the same cell?

  2. #2
    Join Date
    May 2013
    Posts
    6
    Thanks
    2

    Default Re: QTableView tab focus on cell widget

    Here the dual value editor that is instantiated in delegate.createEditor method with

    Qt Code:
    1. QWidget * QStyledItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index )
    2. {
    3. return new EvaDualValEditor(new QDateTimeEdit(parent), new QDateTimeEdit(parent), parent);
    4. }
    To copy to clipboard, switch view to plain text mode 

    ... and it looks like in table edit mode. After tabbing (cursor on blue marked) the editor is closed and the next cell is selected.
    dualValEditor.jpg


    Qt Code:
    1. // header
    2. class EvaDualValEditor : public QWidget
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit EvaDualValEditor(QWidget* editor1, QWidget* editor2, QWidget *parent = 0);
    8. ~EvaDualValEditor();
    9.  
    10. void setValues( QVariant& param1, QVariant& param2 );
    11.  
    12. protected:
    13.  
    14. private:
    15. QHBoxLayout *horizontalLayout_2;
    16. QLabel *label;
    17. QWidget *wgt1;
    18. QLabel *label_2;
    19. QWidget *wgt2;
    20.  
    21. void init();
    22. };
    23.  
    24. // implementation
    25.  
    26. EvaDualValEditor::EvaDualValEditor(QWidget* editor1, QWidget* editor2, QWidget* parent /*= 0*/)
    27. : QWidget(parent)
    28. , wgt1(editor1)
    29. , wgt2(editor2)
    30. {
    31. init();
    32. }
    33.  
    34. EvaDualValEditor::~EvaDualValEditor()
    35. {
    36. }
    37.  
    38. void EvaDualValEditor::init()
    39. {
    40.  
    41. setFocusPolicy (Qt::StrongFocus);
    42. setFocusProxy(wgt1);
    43. setTabOrder(wgt1, wgt2);setObjectName(QString::fromUtf8("EvaDualValEditor"));
    44.  
    45. horizontalLayout_2 = new QHBoxLayout(this);
    46. horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
    47. horizontalLayout_2->setSpacing(1);
    48. horizontalLayout_2->setContentsMargins(1, 1, 1, 1);
    49.  
    50. label = new QLabel(this);
    51. label->setObjectName(QString::fromUtf8("label"));
    52.  
    53. horizontalLayout_2->addWidget(label);
    54.  
    55. wgt1->setObjectName(QString::fromUtf8("widget"));
    56.  
    57. horizontalLayout_2->addWidget(wgt1);
    58.  
    59. label_2 = new QLabel(this);
    60. label_2->setObjectName(QString::fromUtf8("label_2"));
    61.  
    62. horizontalLayout_2->addWidget(label_2);
    63.  
    64. wgt2->setObjectName(QString::fromUtf8("widget_2"));
    65.  
    66. horizontalLayout_2->addWidget(wgt2);
    67.  
    68. }
    69.  
    70. void EvaDualValEditor::setValues( QVariant& param1, QVariant& param2 )
    71. {
    72. if (wgt1 && !param1.isNull())
    73. {
    74. qobject_cast<QDateTimeEdit*>(wgt1)->setDateTime(param1.toDateTime());
    75. }
    76. if (wgt2 && !param2.isNull())
    77. {
    78. qobject_cast<QDateTimeEdit*>(wgt2)->setDateTime(param2.toDateTime());
    79. }
    80. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableView tab focus on cell widget

    Use
    Qt Code:
    1. QTableView::setTabKeyNavigation(false);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    May 2013
    Posts
    6
    Thanks
    2

    Thumbs up Re: QTableView tab focus on cell widget

    Hi,

    here now a part of my solution.

    Delegate
    Qt Code:
    1. QWidget* TableDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. if (index.row()%2 && (index.column()==1 || index.column()==2))
    4. {
    5. EvaDualValEditor* dualValEditor = new EvaDualValEditor(new QDateTimeEdit(parent), new QDateTimeEdit(parent), parent);
    6. connect(dualValEditor, SIGNAL(commitData(QWidget*)), this, SIGNAL(commitData(QWidget*)));
    7. connect(dualValEditor, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), this, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)));
    8. return dualValEditor;
    9. }
    10. return QStyledItemDelegate::createEditor(parent, option, index);
    11. }
    To copy to clipboard, switch view to plain text mode 

    DualValEditor
    Qt Code:
    1. EvaDualValEditor::EvaDualValEditor( QWidget* editor1, QWidget* editor2, QWidget *parent)
    2. : QWidget(parent)
    3. , wgt1(editor1)
    4. , wgt2(editor2)
    5. {
    6. if (editor1 && editor2)
    7. {
    8. init();
    9.  
    10. setFocusPolicy(Qt::StrongFocus);
    11. setFocusProxy(wgt1);
    12. setTabOrder(wgt1, wgt2);
    13. wgt1->installEventFilter(this);
    14. wgt2->installEventFilter(this);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool EvaDualValEditor::focusNextPrevChild( bool next )
    2. {
    3. Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
    4.  
    5. if (next)
    6. {
    7. if (wgt1->hasFocus())
    8. {
    9. wgt2->setFocus(reason);
    10. return true;
    11. }
    12. else if(wgt2->hasFocus())
    13. {
    14. QWidget* tmp = focusProxy();
    15. setFocusProxy(wgt2);
    16. emit commitData(this);
    17. emit closeEditor(this, QAbstractItemDelegate::EditNextItem);
    18. setFocusProxy(tmp);
    19. return true;
    20. }
    21. }
    22. else if(!next)
    23. {
    24. if (wgt2->hasFocus())
    25. {
    26. wgt1->setFocus(reason);
    27. return true;
    28. }
    29. else if(wgt1->hasFocus())
    30. {
    31. emit commitData(this);
    32. emit closeEditor(this, QAbstractItemDelegate::EditPreviousItem);
    33. return true;
    34. }
    35. }
    36.  
    37. return QWidget::focusNextPrevChild(next);
    38. }
    To copy to clipboard, switch view to plain text mode 

    This works exactly as desired.

Similar Threads

  1. Inserting a Custom Widget inside a QTableView Cell
    By fruzzo in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2012, 21:38
  2. Replies: 2
    Last Post: 18th July 2011, 00:25
  3. cell selection in QTableView
    By user in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 01:01
  4. QTable, Removing cell focus
    By Kubil in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2007, 10:13
  5. Replies: 3
    Last Post: 8th September 2006, 18:54

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.