如何从QLineEdit连接焦点事件? [英] How to connect focus event from QLineEdit?

查看:105
本文介绍了如何从QLineEdit连接焦点事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将焦点事件从某些 QLineEdit 元素( ui-> lineEdit )连接到方法 focus() .我该怎么办?

I have to connect focus event from some QLineEdit element (ui->lineEdit) to the method focus(). How can I do this?

推荐答案

当QLineEdit获得焦点时,没有信号发出.因此,将方法连接到焦点事件的想法并不直接合适.

There is no signal emitted when a QLineEdit gets the focus. So the notion of connecting a method to the focus event is not directly appropriate.

如果要具有 focused 信号,则必须派生QLineEdit类.这是如何实现此目标的示例.

If you want to have a focused signal, you will have to derive the QLineEdit class. Here is a sample of how this can be achieved.

myLineEdit.h 文件中,您具有:

class MyLineEdit : public QLineEdit
{
  Q_OBJECT

public:
  MyLineEdit(QWidget *parent = 0);
  ~MyLineEdit();

signals:
  void focussed(bool hasFocus);

protected:
  virtual void focusInEvent(QFocusEvent *e);
  virtual void focusOutEvent(QFocusEvent *e);
};

myLineEdit.cpp 文件中,您具有:

MyLineEdit::MyLineEdit(QWidget *parent)
 : QLineEdit(parent)
{}

MyLineEdit::~MyLineEdit()
{}

void MyLineEdit::focusInEvent(QFocusEvent *e)
{
  QLineEdit::focusInEvent(e);
  emit(focussed(true));
}

void MyLineEdit::focusOutEvent(QFocusEvent *e)
{
  QLineEdit::focusOutEvent(e);
  emit(focussed(false));
}

您现在可以将 MyLineEdit :: focussed()信号连接到您的 focus()方法(插槽).

You can now connect the MyLine:focussed() signal to your focus() method (slot).

这篇关于如何从QLineEdit连接焦点事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆