Results 1 to 6 of 6

Thread: Navigate table cells with down/up arrows while editing a double cell

  1. #1
    Join Date
    Jun 2014
    Posts
    30
    Thanks
    4
    Qt products
    Qt5

    Default Navigate table cells with down/up arrows while editing a double cell

    I have a QTableView with a column of doubles. Since my custom implementations of data and setData operate on doubles it appears Qt automatically provides the QDoubleSpinBox. I would like to be able to navigate through the table cells with the keyboard arrows while editing the double. What happens is that the delegate seems to steal the key down event and uses it to control the spin box rather than navigating to another cell.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Navigate table cells with down/up arrows while editing a double cell

    Provide your own editor widget that will ignore the events you don't want intercepted by the editor. They should propagate to the view and be handled there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    jmalicke (26th October 2014)

  4. #3
    Join Date
    Jun 2014
    Posts
    30
    Thanks
    4
    Qt products
    Qt5

    Default Re: Navigate table cells with down/up arrows while editing a double cell

    I have created my own delegate that renders a QSpinBox. Now how do I do the part that ignores the events?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,938
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Navigate table cells with down/up arrows while editing a double cell

    If your delegate is your own widget derived from QSpinBox, then you would provide event handlers for key press and key release events. In those, you would ignore the up and down arrow keys, but pass everything else to the base class (QSpinBox) for handling.

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

    jmalicke (26th October 2014)

  7. #5
    Join Date
    Jun 2014
    Posts
    30
    Thanks
    4
    Qt products
    Qt5

    Default Re: Navigate table cells with down/up arrows while editing a double cell

    Hi folks,

    Thank you so much for the help!

    I have implemented a custom delegate that returns a custom editor. The NavigableDoubleSpinBoxDelegate returns an editor of the type NavigableDoubleSpinBox.

    Qt Code:
    1. class NavigableDoubleSpinBoxDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. NavigableDoubleSpinBoxDelegate(QObject * parent = nullptr) : QStyledItemDelegate(parent) {}
    7. virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    8. virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
    9. virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    10. virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,const QModelIndex &index) const;
    11. };
    12.  
    13. class NavigableDoubleSpinBox : public QDoubleSpinBox
    14. {
    15. public:
    16. NavigableDoubleSpinBox(QWidget * parent = nullptr) : QDoubleSpinBox(parent) {}
    17.  
    18. protected:
    19. virtual void keyPressEvent(QKeyEvent * event) override;
    20. };
    To copy to clipboard, switch view to plain text mode 

    Here is where the custom editor is provided:

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

    The custom editor is set to ignore key ups, downs, lefts and rights and pass every other key to it's parent event handler.

    Qt Code:
    1. void NavigableDoubleSpinBox::keyPressEvent(QKeyEvent * event)
    2. {
    3. Logger::getLogger().log(Logger::LOG_DEBUG, "Received keyPressEvent");
    4.  
    5. if (event->key() == Qt::Key_Up
    6. || event->key() == Qt::Key_Down
    7. || event->key() == Qt::Key_Left
    8. || event->key() == Qt::Key_Right)
    9. {
    10. return; // Ignore event;
    11. }
    12.  
    13. QDoubleSpinBox::keyPressEvent(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

    And finally, of course I set the delegate to the column:

    Qt Code:
    1. NavigableDoubleSpinBoxDelegate * navigableDelegate = new NavigableDoubleSpinBoxDelegate(this);
    2. ui->pairsTable->setItemDelegateForColumn(MomentValuePairTableModel::COL_VALUE, navigableDelegate);
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, what this code accomplishes is that it removes the arrow key functionality for controlling the spinbox. I still don't get the behavior I want -- that the arrows control the position of the selected cell in the QTableView. At this point, is the event supposed to propagate to the QTableView? Am I missing a step somewhere?
    Last edited by jmalicke; 26th October 2014 at 18:35.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Navigate table cells with down/up arrows while editing a double cell

    Quote Originally Posted by jmalicke View Post
    Qt Code:
    1. if (event->key() == Qt::Key_Up
    2. || event->key() == Qt::Key_Down
    3. || event->key() == Qt::Key_Left
    4. || event->key() == Qt::Key_Right)
    5. {
    6. return; // Ignore event;
    7. }
    To copy to clipboard, switch view to plain text mode 
    This does not ignore the event. This does:

    Qt Code:
    1. if (event->key() == Qt::Key_Up
    2. || event->key() == Qt::Key_Down
    3. || event->key() == Qt::Key_Left
    4. || event->key() == Qt::Key_Right)
    5. {
    6. event->ignore(); // Ignore event;
    7. return;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    jmalicke (26th October 2014)

Similar Threads

  1. Enable editing by double clicking QGraphicsTextItem
    By Seishin in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2012, 15:07
  2. QTableWidget how could I detect is a cell is editing??
    By webquinty in forum Qt Programming
    Replies: 1
    Last Post: 30th November 2012, 15:27
  3. Replies: 6
    Last Post: 10th June 2011, 20:44
  4. Replies: 2
    Last Post: 28th October 2010, 09:26
  5. Replies: 6
    Last Post: 16th February 2010, 18:21

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.