Results 1 to 4 of 4

Thread: Use of QAbstractItemDelegate::closeEditor

  1. #1
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Use of QAbstractItemDelegate::closeEditor

    I'm trying to user input perform validation in a QTableView/QAbstractTableModelstructure. I want to keep the cursor in a table cell if the entry is invalid. The only thing I've found that seems to work is
    Qt Code:
    1. void DoubleEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    2. {
    3. QString data = static_cast<QLineEdit*>(editor)->text();
    4. int pos;
    5. QValidator::State state = validator_->validate(data, pos);
    6. if (validator_->validate(data, pos) == QValidator::Acceptable)
    7. {
    8. model->setData(index, data);
    9. emit const_cast<DoubleEditDelegate*>(this)->clearMessage();
    10. }
    11. else
    12. {
    13. emit const_cast<DoubleEditDelegate*>(this)->errorMessage("Double required at line " + QString::number(index.row()+1));
    14. emit const_cast<DoubleEditDelegate*>(this)->closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    This doesn't seem right - having to const_cast the "this" pointer. Is this the correct way to use the closeEditor signal? If not, how do I do this?

    Thanks for any help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of QAbstractItemDelegate::closeEditor

    Maybe you should reimplement QItemDelegate::createEditor() instead and assign the validator to the editor there?

  3. #3
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use of QAbstractItemDelegate::closeEditor

    Quote Originally Posted by jacek View Post
    Maybe you should reimplement QItemDelegate::createEditor() instead and assign the validator to the editor there?
    The problem with that is that it still allows you to TAB to the next cell. It just doesn't store the data in the model. It doesn't provide any cursor control.

  4. #4
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Use of QAbstractItemDelegate::closeEditor

    Quote Originally Posted by jml View Post
    The problem with that is that it still allows you to TAB to the next cell. It just doesn't store the data in the model. It doesn't provide any cursor control.
    I finally solved the problem. QItemDelegate re-implements the virtual function eventFilter. So I just created a new class derived from QItemDelegate.
    In eventFilter, if it's not a TAB key:
    Qt Code:
    1. return QItemDelegate::eventFilter(editor, event);
    To copy to clipboard, switch view to plain text mode 
    If it is a TAB and validates OK:
    Qt Code:
    1. emit commitData(lineEdit);
    2. emit closeEditor(lineEdit, QAbstractItemDelegate::EditNextItem);
    3. return true;
    To copy to clipboard, switch view to plain text mode 
    If not:
    Qt Code:
    1. emit closeEditor(lineEdit, QAbstractItemDelegate::NoHint);
    2. return true
    To copy to clipboard, switch view to plain text mode 
    Jim L
    Seattle, WA

  5. The following 2 users say thank you to jml for this useful post:

    jacek (4th August 2007), skuda (21st November 2007)

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.