• 5184阅读
  • 2回复

如何使 Key_Escape 在 QLineEdit 中 起作用? [复制链接]

上一主题 下一主题
离线wneunfn
 

只看楼主 倒序阅读 楼主  发表于: 2007-05-22
— 本帖被 XChinux 执行加亮操作(2008-07-18) —
如何使 Key_Escape 在 QLineEdit 中 起作用?

class Q_EXPORT myLineEdit: public QLineEdit

在类中:
void myLineEdit::keyPressEvent(QKeyEvent *e)
{
  switch(e->key())
  {
    case Key_Escape:
    {
      if (edited() && cursorPosition() > 0)
      {
        QKeyEvent ke(QEvent::KeyPress, Key_BackSpace, Key_BackSpace, 0);
        QLineEdit::keyPressEvent( &ke );
      }
      else
      {
      QLineEdit::keyPressEvent( e );
      }
      break;
    }
    default:
      QLineEdit::keyPressEvent( e );
  }
}

bool myLineEdit::event( QEvent * e)
{
  if ( e->type() == QEvent::KeyPress)
  {
    QKeyEvent* ke = (QKeyEvent*) e;
    if(ke->key() == Key_Escape)
    {
        //ke->ingore();
        ke->accept();
        //return QWidget::event( e );
        //return true;
    }
  }
  return QLineEdit::event( e );
}

结果发现根本捕获不到Key_Escape, 一按程序就直接退出了,请问该如何做才能达到目标?
[ 此贴被XChinux在2008-07-18 16:43重新编辑 ]
离线lenovosoft
只看该作者 1楼 发表于: 2007-05-23
void keyPressEvent(QKeyEvent *e)
    {
    qWarning("this is keypress event  %d",e->ascii());
    if(e->key()==65505)
    {
       this->clearFocus();
       emit closeEdit();
       return;
    }
}

看看,我这边没有问题,试试
离线wneunfn

只看该作者 2楼 发表于: 2007-05-30
这样的我也行,不过就是窗口还是退出了……

我自定义了一个键,KEY_CANCEL,然后都解决了……
快速回复
限100 字节
 
上一个 下一个