GG.*_*GG. 14 qt qt-designer qlineedit
我使用QtDesigner创建了一个对话框.QLineEdit对话框中有一个对象带有一些默认内容.当对话框初始化并且焦点转到时QLineEdit,我希望自动选择默认内容,因此一旦用户开始写入,之前的内容将被覆盖.
编辑:
在构造函数中:
dialog->accept();
Run Code Online (Sandbox Code Playgroud)
和
connect( dialog, SIGNAL(accepted()), QlineObj, SLOT( selectAll() ) );
Run Code Online (Sandbox Code Playgroud)
呼叫
lineEdit->selectAll();
Run Code Online (Sandbox Code Playgroud)
设置默认文本后.(也许在对话框构造函数中.)
有一种更简单的方法可以获得几乎相同的行为,即使用setPlaceholderText()而不是setText()设置默认内容.这将显示灰色的默认内容,一旦QLineEdit获得焦点,它将消失.
这是一个比较老的问题,但是,尽管如此,我还是在这里寻找这个确切问题的解决方案。可以通过以下方式解决:
创建一个派生自QLineEdit并覆盖focusInEvent标头中的类的类:
virtual void focusInEvent(QFocusEvent *event) override;
Run Code Online (Sandbox Code Playgroud)
然后像这样实现它:
void MyLineEdit::focusInEvent(QFocusEvent *event)
{
// First let the base class process the event
QLineEdit::focusInEvent(event);
// Then select the text by a single shot timer, so that everything will
// be processed before (calling selectAll() directly won't work)
QTimer::singleShot(0, this, &QLineEdit::selectAll);
}
Run Code Online (Sandbox Code Playgroud)
万一其他人想知道如何做到这一点;-)