Results 1 to 6 of 6

Thread: Process Enter Key on QTableWidget

  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Process Enter Key on QTableWidget

    Hi guys, it's been so long, unfortunately I'm coming and going from Qt due to work issues.
    I'm trying to insert an Enter/Return Key within a QTableWidgetItem.
    When I'm editing a value in a QTableWidget and press Enter, it commits the data but I need a multiline control.
    I already tried to set a QTextEdit, a QTextEdit derived class as cell widget, overloading keyPressEvent, but I had no success.

    Is it possible to do?

    Cheers and thanx in advance.
    Kandalf
    There's no place like ~

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Process Enter Key on QTableWidget

    If you have a custom QTableWidget then install an event filter on it, catch enter/return on it and if you have an editor opened on a cell just forward the key press event to the editor.

    BTW: how will you commit the editor data if the editor will get the returns?

    Regards

  3. #3
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Process Enter Key on QTableWidget

    If you have a custom QTableWidget then install an event filter on it, catch enter/return on it and if you have an editor opened on a cell just forward the key press event to the editor.
    Ok, how can I know if an editor is opened on a cell?

    BTW: how will you commit the editor data if the editor will get the returns?
    I'm planning to catch a Ctrl+Enter and close the editor.
    Kandalf
    There's no place like ~

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Process Enter Key on QTableWidget

    Set a flag in your class when an editor is opened ( you'll have to detect that ).
    You might want to explore:
    Qt Code:
    1. QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index ) [virtual][COLOR=#000000][FONT=Courier New,courier][/FONT][/COLOR]
    To copy to clipboard, switch view to plain text mode 
    maybe you can do something with it ( unfortunately I have never used it ).

    I'm planning to catch a Ctrl+Enter and close the editor
    OK then, sounds good.

    Regards

  5. #5
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Process Enter Key on QTableWidget

    Ok, Thanx a lot for a really quickly and accurate response. I'll see what I can do and I will post the solution (if I find some) for someone who may need it.

    Cheers and thanx again!!!
    Kandalf
    There's no place like ~

  6. #6
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Process Enter Key on QTableWidget

    Hi guys, unfortunately, I still have problems, even with the event filter.
    It seems to not work fine for me, because even when the eventFilter method return true the event is forwarded to the QTableWidget derived object.

    This is my code of the event filter object:
    Qt Code:
    1. #include <QKeyEvent>
    2. #include "TextProcessor.h"
    3.  
    4. bool TextProcessor::eventFilter(QObject *obj, QEvent *evt)
    5. {
    6. if (evt->type() == QEvent::KeyPress)
    7. {
    8. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(evt);
    9. if (keyEvent->key() == Qt::Key_Return)
    10. {
    11. if (keyEvent->modifiers() == Qt::ControlModifier)
    12. {
    13. qWarning("Ctrl");
    14. emit escapeCharPressed();
    15. return false;
    16. }
    17. else
    18. {
    19. qWarning("Enter");
    20. // emit lineFeed();
    21. return true;
    22. }
    23. }
    24. else
    25. {
    26. return false;
    27. }
    28. }
    29. else
    30. {
    31. return QObject::eventFilter(obj, evt);
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    The case is that when I press Enter Key I got the line in debugging, but the editor is still closing, so I guess the event is still forwarding to the monitored object.
    What am I doing wrong?

    Thanx a lot in advance.
    Kandalf
    There's no place like ~

Similar Threads

  1. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  2. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 18:46
  3. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  4. Replies: 6
    Last Post: 5th March 2006, 21:05
  5. How to activate another process?
    By gtthang in forum Qt Programming
    Replies: 6
    Last Post: 3rd February 2006, 07:53

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.