如何使光标从其内容的开头用QLineEdit? [英] How to make the cursor start at the beginning of its contents with a QLineEdit?

查看:155
本文介绍了如何使光标从其内容的开头用QLineEdit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 7 SP1

MSVS 2010

Qt 4.8.4

Windows 7 SP1
MSVS 2010
Qt 4.8.4

此代码:

#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();
}

显示此:

请注意, AB被截断,光标在行编辑的结尾。

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

我想显示:

>

FG被截断,光标在行编辑的开头。

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

我试图将setCursorPosition和cursorBackward设置为无效。如果我通过字体指标的elidedText转换文本,它将从开头显示结尾的...。但是我不想这样做。

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.

问题:有没有办法使光标在显示QLineEdit后在其内容的开头开始?

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

推荐答案

在设置文本后将光标位置设置为0应该很好。至少它在Linux上的Qt 4.8.3。

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();
}

这篇关于如何使光标从其内容的开头用QLineEdit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆