QPushButton gets stuck drawing pressed when transfer of focus to button interrupted
Hello
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.
Code:
{
Q_OBJECT
public:
{}
~test_line_edit(){}
protected:
{
if(!text().isEmpty())
{
msg.setText(tr("Test"));
msg.exec();
setFocus(Qt::OtherFocusReason);
}
else
{
}
}
};
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 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.nokia.com/browse/QTBUG-7901
Here’s a full program that demonstrates the problem (warning: some code generated from the VS plugin)
main.h:
Code:
#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>
{
Q_OBJECT
public:
{}
~test_line_edit(){}
protected:
{
if(!text().isEmpty())
{
msg.setText(tr("Test"));
msg.exec();
//setFocus(Qt::OtherFocusReason);
}
else
{
}
}
};
QT_BEGIN_NAMESPACE
class Ui_btn_drawing_testClass
{
public:
test_line_edit *line_edit;
{
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->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
->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
{
btn_drawing_testClass
->setWindowTitle
(QApplication::translate("btn_drawing_testClass",
"btn_drawing_test",
0,
QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class btn_drawing_testClass: public Ui_btn_drawing_testClass {};
} // namespace Ui
QT_END_NAMESPACE
{
Q_OBJECT
public:
{
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:
Code:
#include <QtGui/QApplication>
#include "main.h"
int main(int argc, char *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.
Re: QPushButton gets stuck drawing pressed when transfer of focus to button interrupt
Quote:
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?
The reason for this is that you are calling you message box in a modal state with exec().
This causes the applications even loop to wait for the message box to be dismissed, and therfore, the paintEvent for the pushbuttons is not being handled until the message box is not dismissed.
What you can do is:
1. Rethink your design. If the user is shifting input focus from a control, when the application logic states that this should not be allowed, its better to force the focus to stay on the focused control than to send a message box.
You could in addition to forcing the focus, have somewhere a message saying why it is being done, similar to what many applications do in the status bar.
2. Show the message box with show() and not exec(), but then it wont be modal, can be covered by other windows, and can bring to that that it will be immediately covered by another window so that the user not even notice it was popped.
Re: QPushButton gets stuck drawing pressed when transfer of focus to button interrupt
Got it answered here without having to resort to either option (unless something comes up in the future but seems to work so far): http://stackoverflow.com/questions/4...tton-is-interr
Re: QPushButton gets stuck drawing pressed when transfer of focus to button interrupt
if it works for you - Great!
Re: QPushButton gets stuck drawing pressed when transfer of focus to button interrupt
Well we haven’t made all the dialogs yet nor run through all the testing so something could come up. I’m still fairly new to Qt but the only potential problem I see is something getting between the creation of a user QEvent in test_line_edit::focusOutEvent and it getting processed in test_line_edit::customEvent. Do you know if that’s possible? Seems like it might be but I don’t really know under what conditions… Also are there any other obvious problems with this approach that I might be missing?
Re: QPushButton gets stuck drawing pressed when transfer of focus to button interrupt
Well, again, you can bend anything and force anything.
But I still think that the problem is a design problem, not an implementation problem.
You can work around design problem by forcing stuff, but then you are on slippery road of surprises.