Results 1 to 9 of 9

Thread: QTableWidget KeyPress filtering

  1. #1
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18

    Default QTableWidget KeyPress filtering

    Hi,

    I have a QTableWidget with one column that can have 0 to 255 entered (all other characters should be ignored). All other columns are read only.

    I setup the table to pass events to the main window eventFilter, unfortunately keyPress doesn't make it there keyRelease does.

    So I guess I need to catch them from QTableWidgetItem. I tried the following, this was in the program body, maybe I should try similar in constructor? Also I think the item in the connect function probably needs to be *item or &item or something ?

    Qt Code:
    1.  
    2. if((item = new QTableWidgetItem("0")) != NULL) {
    3.  
    4. table->setItem(row, col, item);
    5. connect(item, SIGNAL(keyPressEvent(QKeyEvent *)), this, SLOT(onKeyPressEvent(QKeyEvent *)));
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTableWidget KeyPress filtering

    Why are you using event filters? Use an item delegate.
    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. #3
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18

    Default Re: QTableWidget KeyPress filtering

    Thanks, Could you give a little more information or point me to an example?

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

    Default Re: QTableWidget KeyPress filtering

    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.


  5. #5
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18

    Default Re: QTableWidget KeyPress filtering

    Thanks I got the basic key filtering working with the following code.

    Qt Code:
    1. bool StringDelegate::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if(event->type() == QEvent::KeyPress) {
    4.  
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    6.  
    7. return (keyEvent->key() < '0' || keyEvent->key() > '9');
    8. }
    9. // standard event processing
    10. return QObject::eventFilter(obj, event);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now I am trying to enforce the range when the user exits editing: I tried to add the following to the eventFilter, it compiles but doesn't work.

    Qt Code:
    1. } else if(event->type() == QEvent::Leave) {
    2.  
    3. QLineEdit *lineEdit = static_cast<QLineEdit*>(obj);
    4. QString text = lineEdit->text();
    5.  
    6. if(text.toInt() > 255)
    7. text = "255";
    8. else if(text.toInt() < 1)
    9. text = "0";
    10.  
    11. lineEdit->setText(text);
    12. }
    To copy to clipboard, switch view to plain text mode 

    The following code checks range when editing starts, but i'm just interested in when editing ends:

    Qt Code:
    1. void StringDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. QString value = index.model()->data(index, Qt::EditRole).toString();
    4. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
    5.  
    6. if(value.toInt() > 255)
    7. value = "255";
    8. else if(value.toInt() < 1)
    9. value = "0";
    10.  
    11. lineEdit->setText(value);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas? Thanks in advance.

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

    Default Re: QTableWidget KeyPress filtering

    Why did you fix yourself on using an event filter????? All you need to do is to override QAbstractItemDelegate::createEditor().
    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.


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

    grantbj74 (18th October 2011)

  8. #7
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18

    Default Re: QTableWidget KeyPress filtering

    Thanks,

    So the following code sets the range (0 to 255), using a validator:

    Qt Code:
    1. QWidget* StringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &,const QModelIndex &) const
    2. {
    3. QLineEdit *editor = new QLineEdit(parent);
    4. QIntValidator *val = new QIntValidator(editor);
    5.  
    6. val->setRange(0, 255);
    7. editor->setValidator(val);
    8.  
    9. return editor;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Is there a way to say chop off leading zeros once editing is finished using delegates?

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

    Default Re: QTableWidget KeyPress filtering

    How about returning a spinbox from createEditor() with a range set to 0-255?
    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.


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

    grantbj74 (19th October 2011)

  11. #9
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18

    Default Re: QTableWidget KeyPress filtering

    Yes that worked, Thanks

    Qt Code:
    1. QWidget* StringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &,const QModelIndex &) const
    2. {
    3. QSpinBox *spin = new QSpinBox(parent);
    4.  
    5. spin->setRange(0, 255);
    6. return spin;
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. KeyPress and KeyRelease Events
    By sattu in forum Qt Programming
    Replies: 8
    Last Post: 30th September 2011, 19:23
  2. QListWidget keypress capture ?
    By tonnot in forum Qt Programming
    Replies: 5
    Last Post: 29th September 2011, 21:07
  3. Handle KeyRelease or KeyPress on any row in QTableView
    By AbuYusuf in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2010, 21:04
  4. fn+f1 keypress detect
    By oguzy in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 17:21
  5. keypress event
    By vishesh in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2007, 15:12

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
  •  
Qt is a trademark of The Qt Company.