QLineEdit - 焦点事件 [英] QLineEdit - focus event

查看:1455
本文介绍了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.

如果你想有一个聚焦信号,你将得到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 file你有:

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 :: focused )发送到您的 focus()方法(插槽)。

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

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

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