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

c++, qt

Solution

Setting cursor position to 0 just after setting text should work just fine. At least it does here on Linux, Qt 4.8.3.

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow*          window = new QMainWindow;
    QVBoxLayout*          layout = new QVBoxLayout;
    QLineEdit*         line_edit = new QLineEdit;

    line_edit->setText("ABCDEFG");
    line_edit->setFixedSize(40,20);
    line_edit->setCursorPosition(0);
    layout->addWidget(line_edit);
    window->setCentralWidget(line_edit);
    window->show();
    return app.exec();
}

Problem

Windows 7 SP1 MSVS 2010 Qt 4.8.4 This code: ``` #include <QTGui> int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow* window = new QMainWindow; QLineEdit* line_edit = new QLineEdit; line_edit->setText("ABCDEFG"); line_edit->setFixedSize(40,20); window->setCentralWidget(line_edit); window->show(); return app.exec(); } ``` Displays this: Note that the "AB" is truncated and the cursor is at the end of the line edit. I want it to display: 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?

Original source