Results 1 to 2 of 2

Thread: QLineEdit actions in Delegate

  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 QLineEdit actions in Delegate

    Using Qt 4.4.2, I have a QTableWidget where one of the columns contains a string representation of a UUID (2C44E5CE-C463-3CE4-B4D6-785C0F93124A). I use delegates to handle all the in-cell editing in my table (QLineEdit & QComboBoxes) which works fine.

    I am required to support editing the UUID and need to validate the entry. My problem is that when the QLineEdit object is created the entire UUID is selected and the cursor is at the end of the string. If I explicitly select the first character I can change it but then the cursor moves to the end of the string. Having the cursor continually moving to the end is not acceptable behavior when editing a 32-character value. I can paste an existing UUID into the QLineEdit object just fine, the problem is editing character-by-character.

    The default string is either a valid UUID or a 'NULL' UUID (00000000-0000-0000-0000-000400000000). I want to be able to start typing at the leftmost character of the string and continue typing without having to reselect the character I want to replace.

    I must be doing something wrong or just forgetting to do something to accomplish this. Does anyone have any suggestions?

    Qt Code:
    1. QWidget* UUIDColumnDelegate::createEditor( QWidget* parent,
    2. const QStyleOptionViewItem& option,
    3. const QModelIndex& index ) const
    4. {
    5. QLineEdit* lineEdit = new QLineEdit( parent );
    6. if ( lineEdit == NULL )
    7. {
    8. // report error
    9. return QItemDelegate::createEditor( parent, option, index );
    10. }
    11.  
    12. QString sRegExp( "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$" );
    13. QRegExp regExp( sRegExp, Qt::CaseInsensitive, QRegExp::RegExp );
    14. QRegExpValidator* uuidValidator = new QRegExpValidator( regExp, lineEdit );
    15. if ( uuidValidator == NULL )
    16. {
    17. // report error
    18. return QItemDelegate::createEditor( parent, option, index );
    19. }
    20.  
    21. lineEdit->setInputMask( "HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH" );
    22. lineEdit->setMaxLength( 36 );
    23. lineEdit->setCursorPosition( 0 ); // appears to have no effect
    24. lineEdit->setSelection( 0, 1 ); // appears to have no effect
    25. lineEdit->setValidator( uuidValidator );
    26.  
    27. connect( lineEdit, SIGNAL( textEdited( const QString& ) ),
    28. this, SLOT( uuidTextEdited( const QString& ) ) );
    29. connect( lineEdit, SIGNAL( editingFinished() ),
    30. this, SLOT( commitAndCloseEditor() ) );
    31.  
    32. lineEdit->installEventFilter( const_cast<UUIDColumnDelegate*>( this ) );
    33. return lineEdit;
    34. }
    35.  
    36. void AbstractColumnDelegate::uuidTextEdited( const QString& sValue )
    37. {
    38. if ( sValue.isEmpty() )
    39. return;
    40. QString sText = sValue.toUpper();
    41. qobject_cast<QLineEdit*>( sender() )->setText( sText );
    42. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QLineEdit actions in Delegate

    In case anyone runs into this problem: I now have the QLineEdit object acting mostly civilized. It's not perfect but acceptable.

    I removed the following code from the createEditor() method:

    Qt Code:
    1. lineEdit->setCursorPosition( 0 ); // appears to have no effect
    2. lineEdit->setSelection( 0, 1 ); // appears to have no effect
    To copy to clipboard, switch view to plain text mode 

    I added a slot for the cursorPositionChanged() connection that does:
    Qt Code:
    1. void AbstractColumnDelegate::uuidCursorChanged( int nOldPos, int nNewPos )
    2. {
    3. if ( nNewPos == 36 ) // 36 is the string max
    4. {
    5. QLineEdit* pEditor = qobject_cast<QLineEdit*>( sender() );
    6.  
    7. // Allow for full selection from either direction using mouse
    8. // or Ctrl+Shift Arrow keys
    9. if ( pEditor->selectionStart() == 0 )
    10. return;
    11.  
    12. int nPos = nOldPos + 1;
    13.  
    14. // Left to right editing: skip over the '-' characters
    15. if ( (nPos == 8) || (nPos == 13) || (nPos == 18) || (nPos == 23) )
    16. nPos++;
    17.  
    18. pEditor->setCursorPosition( nPos );
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    This allows the cursor to move 1 character at a time right or left, skip over the '-' characters and allows the entire string to be selected from either the right or left using a mouse drag or the Ctrl+Shift+Arrow keys.

Similar Threads

  1. Logging Qt User Actions
    By Frank J. Lhota in forum Qt Programming
    Replies: 14
    Last Post: 30th May 2014, 22:36
  2. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 23:55
  3. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 16:13
  4. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 02: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.