QT QLineEdit 获取焦点/获取焦点后全选字符

为了实现  QLineEdit 获取焦点/获取焦点后全选字符的功能,在网上先查了 QLineEdit 获取焦点的方法,有两种:
1 此方法只有在窗体从失去焦点到获取焦点时有作用:即窗体失去焦点后再次获取焦点时,将焦点置于某个 QLineEdit 中。
 此功能明显不是偶想要的!!! 但由于网上现有的文章都是一样的,说的不是很清楚,所以只有试了才知道。
有三段代码:
1) 在窗体初始化时增加如下代码:

1     // QLineEdit 获取焦点 1 - 此方法错误: 只有在窗体从失去焦点到获取焦点时有作用
2     ui->ledSendStr->installEventFilter(this);
3     ui->ledSendFile->installEventFilter(this);
4     ui->ledSendPreTime->installEventFilter(this);

2) 在窗体的 .h 文件中声明 SLOT,如下:

1 public slots:
2     bool eventFilter(QObject *,QEvent *);       // QLineEdit 获取焦点 2

3) 实现的代码

 1 // QLineEdit 获取焦点 3
 2 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
 3 {
 4     if(watched == ledSendStr)
 5     {
 6         if(QEvent::FocusIn == event->type())
 7         {
 8             qDebug() << "focus in";
 9             ledSendStr->selectAll();        // 不起使用,只有在窗体获取到焦点时才有功能
10             ledSendStr->setFocus(Qt::OtherFocusReason);
11         }
12         else if(QEvent::FocusOut == event->type())
13         {
14             qDebug() << "focus out";
15         }
16     }
17     else if(watched == ledSendFile)
18     {
19     }
20     else if(watched == ledSendPreTime)
21     {
22     }
23     return QWidget::eventFilter(watched,event);     // 最后将事件交给上层对话框
24 }

 

2 继承 QLineEdit, 再实现如下两个 SIGNAL:
  virtual void focusInEvent(QFocusEvent *e);
  virtual void focusOutEvent(QFocusEvent *e);

1) 继承类的声明:

 1 #ifndef MYLINEEDIT_H
 2 #define MYLINEEDIT_H
 3 
 4 #include <QLineEdit>
 5 #include <QDebug>
 6 
 7 class MyLineEdit: public QLineEdit
 8 {
 9     Q_OBJECT
10 
11  public:
12     MyLineEdit(QWidget *parent=0);
13     ~MyLineEdit();
14 
15  protected:
16     virtual void focusInEvent(QFocusEvent *e);
17     virtual void focusOutEvent(QFocusEvent *e);
18 };
19 
20 #endif // MYLINEEDIT_H

2) 继承类的实现:

 1 #include "myLineEdit.h"
 2 
 3 MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
 4 {
 5 }
 6 
 7 MyLineEdit::~MyLineEdit()
 8 {
 9 }
10 
11 void MyLineEdit::focusInEvent(QFocusEvent *e)
12 {
13     // qDebug() << "*MyLineEdit::focusInEvent" << this->objectName();
14 
15     QPalette pGreen = QPalette();
16     pGreen.setColor(QPalette::Base,Qt::gray);    //QPalette::Base 对可编辑输入框有效,还有其他类型,具体的查看文档
17     setPalette(pGreen);
18     
19     ledSendStr->selectAll();        // 不起使用,只有在窗体获取到焦点时才有功能
20     ledSendStr->setFocus(Qt::OtherFocusReason);
21 }
22 
23 void MyLineEdit::focusOutEvent(QFocusEvent *e)
24 {
25     // qDebug() << "*MyLineEdit::focusOutEvent" << this->objectName();
26 
27     QPalette pWhite = QPalette();
28     pWhite.setColor(QPalette::Base,Qt::white);
29     setPalette(pWhite);
30 }

3) 使用 MyLineEdit 类定义控件,并完成布局。
测试运行发现,3) 中定义的控件随着焦点的获取/失去颜色会发生变化,但全选字符串的功能却无效!
但如果在如下代码:

1 ledSendStr->selectAll();
2 ledSendStr->setFocus(Qt::OtherFocusReason);

放在一个按键的 Clicked 槽中就可以实现全选字符串的功能。 为什么无效呢???原因不知道,想到的区别就是同样的代码,一个是在 MainWindow 中、而另一个是在 myLineEdit 中。

试着将 Focus Get/Lost 动作放到 MainWindow 中响应:通过 SIGNAL/SLOT 来实现。 1) 在继承类的声明中增加如下代码:

1 signals:
2     void selectAllWhenFocus(QString strCtrlName);

2) 在继承类实现的 void MyLineEdit::focusInEvent(QFocusEvent *e) 函数中增加如下代码:

1 emit selectAllWhenFocus(this->objectName());

3) 在 MainWindow 的 .h 文件中增加如下代码:

1 public slots:
2     void selectAllWhenFocusSlot(QString strCtrlName);

4) 在 MainWindow 的源代码中增加如下代码:

1 void MainWindow::selectAllWhenFocusSlot(QString strCtrlName)
2 {
3     // qDebug() << "Main Windows: " << strCtrlName;
4 
5     ledSendStr->selectAll();
6     ledSendStr->setFocus(Qt::OtherFocusReason);
7 }

但发现一样无法实现全选控件中字符串的功能。 无语......

最后无奈下想到延时一下,再做全选的动作。修改上述第四步的代码如下:

 1 void MainWindow::selectAllWhenFocusSlot(QString strCtrlName)
 2 {
 3     // qDebug() << "Main Windows: " << strCtrlName;
 4 
 5     m_strCtrlName = strCtrlName;
 6     if(NULL != m_timerDelaySelect)
 7     {
 8         if(m_timerDelaySelect->isActive())
 9         {
10             m_timerDelaySelect->stop();
11         }
12         delete m_timerDelaySelect;
13         m_timerDelaySelect = NULL;
14     }
15     m_timerDelaySelect = new QTimer(this);
16     //新建定时器
17     connect(m_timerDelaySelect,SIGNAL(timeout()),this,SLOT(timerUpDateSeleceAll()));
18     m_timerDelaySelect->start(200);
19 }

和延时超时的处理函数:

 1 void MainWindow::timerUpDateSeleceAll()
 2 {
 3     if(m_strCtrlName == "ledSendPreTime")
 4     {
 5         ledSendPreTime->selectAll();
 6         ledSendPreTime->setFocus(Qt::OtherFocusReason);
 7     }
 8     else if(m_strCtrlName == "ledSendStr")
 9     {
10         ledSendStr->selectAll();
11         ledSendStr->setFocus(Qt::OtherFocusReason);
12     }
13     else if(m_strCtrlName == "ledSendFile")
14     {
15         ledSendFile->selectAll();
16         ledSendFile->setFocus(Qt::OtherFocusReason);
17     }
18 
19     if(NULL != m_timerDelaySelect)
20     {
21         if(m_timerDelaySelect->isActive())
22         {
23             m_timerDelaySelect->stop();
24         }
25         delete m_timerDelaySelect;
26         m_timerDelaySelect = NULL;
27     }
28 }

发现这样全选控件中文符串的功能竟然实现了!!!

在实现上述功能时,遇到的第一个问题其实不是代码,而不是 Layout 的问题: 之前所有控件是“拖”出来的,然后进行了 Layout 处理。忽然发现按上述修改,使用 MyLineEdit 类定义的第个控件是在代码中定义的,无法像之前一样“拖”到窗体中、并完成 Layout! 还好 QT 的代码  Layout 功能比较强大。最后两者结合才完成了想要的窗体 Layout。

 

posted @ 2016-05-23 21:11  91program  阅读(16946)  评论(1编辑  收藏  举报