起初,它似乎与this question重复,但解决方案并不适用于我。看起来我做的一切都很正确,但我没有在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());
}
}
我做错了什么?
感谢Stuart建议继承QPlainTextEdit并将focusOutEvent()函数放在那里。这被调用,但现在我看到了
答案 0 :(得分:0)
好的,我现在明白了:
QPlainTextEdit
并将focusOutEvent()
函数放在那里调用它,但是:
e->lostFocus()
为获得和失去焦点而返回true。connect()
功能时,Qt Creator的自动完成功能将const QStringList&
类型转换为QStringList
。修复了这个叫做插槽的问题。并且不知何故,经过一些更无关的重新排列后,我只会发出焦点丢失的信号,就像它应该的那样,而不是丢失和获得的信号。我不知道发生的时间和时间。