Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

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:

enter image description here

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

I want it to display:

enter image description here

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?

like image 300
Macbeth's Enigma Avatar asked Feb 19 '13 22:02

Macbeth's Enigma


People also ask

How to insert cursor at the beginning of every selected line?

Insert cursor at the beginning of every selected line: As soon as you see the cursor blinking at the end of every line, do the following: Windows: tab twice on your keyboard’s home button. Now you can add whatever you need to at the beginning of your lines.

How do I move the cursor to the end of line?

Vim has a straightforward way to move the cursor to the end of the line. Again, you need to be in Normal mode to do this. It does not matter in which column your cursor, only which line it is on. Then, press the $ key and it will move the cursor to the end of the line.

How do I add lines at the beginning of a line?

As soon as you see the cursor blinking at the end of every line, do the following: Windows: tab twice on your keyboard’s home button. Now you can add whatever you need to at the beginning of your lines.


1 Answers

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();
}
like image 79
Jakob Leben Avatar answered Oct 07 '22 01:10

Jakob Leben