Results 1 to 1 of 1

Thread: How to make the cursor start at the beginning of its contents with a QLineEdit?

  1. #1
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    3
    Qt products
    Qt4

    Default Re: How to make the cursor start at the beginning of its contents with a QLineEdit?

    Windows 7 SP1<br>
    MSVS 2010<br>
    Qt 4.8.4<br>

    This code:

    #include <QTGui>
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QMainWindow* window = new QMainWindow;
    5. QLineEdit* line_edit = new QLineEdit;
    6.  
    7. line_edit->setText("ABCDEFG");
    8. line_edit->setFixedSize(40,20);
    9. window->setCentralWidget(line_edit);
    10. window->show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Displays this:

    At end.jpg

    Note that the "AB" is truncated and the cursor is at the end of the line edit.

    I want it to display:

    at start.jpg

    Here "FG" is truncated and the cursor is at the beginning of the line edit.

    I've tried to setCursorPosition and cursorBackward to no avail. If I convert the text via the font metric's elidedText it will display from the beginning with the trailing "...". But I don't want to do that.

    Question: Is there a way to make the cursor to start at the beginning of its contents after displaying a QLineEdit?

    Windows 7 SP1<br>
    MSVS 2010<br>
    Qt 4.8.4<br>

    This code:

    #include <QTGui>
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QMainWindow* window = new QMainWindow;
    5. QLineEdit* line_edit = new QLineEdit;
    6.  
    7. line_edit->setText("ABCDEFG");
    8. line_edit->setFixedSize(40,20);
    9. window->setCentralWidget(line_edit);
    10. window->show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Displays this:

    At end.jpg

    Note that the "AB" is truncated and the cursor is at the end of the line edit.

    I want it to display:

    at start.jpg

    Here "FG" is truncated and the cursor is at the beginning of the line edit.

    I've tried to setCursorPosition and cursorBackward to no avail. If I convert the text via the font metric's elidedText it will display from the beginning with the trailing "...". But I don't want to do that.

    Question: Is there a way to make the cursor to start at the beginning of its contents after displaying a QLineEdit?


    Added after 38 minutes:


    Actually
    Qt Code:
    1. line_edit->setCursorPosition(0);
    To copy to clipboard, switch view to plain text mode 
    works just fine in the above code. I am having this problem in a much larger program where setCursorPostion did not work. I melted the issue down to a simple program for illustration but neglected to test the setCursorPostion there. I'll need to analyze further why this isn't working in my larger program.


    Added after 11 minutes:


    I found my problem in my larger program: I was setting the cursor position before I was setting the text. Setting it after setting the text solved my problem.
    Last edited by therefore; 19th February 2013 at 23:23.

Similar Threads

  1. QLineEdit cursor lost when switching between layouts
    By missoni in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2011, 12:48
  2. set cursor position in qlineedit with an inputmask
    By guitar1 in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2011, 17:13
  3. Replies: 2
    Last Post: 2nd October 2009, 15:32
  4. QLineEdit keep cursor active.
    By bunjee in forum Qt Programming
    Replies: 10
    Last Post: 2nd September 2009, 14:50
  5. how to make these contents work??
    By srohit24 in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2009, 14:02

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.