Qt入门之syszuxpinyin输入法应该的小知识

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011791262/article/details/24872051

移植好的syszuxpinyin输入法能正常的检测到控件焦点并自动弹出软键盘

当使用默认的QLineEdit控件时就有了一些小小的问题:

问题一:QLineEditt在默认情况下会自动出现焦点,从而导致一进入界面就弹出软键盘

但是我们需要点击一下控件它才弹出软键盘

解决方法:设置QLineEdit的属性为setFocusPolicy(Qt::StrongFocus) 或者是 setFocusPolicy(Qt::ClickFocus);

这是帮助文档里的一段说明:

The policy is Qt::TabFocus if the widget accepts keyboard focus by tabbing,Qt::ClickFocus if the widget accepts focus by clicking,

  Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it does not accept focus at all

问题二:在默认情况下能输入完了以后没有点其他控件而再次输入,简单点就是说点第一次的时候可以弹出,第二次的时候不能

原因是控件无法重新获得焦点,只有当它失去焦点的时候才能重新获得焦点,所以你必须先点其他控件让它失去焦点,然后再点它

重新获得焦点.在板子上试验过就知道

解决方法:子类化QLineEdit

.h文件

 
  1. #ifndef _QLINEEDITIM_H

  2. #define _QLINEEDITIM_H

  3.  
  4. #include <QLineEdit>

  5. #include <QMouseEvent>

  6.  
  7. class QLineEditIM : public QLineEdit

  8. {

  9. Q_OBJECT

  10.  
  11. public:

  12. explicit QLineEditIM(QWidget *parent = 0);

  13.  
  14. protected:

  15. virtual void mousePressEvent(QMouseEvent *event);

  16.  
  17. signals:

  18. void clicked();

  19.  
  20. public slots:

  21. void showInputMethod();

  22.  
  23. };

  24.  
  25. #endif //_QLINEEDITIM_H

.cpp文件

 
  1. #include "syszuxim.h"

  2. #include "syszuxpinyin.h"

  3. #include "qlineeditIM.h"

  4.  
  5. QLineEditIM::QLineEditIM(QWidget *parent):

  6. QLineEdit(parent)

  7.  
  8. {

  9.  
  10. QWSInputMethod *im = new SyszuxIM;

  11. QWSServer::setCurrentInputMethod(im);

  12.  
  13. connect(this, SIGNAL(clicked()), this, SLOT(showInputMethod()));

  14. }

  15.  
  16. void QLineEditIM::mousePressEvent(QMouseEvent *event)

  17. {

  18. if (event->button() == Qt::LeftButton)

  19. {

  20. emit clicked();

  21. }

  22.  
  23. QLineEdit::mousePressEvent(event);

  24. }

  25.  
  26.  
  27. /*

  28. * name : void showInputMethod()

  29. * Type : QEvent

  30. * Func : show inputthmod

  31. * in : Null

  32. * out : Null

  33. */

  34.  
  35. void QLineEditIM::showInputMethod()

  36. {

  37. clearFocus();

  38. setFocus();

  39.  
  40. }

猜你喜欢

转载自blog.csdn.net/g200407331/article/details/89763783
今日推荐