简体   繁体   English

如何从Q(double)spinbox捕获信号

[英]How to capture signal from Q(double)spinbox

I'm writing a mini application by means of Qt 4.7. 我正在通过Qt 4.7编写一个小型应用程序。 And I have a reoccurring problem with some QSpinBoxes and QDoubleSpinBoxes. 我在某些QSpinBoxes和QDoubleSpinBoxes中经常遇到问题。 I set the editingFinished() signal and when I change the value in any of these fields they send two signals: when the spinbox loses focus and when enter is pressed. 我设置了editingFinished()信号,并且当我在这些字段中的任何一个字段上更改值时,它们都会发送两个信号:当旋转框失去焦点时和按下enter键时。 So when I press the tab or enter button my program makes the calculations twice. 因此,当我按下选项卡或输入按钮时,我的程序将进行两次计算。 Is there any smart and easy way to set only lostFocus signal? 有什么聪明又简单的方法可以只设置lostFocus信号?

PS I'm newbie in Qt. PS我是Qt的新手。 Sorry for my english, I still learning. 对不起,我的英语,我还在学习。

edit: 编辑:

Thanks a lot for your help netrom! 非常感谢您对netrom的帮助!

But it is still something wrong...Should it looks like below? 但这还是有问题的...应该看起来像下面吗? I can compile it and run, but it seems SpinBox still react on Enter button. 我可以编译并运行它,但是SpinBox似乎仍然对Enter按钮做出反应。

dialog.h: dialog.h:

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QSpinBox>
#include <QKeyEvent>

namespace Ui {
    class SpinBox;
    class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;

private slots:
    void on_spinBox_editingFinished();
};

class SpinBox : public QSpinBox
{
  Q_OBJECT

public:
  explicit SpinBox(QWidget *parent = 0) : QSpinBox(parent) { }

protected:
  void keyPressEvent(QKeyEvent *event) {
    switch (event->key()) {
    case Qt::Key_Return:
    case Qt::Key_Enter:
      return;

    default: break;
    }

    QSpinBox::keyPressEvent(event);
  }
};
#endif // DIALOG_H

You could try checking if the spinbox widget has the focus at the beginning of your slot, it should tell you if the editingFinished() signal was the result of Enter/Return key or the loss of focus. 您可以尝试检查Spinbox小部件在插槽的开头是否具有焦点,它应该告诉您editingFinished()信号是Enter / Return键还是失去焦点的结果。

void Dialog::on_spinBox_editingFinished() {
    if(ui->spinBox->hasFocus()) 
        return;   

    // rest of your code
}

You could override keyPressEvent(QKeyEvent*) and ignore the event when enter is pressed. 您可以覆盖keyPressEvent(QKeyEvent*)并在按下Enter键时忽略该事件。 Another way to do it would be to override focusOutEvent(QFocusEvent*) but make sure that setFocusPolicy() is set to something else than Qt::NoFocus . 另一种方法是重写focusOutEvent(QFocusEvent*)但要确保setFocusPolicy()设置为Qt::NoFocus以外的其他Qt::NoFocus

Here's an example of the first method: You inherit from QSpinBox and override the keyPressEvent() method and make it ignore the enter/return key: 这是第一种方法的示例:您从QSpinBox继承并重写keyPressEvent()方法,并使其忽略Enter /返回键:

class SpinBox : public QSpinBox {
  Q_OBJECT

public:
  SpinBox(QWidget *parent = NULL) : QSpinBox(parent) { }

protected:
  void keyPressEvent(QKeyEvent *event) {
    switch (event->key()) {
    case Qt::Key_Return:
    case Qt::Key_Enter:
      return;

    default: break;
    }

    QSpinBox::keyPressEvent(event);    
  }
};

Now just use the editingFinished() signal which will only be given when the focus is lost (by using the mouse or tab key, for instance). 现在,只需使用editingFinished()信号,该信号仅在失去焦点时才会显示(例如,使用鼠标或Tab键)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM