Results 1 to 9 of 9

Thread: closeEditor() not always closing QLineEdit

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

    Question closeEditor() not always closing QLineEdit

    While using my custom QItemDelegate, the QLineEdit editor is not always closing. The closeEditor() signal is emitted but the QLineEdit object remains in the QTableWidgetItem.

    I am using 4.2.2 and setting the edit triggers using QAbstractItemView::AllEditTriggers.

    This happens when I have multiple rows in the table and I click (speed is irrelevant) on the editable items:
    • Click on row 0, column 1
    • Click on row 1, column 1
    • Click on row 2, column 1
    • repeat
    Eventually one of the editors in column 1 will get stuck on, even though the closeEditor() signal was emitted. The qDebug() statement shows the contents of the item where the editor should be closed and is displayed even when the editor does not go away. This state does not allow any other editors to appear in the table, effectively stopping the user from editing any other items.

    Has anyone else seen this type of behavior? Any ideas how to prevent this?

    Qt Code:
    1. class DMXTableCellDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. DMXTableCellDelegate( QObject* parent = 0 ) : QItemDelegate( parent )
    7. { m_parent = parent; }
    8.  
    9. QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option,
    10. const QModelIndex& index ) const;
    11.  
    12. private slots:
    13. void commitAndCloseEditor( void );
    14.  
    15. private:
    16. QObject* m_parent;
    17.  
    18. };
    19.  
    20. QWidget* DMXTableCellDelegate::createEditor( QWidget* parent,
    21. const QStyleOptionViewItem& option,
    22. const QModelIndex& index ) const
    23. {
    24. if ( index.column() == COL_Name )
    25. {
    26. QLineEdit* lineEdit = new QLineEdit( parent );
    27. lineEdit->setMaxLength( DEVICE_NAME_MAX );
    28.  
    29. connect( lineEdit, SIGNAL( returnPressed() ),
    30. this, SLOT( commitAndCloseEditor() ) );
    31. connect( lineEdit, SIGNAL( editingFinished() ),
    32. this, SLOT( commitAndCloseEditor() ) );
    33.  
    34. return lineEdit;
    35. }
    36. else
    37. return QItemDelegate::createEditor( parent, option, index );
    38. }
    39.  
    40. void DMXTableCellDelegate::commitAndCloseEditor( void )
    41. {
    42. QLineEdit* editor = qobject_cast<QLineEdit*>( sender() );
    43. emit commitData( editor );
    44. qDebug( "emit closeEditor(): %s", editor->text().toAscii().data() );
    45. emit closeEditor( editor, QAbstractItemDelegate::EditNextItem );
    46. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: closeEditor() not always closing QLineEdit

    Are you sure you should emit the signal yourself? I'm not sure the signal is meant to be used this way. From what I see you should reimplement QAbstractItemDelegate::setModelData() instead.

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

    Default Re: closeEditor() not always closing QLineEdit

    Quote Originally Posted by wysota View Post
    Are you sure you should emit the signal yourself? I'm not sure the signal is meant to be used this way. From what I see you should reimplement QAbstractItemDelegate::setModelData() instead.
    I believe I'm safe using the code this way because I took it directly from an example in the C++ GUI Programming With Qt 4 book (Chap. 10 Implementing Custom Delegates).

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

    Default Re: closeEditor() not always closing QLineEdit

    I added the following connection to verify that the QLineEdit object was not being destroyed by the delegate:
    Qt Code:
    1. connect( lineEdit, SIGNAL( destroyed( QObject* ) ),
    2. this, SLOT( DEBUG_destroyed( QObject* ) ) );
    3.  
    4. . . .
    5.  
    6. void DMXTableCellDelegate::DEBUG_destroyed( QObject* obj )
    7. {
    8. qDebug( "destroyed() - 0x%x", obj );
    9. }
    To copy to clipboard, switch view to plain text mode 
    The output result was:
    emit closeEditor( 0x17118b8 ): Larry Fist
    destroyed() - 0x17118b8
    emit closeEditor( 0x1716900 ): Thing 2
    destroyed() - 0x1716900
    emit closeEditor( 0x1706258 ): Thing 1
    The destroyed() signals were received by my delegate for each focus change until the last when no destroyed() signal was received. At this point the editor was still active while focus was on another item.

    UPDATE:
    I placed a qDebug() call in the createEditor() and what I noticed, for 3 table rows, as I slowly clicked through each column 1 item, was that editor objects were created at only two memory locations. As soon as a third memory location was used the editor froze. A closeEditor() was emitted but no destroyed() signal was received. Could this be a clue?
    Last edited by mclark; 26th November 2007 at 22:21.

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

    Default Re: closeEditor() not always closing QLineEdit

    I think your connect statements are incorrect. editingFinished() is emited when return is pressed, so there is no need to connect to returnPressed as well... Maybe that's your problem? Lose one of those connect statements and see if the situation improves.

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

    Default Re: closeEditor() not always closing QLineEdit

    Quote Originally Posted by wysota View Post
    Lose one of those connect statements and see if the situation improves.
    Thanks wysota, you are partially right. By removing the returnPressed() connection other items can now be edited when an editor in an item gets stuck 'on'. But, I still get an editor stuck 'on', the delegate never gets sent the destroyed() signal.

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

    Default Re: closeEditor() not always closing QLineEdit

    Maybe there is a bug in your version of Qt? Upgrade might fix the problem. Or at least search the task-tracker to see if a similar issue was reported.

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

    mclark (26th November 2007)

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

    Default Re: closeEditor() not always closing QLineEdit

    Thanks again wysota, I'm putting together a 'simple' application to test my thought that this may be a bug in Qt 4.2.2. I may have to update to 4.3... If I find a solution I'll post it here.

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

    Default Re: closeEditor() not always closing QLineEdit

    As I said, check the task-tracker. It might save you a lot of time and effort.

Similar Threads

  1. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 01:25

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.