当焦点转移到按钮中断时,QPushButton被卡住了

时间:2011-01-21 20:46:50

标签: c++ qt qt4

我有一些带有输入字段的表单,当字段失去焦点时会向用户发出警告(当然,如果出现问题)。为此,我创建了自己的控件,继承自QLineEdit。对于这个问题,考虑一个极其简单的字段,当控件中有文本时,只要焦点丢失,它就会弹出一个消息框。实际的控件有更多的逻辑,但都应该弹出一个消息框。 (转到qtcentre.org)

class test_line_edit : public QLineEdit
{
  Q_OBJECT

public:
  test_line_edit(QWidget * parent = 0) : QLineEdit(parent)
  {}

  ~test_line_edit(){}

protected:  
  virtual void focusOutEvent(QFocusEvent * e)
  {
    if(!text().isEmpty())
    {
      QMessageBox msg;
      msg.setText(tr("Test"));
      msg.exec();
      setFocus(Qt::OtherFocusReason);
    }
    else
    {
      QLineEdit::focusOutEvent(e);
    }
  }

};

现在说我有一个简单的表单,其中只有一个控件和一个QPushButton。如果用户键入控件然后单击按钮,则消息框会弹出,但按钮会被拉出,看起来就像按下了一样。我怎么能停下来?最好的我可以告诉QPushButton的按下信号被解雇,但这是我能从按钮听到的。由于这些test_line_edit控件将在许多不同形式的任何地方使用,我希望我可以在test_line_edit类中更改一些可以解决问题的东西。使用Visual Studio 2010在Windows XP上运行Qt 4.7.0。

我已经看到了这个错误并且不确定它是否相关,因为它是一个不同的平台而我实际上并没有按下按钮:http://bugreports.qt-project.org/browse/QTBUG-7901

这是一个演示问题的完整程序(警告:从Qt VS插件生成的一些代码)

main.h:

#include <QtGui/QMainWindow>

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

#include <QLineEdit>
#include <QMessageBox>

class test_line_edit : public QLineEdit
{
  Q_OBJECT

public:
  test_line_edit(QWidget * parent = 0) : QLineEdit(parent)
  {}

  ~test_line_edit(){}

protected:  
  virtual void focusOutEvent(QFocusEvent * e)
  {
    if(!text().isEmpty())
    {
      QMessageBox msg;
      msg.setText(tr("Test"));
      msg.exec();
      //setFocus(Qt::OtherFocusReason);
    }
    else
    {
      QLineEdit::focusOutEvent(e);
    }

    QLineEdit::focusOutEvent(e);
  }

};

QT_BEGIN_NAMESPACE

class Ui_btn_drawing_testClass
{
public:
    QWidget *centralWidget;
    QVBoxLayout *verticalLayout;
    test_line_edit *line_edit;
    QPushButton *btn;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *btn_drawing_testClass)
    {
        if (btn_drawing_testClass->objectName().isEmpty())
            btn_drawing_testClass->setObjectName(QString::fromUtf8("btn_drawing_testClass"));
        btn_drawing_testClass->resize(167, 127);
        centralWidget = new QWidget(btn_drawing_testClass);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        verticalLayout = new QVBoxLayout(centralWidget);
        verticalLayout->setSpacing(6);
        verticalLayout->setContentsMargins(11, 11, 11, 11);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        line_edit = new test_line_edit(centralWidget);
        line_edit->setObjectName(QString::fromUtf8("line_edit"));

        verticalLayout->addWidget(line_edit);

        btn = new QPushButton(centralWidget);
        btn->setObjectName(QString::fromUtf8("btn"));

        verticalLayout->addWidget(btn);

        btn_drawing_testClass->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(btn_drawing_testClass);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 167, 20));
        btn_drawing_testClass->setMenuBar(menuBar);
        mainToolBar = new QToolBar(btn_drawing_testClass);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        btn_drawing_testClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(btn_drawing_testClass);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        btn_drawing_testClass->setStatusBar(statusBar);

        retranslateUi(btn_drawing_testClass);

        QMetaObject::connectSlotsByName(btn_drawing_testClass);
    } // setupUi

    void retranslateUi(QMainWindow *btn_drawing_testClass)
    {
        btn_drawing_testClass->setWindowTitle(QApplication::translate("btn_drawing_testClass", "btn_drawing_test", 0, QApplication::UnicodeUTF8));
        btn->setText(QApplication::translate("btn_drawing_testClass", "PushButton", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class btn_drawing_testClass: public Ui_btn_drawing_testClass {};
} // namespace Ui

QT_END_NAMESPACE

class btn_drawing_test : public QMainWindow
{
    Q_OBJECT

public:
  btn_drawing_test(QWidget *parent = 0, Qt::WFlags flags = 0): QMainWindow(parent, flags)
  {
    ui.setupUi(this);
  }
  ~btn_drawing_test(){}

private:
    Ui::btn_drawing_testClass ui;

protected slots:
  void on_btn_clicked()
  {
    int breakpoint = 5;
  }
  void on_btn_pressed()
  {
    int breakpoint = 5;
  }    
  void on_btn_toggled(bool)
  {
    int breakpoint = 5;
  }
};

main.cpp中:

#include <QtGui/QApplication>
#include "main.h"



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    btn_drawing_test w;
    w.show();
    return a.exec();
}

感谢您的帮助,如果我能提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

我对你的行编辑课做了一些修改,看看它是否能解决问题:

class test_line_edit : public QLineEdit
{
    Q_OBJECT

public:
    test_line_edit(QWidget * parent = 0) : QLineEdit(parent) {}
    ~test_line_edit(){}

protected:
    virtual void focusOutEvent(QFocusEvent * e)
    {
        QLineEdit::focusOutEvent(e);
        QEvent *event = new QEvent(QEvent::User);
        QApplication::postEvent(this, event);
    }

    virtual void customEvent(QEvent * event)
    {
        QLineEdit::customEvent(event);
        if (event->type()==QEvent::User)
        {
            QMessageBox msg;
            msg.setText(tr("Test"));
            msg.exec();
            setFocus(Qt::OtherFocusReason);
        }
    }
};

我没有在focusOutEvent方法中显示模态窗口,而是posted an event进入事件队列。这在我的ubuntu上工作正常,不要指望在Windows上有任何问题。

希望这有帮助,尊重