0

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

  1. e->lostFocus() returns true for both gaining and losing focus.
  2. The slots that I connect to the doneEditing signal don't get called.
Community
  • 1
  • 1
AaronD
  • 503
  • 1
  • 7
  • 23
  • 1
    I think it might be because it's actually the QPlainTextEdit that gets the focus. You could create a derived class from QPlainTextEdit and override the focus changed event in that instead. – Stuart Fisher Jan 15 '16 at 11:24

1 Answers1

0

Okay, I got it now:

  1. Stuart's suggestion to subclass QPlainTextEdit and put the focusOutEvent() function in there got it called, but:
    • e->lostFocus() returned true for both gaining and losing focus.
    • The slots that I connected to that signal weren't called.
  2. I noticed when reviewing my connect() functions that Qt Creator's autocomplete converted the const QStringList& type into QStringList. Fixing that got the slots called.

And somehow, after some more unrelated rearranging, I'm only emitting the signal on focus lost, like it's supposed to, instead of both lost and gained. I have no idea how or when that happened.

AaronD
  • 503
  • 1
  • 7
  • 23