Tried clearFocus but that didn't work. According to the docs
If the widget has active focus, a focus out event is sent to this widget to tell it that it is about to lose the focus.
Maybe "about to lose focus" is different from actually losing focus? I don't see how I can check setFocus without having a way to remove focus
For the record, I've tested my control in a test app to make sure it actually works, which it does. All I wish to do now is enshrine this behavior in a unit test.
Here's a more complete yet still simple example of what I'm after:
my_ctrl header
{
Q_OBJECT
public:
~my_ctrl();
protected:
};
class my_ctrl : public QLineEdit
{
Q_OBJECT
public:
my_ctrl(QWidget * parent = 0);
~my_ctrl();
protected:
virtual void focusInEvent(QFocusEvent * e);
virtual void focusOutEvent(QFocusEvent * e);
};
To copy to clipboard, switch view to plain text mode
my_ctrl implementation
{}
my_ctrl::~my_ctrl()
{}
{
setText("Focus In");
}
{
setText("Focus Out");
}
my_ctrl::my_ctrl(QWidget * parent)
: QLineEdit(parent)
{}
my_ctrl::~my_ctrl()
{}
void my_ctrl::focusInEvent(QFocusEvent * e)
{
setText("Focus In");
QLineEdit::focusInEvent(e);
}
void my_ctrl::focusOutEvent(QFocusEvent * e)
{
setText("Focus Out");
QLineEdit::focusOutEvent(e);
}
To copy to clipboard, switch view to plain text mode
test code
class test_my_ctrl
: public QObject{
Q_OBJECT
private slots:
void test_lose_focus();
};
void test_my_ctrl::test_lose_focus()
{
my_ctrl ctrl;
QTest::keyClicks(&ctrl, "1.23456789");
QCOMPARE
(ctrl.
text(),
QString("1.23456789"));
ctrl.clearFocus(); //doesn't work
QCOMPARE
(ctrl.
text(),
QString("Focus Out"));
//fails with Actual (ctrl.text()): 1.23456789
}
QTEST_MAIN(test_my_ctrl)
#include "main.moc"
class test_my_ctrl : public QObject
{
Q_OBJECT
private slots:
void test_lose_focus();
};
void test_my_ctrl::test_lose_focus()
{
my_ctrl ctrl;
QTest::keyClicks(&ctrl, "1.23456789");
QCOMPARE(ctrl.text(), QString("1.23456789"));
ctrl.clearFocus(); //doesn't work
QCOMPARE(ctrl.text(), QString("Focus Out")); //fails with Actual (ctrl.text()): 1.23456789
}
QTEST_MAIN(test_my_ctrl)
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks