Results 1 to 2 of 2

Thread: QLineEdit::cursorPositionAt(const QPoint) with Qt::AlignRight

  1. #1
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QLineEdit::cursorPositionAt(const QPoint) with Qt::AlignRight

    Hello!

    when entering in a qlineedit I wish to set the cursor on the position where the click was done...

    here is my code:

    Qt Code:
    1. QPoint myPosition = this->mapFromGlobal(mouseClickPosition);
    2. int txtCursorToSet = cursorPositionAt(myPosition );
    3. setCursorPosition(txtCursorToSet);
    To copy to clipboard, switch view to plain text mode 

    This work only when the alignment() is Qt::AlignLeft... What should I do to get the same result with Qt::AlignHCenter and Qt::AlignRight?

  2. #2
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLineEdit::cursorPositionAt(const QPoint) with Qt::AlignRight

    Well... I waited for a couple of weeks but no-one answered...

    I found out a work-around... but maybe someone can provide better solutions! Or at least any documentation about that topic?

    Qt Code:
    1. QPoint myPos = this->mapFromGlobal(mousePosition); // convert coordinates of the mouse click
    2.  
    3. if((alignment() &= Qt::AlignLeft) == Qt::AlignLeft) //when left alignment
    4. {
    5. int temp = cursorPositionAt(myPos); //use the easy way
    6. setCursorPosition(temp);
    7. return;
    8. }
    9. //cursorPositionAt works only for left or justified alignment,
    10. //with other alignment it estimate always believing it is left alignment.
    11. //deplacing the point of click is a workaround to this lack
    12. QFontMetrics fm = fontMetrics();
    13. int textWidth = fm.boundingRect(text()).width();
    14.  
    15. setCursorPosition(0); //set at 0 to get the cursorRect() at the right place
    16.  
    17. int marginWidth = cursorRect().right();
    18. int rePosition = width();
    19. if((alignment() &= Qt::AlignRight) == Qt::AlignRight)
    20. {
    21. rePosition = rePosition - textWidth + marginWidth;
    22. }
    23. else //center alignment
    24. {
    25. rePosition = rePosition / 2;
    26. rePosition = rePosition - (textWidth / 2) + marginWidth;
    27. }
    28.  
    29. myPos.setX(myPos.x() - rePosition);
    30. int temp = cursorPositionAt(myPos);
    31. setCursorPosition(temp);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Max and min from QVector<QPoint>
    By awpitt13 in forum Newbie
    Replies: 4
    Last Post: 14th February 2012, 01:18
  2. Getting widget from Qpoint
    By codeman in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 15:39
  3. Replies: 1
    Last Post: 4th December 2009, 17:03
  4. Getting const char * from QLineEdit
    By squidge in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2009, 09:28
  5. const member and const method
    By mickey in forum General Programming
    Replies: 8
    Last Post: 9th April 2008, 09:44

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.