PDA

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



therefore
19th February 2013, 23:23
Windows 7 SP1<br>
MSVS 2010<br>
Qt 4.8.4<br>

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:

8737

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

I want it to display:

8738

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>

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:

8737

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

I want it to display:

8738

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

line_edit->setCursorPosition(0);
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.