信号单击QSpinBox Qt [英] Signal click on QSpinBox Qt

查看:106
本文介绍了信号单击QSpinBox Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击 QSpinBox 时,我想打开一个窗口.问题在于此小部件没有点击"这样的信号.

I would like to open a window when I click on a QSpinBox. The problem is that there is no such signal "clicked" for this widget.

有人知道怎么做吗?

推荐答案

QSpinBox 只是带有两个按钮,输入验证和事件处理的 QLineEdit .它没有 clicked 信号,因为它甚至可以处理鼠标本身.

A QSpinBox is just a QLineEdit with two buttons, input validation and event handling. It doesn't have clicked signal because it's supposed to handle the mouse even itself.

问题在于,即使制作从 QSpinBox 派生的自定义窗口小部件也是不够的,因为它本身不接收鼠标事件,它们由其子窗口小部件处理.您可以在 QSpinBox 子级上安装事件过滤器,以捕获click事件,但这不是最巧妙的方法.

The problem is that even making a custom widget derived from QSpinBox won't be enough since it doesn't receive the mouse events itself, they are handled by its children widgets. You could install an event filter on the QSpinBox children in order to catch the click event, but that's not the neatest way.

如果用户选择框时只想显示数字键盘,则可以直接使用 QLineEdit .您将丢失 QSpinBox 按钮(但需要时可以添加自己的按钮)和验证(但可以使用 QValidator 添加自己的按钮).

If you just want to display a numpad when the user select the box, you can use directly a QLineEdit. You will lose the QSpinBox buttons (but you can add your own ones if you need them) and the validation (but you can add you own using QValidator).

然后,您只需要派生它即可捕获 focus 事件,并触发显示键盘的自定义信号:

Then you just have to derive it in order to catch the focus event, trigger a custom signal which would show your keyboard :

class MySpinBox: public QLineEdit
{
  Q_OBJECT

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

signals:
  needNumpad(bool hasFocus);

protected:
  virtual void focusInEvent(QFocusEvent *e) {
      QLineEdit::focusInEvent(e);
      emit(needNumpad(true));
  }
  virtual void focusOutEvent(QFocusEvent *e) {
      QLineEdit::focusInEvent(e);
      emit(needNumpad(false));
  }
}

这篇关于信号单击QSpinBox Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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