QlineEdit selectAll 不起作用?
QlineEdit selectAll doesn't work?
我使用以下代码。其中 lineEdit->selectAll()
由 pushButton 调用,仅在 首次启动时由 eventFilter
调用。尽管 label->setText
一直正常工作。为什么?
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEdit->installEventFilter(this);
}
void Widget::on_pushButton_clicked()
{
ui->lineEdit->selectAll();
}
bool Widget::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->lineEdit && event->type() == QEvent::FocusIn )
{
ui->lineEdit->selectAll();
ui->label->setText("Focused!");
return false;
}
if (object == ui->lineEdit && event->type() == QEvent::FocusOut )
{
ui->label->setText("unFucused!");
return false;
}
return false;
}
UPD:做了 Ilya 推荐的。仍然有同样的问题。
void myLine::focusInEvent(QFocusEvent* event)
{
setText("Focused!");
selectAll();
}
void myLine::focusOutEvent(QFocusEvent* event)
{
setText("UnFocused!");
}
因为你错误地使用了eventFilter:
bool Widget::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->lineEdit && event->type() == QEvent::FocusIn )
{
ui->lineEdit->selectAll();
ui->label->setText("Focused!");
}
if (object == ui->lineEdit && event->type() == QEvent::FocusOut )
{
ui->label->setText("unFucused!");
}
return QWidget::eventFilter(object, event);
}
没有真正回答问题,但有更多 "standard" 自定义这些事件的方法。
创建一个 QLineEdit 子类并定义您自己的 focusInEvent
/ focusOutEvent
处理程序。
如果您使用的是 UI 设计器,请将您的 lineEdit 升级到您的子类(右键单击 > "Promote to")。
在这里找到答案Select text of QLineEdit on focus
而是ui->lineEdit->selectAll()
应该使用 QTimer::singleShot(0,ui->lineEdit,SLOT(selectAll()))
,
因为 mousePressEvent
在 focusInEvent
之后立即触发,所以在 focusInEvent
中选择的文本未被 mousePressEvent
选择。
我使用以下代码。其中 lineEdit->selectAll()
由 pushButton 调用,仅在 首次启动时由 eventFilter
调用。尽管 label->setText
一直正常工作。为什么?
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEdit->installEventFilter(this);
}
void Widget::on_pushButton_clicked()
{
ui->lineEdit->selectAll();
}
bool Widget::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->lineEdit && event->type() == QEvent::FocusIn )
{
ui->lineEdit->selectAll();
ui->label->setText("Focused!");
return false;
}
if (object == ui->lineEdit && event->type() == QEvent::FocusOut )
{
ui->label->setText("unFucused!");
return false;
}
return false;
}
UPD:做了 Ilya 推荐的。仍然有同样的问题。
void myLine::focusInEvent(QFocusEvent* event)
{
setText("Focused!");
selectAll();
}
void myLine::focusOutEvent(QFocusEvent* event)
{
setText("UnFocused!");
}
因为你错误地使用了eventFilter:
bool Widget::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->lineEdit && event->type() == QEvent::FocusIn )
{
ui->lineEdit->selectAll();
ui->label->setText("Focused!");
}
if (object == ui->lineEdit && event->type() == QEvent::FocusOut )
{
ui->label->setText("unFucused!");
}
return QWidget::eventFilter(object, event);
}
没有真正回答问题,但有更多 "standard" 自定义这些事件的方法。
创建一个 QLineEdit 子类并定义您自己的
focusInEvent
/focusOutEvent
处理程序。如果您使用的是 UI 设计器,请将您的 lineEdit 升级到您的子类(右键单击 > "Promote to")。
在这里找到答案Select text of QLineEdit on focus
而是ui->lineEdit->selectAll()
应该使用 QTimer::singleShot(0,ui->lineEdit,SLOT(selectAll()))
,
因为 mousePressEvent
在 focusInEvent
之后立即触发,所以在 focusInEvent
中选择的文本未被 mousePressEvent
选择。