qt - 在焦点上选择 QLineEdit 的文本

标签 qt qt-designer qlineedit

我使用 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/

相关文章:

c++ - 使用 C 函数填充 QByteArray

c++ - Qt Creator 中的 Qt GUI 应用程序build设置

python - 使用 QT 设计器创建的 PyQt5 程序从终端打开时不显示任何窗口

python - 动态创建选项卡QTabWidget和填充表格QTableWidget

c++ - Qt (C++) : Add background text in line edit

c++ - 链接点击信号QWebEngineView

c++ - VC++ 2008下qtwinmigrate + qtpropertybrowser的编译组合

python - 为什么我不能在graphicsview上使用鼠标事件(mouseMoveEvent, mouseReleaseEvent)?

python - 将 QLineEdit 设置为只读但仍接受 Drops

python - PyQt : Set text in a QLabel adding letter by letter