我使用 QtDesigner 创建了一个对话框。有一个QLineEdit
对话框中的对象具有一些默认内容。当对话框初始化并且焦点转到 QLineEdit
,我想自动选择默认内容,所以一旦用户开始写作,之前的内容将被覆盖。
编辑:
在构造函数中:
dialog->accept();
和
connect( dialog, SIGNAL(accepted()), QlineObj, SLOT( selectAll() ) );
最佳答案
这是一个较旧的问题,但尽管如此,我最终还是在这里寻找解决这个确切问题的方法。可以通过以下方式解决:
创建一个派生自 QLineEdit
的类并覆盖 focusInEvent
在标题中:
virtual void focusInEvent(QFocusEvent *event) override;
然后像这样实现它:
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);
}
以防万一其他人想知道如何做到这一点;-)
关于qt - 在焦点上选择 QLineEdit 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434174/