Results 1 to 4 of 4

Thread: How to know when field is edited within QTableView

  1. #1
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to know when field is edited within QTableView

    I have a table view within a window with many other widgets, and would like to disable other widgets if a field is edited within the table view. I would like to enable a commit button also.

    Qt Code:
    1. sourcemodel = new QSqlRelationalTableModel();
    2. sourcemodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    3. model = new jobItemSortFilterProxyModel();
    4. model->setSourceModel(sourcemodel);
    5. sourcemodel->setTable("jobitem");
    6. sourcemodel->select();
    To copy to clipboard, switch view to plain text mode 

    Pretty straight forward. How can I pick up when the various delegates, ie. text edit or spin box change the data?

    The dataChanged() signal fires when setData is called, not when editing begins.

    Derek

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to know when field is edited within QTableView

    Create a custom delegate and emit signals from:
    • QAbstractItemDelegate::setEditorData() (edit begins)
    • QAbstractItemDelegate::setModelData() (edit ends)


    You can easily achieve this by subclassing the default delegate implementation QItemDelegate, adding a couple of signals, overriding methods listed above and emitting corresponding signals from there.

    Qt Code:
    1. void MyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    2. {
    3. QItemDelegate::setEditorData(editor, index);
    4. emit editBegins();
    5. }
    6.  
    7. void MyItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
    8. {
    9. QItemDelegate::setModelData(editor, model, index);
    10. emit editEnds();
    11. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    dkite (30th December 2006)

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

    Default Re: How to know when field is edited within QTableView

    For simple uses you might just connect to the activated() signal of the view, but be warned, that it'll not handle all possible situations. It depends which edit triggers you use.

  5. #4
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to know when field is edited within QTableView

    For completeness, emitting a signal from a const method:

    Qt Code:
    1. void MyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    2. {
    3. QItemDelegate::setEditorData(editor, index);
    4. emit const_cast<MyItemDelegate*>(this)->editBegins();
    5. }
    To copy to clipboard, switch view to plain text mode 

    http://lists.trolltech.com/qt-intere...ad01411-0.html

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.