Results 1 to 14 of 14

Thread: QItemDelegate Editor Crash

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    35
    Thanked 1 Time in 1 Post

    Question Re: QItemDelegate Editor Crash

    All the problems I'm having are related to the QWidget* parameter in the QMessageBox() call that I'm using to warn the user. If I comment out only the QMessageBox call I do not get another call to setModelData(), nor do I get a crash.

    These will cause the crash in setModelData():
    Qt Code:
    1. QMessageBox::warning( editor,
    2. "Message Title",
    3. "Message String",
    4. QMessageBox::Ok, QMessageBox::NoButton );
    5.  
    6. QMessageBox::warning( qobject_cast<QWidget*>( m_parent ),
    7. "Message Title",
    8. "Message String",
    9. QMessageBox::Ok, QMessageBox::NoButton );
    To copy to clipboard, switch view to plain text mode 

    Is the problem that I cannot call QMessageBox from within the setModelData() function?

    If this is NOT the problem, what QWidget* pointer should I be using when I call QMessageBox? The above examples use the QWidget* editor parameter and the delegate owners pointer. What else could I use here?

  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: QItemDelegate Editor Crash

    In general you shouldn't do that because setModelData can be called without the editor. If you really need such a mechanism, use a custom event or something like that to trigger the message box after the new data is already in the model and the flow returns to the event loop.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    35
    Thanked 1 Time in 1 Post

    Question Re: QItemDelegate Editor Crash

    I am unfamiliar with customEvent. Since I already have an event filter for my base Table class, can I use it to receive the custom events?

    I'm posting custom events from my setModelData() function but they are never received by my customEvent() handler. Clearly I'm misunderstanding how I need to connect these together. Any suggestions?

    These are my class definitions:
    Qt Code:
    1. class TableEvent : public QEvent
    2. {
    3. public:
    4. TableEvent( Type type, const QString& sOrigValue ) : QEvent( type )
    5. {
    6. m_sOrigValue = sOrigValue;
    7. }
    8.  
    9. public:
    10. QString m_sOrigValue;
    11.  
    12. };
    13.  
    14.  
    15. class TableKeyHandler : public QObject
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. TableKeyHandler( Table* parent = 0 ) { m_pParent = parent; }
    21.  
    22. protected:
    23. bool eventFilter( QObject* obj, QEvent* pEvent );
    24. void customEvent( TableEvent* pEvent );
    25.  
    26. private:
    27. Table* m_pParent;
    28.  
    29. };
    To copy to clipboard, switch view to plain text mode 


    And this is how I'm attempting to process events?
    Qt Code:
    1. bool TableKeyHandler::eventFilter( QObject* obj, QEvent* pEvent )
    2. {
    3. if ( pEvent->type() == QEvent::KeyPress )
    4. {
    5. QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>( pEvent );
    6. int nKey = pKeyEvent->key();
    7. . . .
    8. }
    9. }
    10.  
    11.  
    12. void TableCellKeyHandler::customEvent( TableEvent* pEvent )
    13. {
    14. TableEvent* pTableEvent = static_cast<TableEvent*>( pEvent );
    15.  
    16. if ( pEvent->type() == EVENT_NAME_CHANGE )
    17. {
    18. // do validation of Name here
    19. pEvent->accept();
    20. }
    21. else
    22.  
    23. pEvent->ignore();
    24. }
    To copy to clipboard, switch view to plain text mode 

    And this is how I'm attempting to post a custom event:
    Qt Code:
    1. void DMXTableCellDelegate::setModelData( QWidget* editor, QAbstractItemModel* model,
    2. const QModelIndex& index ) const
    3. {
    4. if ( index.column() == COL_Name )
    5. {
    6. QLineEdit* lineEdit = qobject_cast<QLineEdit*>( editor );
    7.  
    8. // Validate the Name string
    9. if ( (lineEdit != NULL) || !lineEdit->text().isEmpty() )
    10. {
    11. QString sOrigName = index.data( Qt::EditRole ).toString();
    12. TableEvent* pEvent = new TableEvent( EVENT_NAME_CHANGE, sOrigName );
    13. QObject* pObj = qobject_cast<QObject*>(m_parent);
    14. static_cast<DMXTable*>( m_parent )->m_parent->m_pApp->postEvent( pObj, pEvent );
    15. }
    16. }
    17. . . .
    18. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2009
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: QItemDelegate Editor Crash

    Hello there!

    I know this is an ancient thread, but don't you hate it when a thread is full of questions and not answers?

    I have seen a crash similar to what you describe, but might not be exactly the same.
    The problem is due to the QMessageBox holding up the commit sequence, AND the model being reset at the same time.

    When you call QMessageBox::warning(), it will NOT return from the warning() call until the user has pressed OK on the warning box that appears.

    To make this happen, QMessageBox runs its own QT-event-processor loop within ::warning(), and that processes the gui drawing calls (to make the dialog appear and be interactive) AS WELL AS distributes and processes other events in the system.

    In my case, one of those posted events was updating the data in the table,
    which called model->reset(),
    which reset the editor,
    which called editor->deleteLater(),
    and then the DeleteLater event was processed, my editor was deleted.
    The trap is set.

    When the OK in QMessageBox is pressed, delegate->setModelData() can finally return.
    The very next call (in qabstractitemview.cpp) is editor->installEventFilter().
    So it calls a method on a stale and invalid pointer. FAIL.

    So my solution was to avoid opening a QMessageBox during setModelData().
    Instead, if there are error messages to display, I post a message to the tableview / dialog / something else
    and they can display the error message.

    Takeaway message: setModelData() should not start a new event loop.

Similar Threads

  1. QItemDelegate and QCheckBox
    By KShots in forum Qt Programming
    Replies: 5
    Last Post: 19th November 2008, 01:49
  2. setModelData for subclassed QItemDelegate
    By T4ng10r in forum Qt Programming
    Replies: 7
    Last Post: 27th May 2007, 13:09
  3. Problems with QItemDelegate
    By Jimmy2775 in forum Qt Programming
    Replies: 10
    Last Post: 12th June 2006, 21:18
  4. Replies: 3
    Last Post: 12th May 2006, 20:31

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.