开发者

QPushButton gets stuck drawing pressed when transfer of focus to button is interrupted

开发者 https://www.devze.com 2023-02-05 20:37 出处:网络
I have some forms with input fields that provide warnings to the user when the fields looses focus (if something is wrong of course).To that end I’ve created my own control that inherits from QLineEd

I have some forms with input fields that provide warnings to the user when the fields looses focus (if something is wrong of course). To that end I’ve created my own control that inherits from QLineEdit. For this question consider the extremely simple field that will pop a message box whenever focus is lost when there is text in the control. The actual controls have more logic but all are supposed to pop a message box. (Crossposted to 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);
    }
  }

};

Now say I have a simple form with just one of these controls and a QPushButton. If the user types in the control and then clicks the button the messagebox pops but the button gets stuck being drawn looking like it’s pressed. How can I make that stop? As best I can tell the QPushButton’s pressed signal is getting fired but that is all I can hear from the button. Since these test_line_edit controls are going to be used everywhere on many different forms I’m hoping there is something I can change inside the test_line_edit class that will fix the problem. Running Qt 4.7.0 on Windows XP using Visual Studio 2010.

I’ve seen this bug and not sure if it’s related since it’s a different platform and I’m not actually holding the button down: http://bugreports.qt-project.org/browse/QTBUG-7901

Here’s a full program that demonstrates the problem (warning: some code generated from the Qt VS plugin)

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开发者_JS百科_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();
}

Thank you for your help and please let me know if I can provide any more information.


I've made some changes to your line edit class, see if it solves the problem:

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);
        }
    }
};

Instead of showing modal window in the focusOutEvent method, I've posted an event into the event queue. This worked fine on my ubuntu, don't expect any problems on windows either.

hope this helps, regards

0

精彩评论

暂无评论...
验证码 换一张
取 消