At first, it may seem to be a duplicate of this question, but the solution doesn't work for me. It looks like I'm doing everything right, but I'm not hitting the breakpoint at the start of LabelledTextEdit::focusOutEvent():
class LabelledTextEdit : public QWidget
{
Q_OBJECT
public:
explicit LabelledTextEdit(QString label, int labelheight, int left, int top, int width, int height, QWidget* parent = 0);
const QStringList getLines() const;
QLabel* label;
QPlainTextEdit* text;
protected:
void focusOutEvent(QFocusEvent* e) override;
signals:
void doneEditing(const QStringList& lines);
};
LabelledTextEdit::LabelledTextEdit(QString labeltext, int labelheight, int left, int top, int width, int height, QWidget* parent) :
QWidget(parent)
{
setGeometry(left, top, width, height);
setFocusPolicy(Qt::StrongFocus);
label = new QLabel(labeltext, this);
//continue setting up label
label->setGeometry(
0, //Left
0, //Top
width, //Width
labelheight //Height
);
text = new QPlainTextEdit(this);
//continue setting up text
text->setGeometry(
0, //Left
labelheight, //Top
width, //Width
height - labelheight //Height
);
}
const QStringList LabelledTextEdit::getLines() const
{
return text->toPlainText().split('\n', QString::SplitBehavior::KeepEmptyParts);
}
void LabelledTextEdit::focusOutEvent(QFocusEvent* e)
{
QWidget::focusOutEvent(e); //breakpoint here is not hit
if(e->lostFocus())
{
emit doneEditing(getLines());
}
}
What am I doing wrong?
Update:
Thanks Stuart for the suggestion to subclass QPlainTextEdit and put the focusOutEvent() function in there. That gets called, but now I see that
- e->lostFocus() returns true for both gaining and losing focus.
- The slots that I connect to the doneEditing signal don't get called.