如何仅在某些情况下有选择地发出 QTextTexid::textChanged 信号?
How to emit QTextTexid::textChanged signal only in certain cases selectively?
能否仅在某些情况下触发 textChanged 信号?例如触发插入文本,插入 space 个字符但不返回 space?
我 运行 每次文本更改时都会检查 QTextEdit 中的键入字符,并根据结果突出显示背景中只读 QTextEdit 中的文本,用作永远在线的占位符文本用户在打字时查看。如果用户犯了错误,角色会以红色突出显示,并在错误修复后重置为初始背景颜色。按下后退 space 键时会出现问题,因为它被记录为错误,因此前一个字符也突出显示为红色。
void Widget::onTextChanged()
{
QChar c;
QString txt_contents = txtedit_->toPlainText();
if(txt_contents.isEmpty()){
c = '[=11=]';
//reset text display
txtdisplay_->clear();
txtdisplay_->append(*label_text_);
}
else
c = txtedit_->toPlainText().back();
if(!texteditq_->isEmpty()){
if(c == texteditq_->head()){
//refresh text display
correct_++;
txtdisplay_->clear();
txtdisplay_->append(*label_text_);
//remove character that was used for the successful check from the
//queue
texteditq_->dequeue();
}else{
//set backgroud color to red for errors
fmt_->setBackground(Qt::red);
if(!txtedit_->toPlainText().isEmpty()){
//move cursor to the end of the editor, where the error is and save
//the position the error occurs at
c_edit_->movePosition(QTextCursor::End);
quint32 error_pos = c_edit_->position();
//move the cursor in the display for the background text and
//use the KeepAnchor move mode to highlight the misspelled char
c_display_->setPosition(error_pos-1,QTextCursor::MoveAnchor);
c_display_->setPosition(error_pos,QTextCursor::KeepAnchor);
//apply formating to that character
c_display_->setCharFormat(*fmt_);
}
}
}
}
根据 OP 的要求,我发布了一个名为 CustomTextEdit
的 class 解决方案,它是 subclassing QTextEdit
。它挂接到 keyPressEvent()
并检查按下的键。如果它不是退格键,那么自定义信号 keyPressed()
将被发射。
class CustomTextEdit : public QTextEdit {
Q_OBJECT
public:
explicit CustomTextEdit(QWidget *parent = nullptr) : QTextEdit(parent) {}
signals:
void keyPressed();
protected:
void keyPressEvent(QKeyEvent *e) override {
if (e->key() != Qt::Key_Backspace) {
emit keyPressed();
}
QTextEdit::keyPressEvent(e);
}
};
能否仅在某些情况下触发 textChanged 信号?例如触发插入文本,插入 space 个字符但不返回 space?
我 运行 每次文本更改时都会检查 QTextEdit 中的键入字符,并根据结果突出显示背景中只读 QTextEdit 中的文本,用作永远在线的占位符文本用户在打字时查看。如果用户犯了错误,角色会以红色突出显示,并在错误修复后重置为初始背景颜色。按下后退 space 键时会出现问题,因为它被记录为错误,因此前一个字符也突出显示为红色。
void Widget::onTextChanged()
{
QChar c;
QString txt_contents = txtedit_->toPlainText();
if(txt_contents.isEmpty()){
c = '[=11=]';
//reset text display
txtdisplay_->clear();
txtdisplay_->append(*label_text_);
}
else
c = txtedit_->toPlainText().back();
if(!texteditq_->isEmpty()){
if(c == texteditq_->head()){
//refresh text display
correct_++;
txtdisplay_->clear();
txtdisplay_->append(*label_text_);
//remove character that was used for the successful check from the
//queue
texteditq_->dequeue();
}else{
//set backgroud color to red for errors
fmt_->setBackground(Qt::red);
if(!txtedit_->toPlainText().isEmpty()){
//move cursor to the end of the editor, where the error is and save
//the position the error occurs at
c_edit_->movePosition(QTextCursor::End);
quint32 error_pos = c_edit_->position();
//move the cursor in the display for the background text and
//use the KeepAnchor move mode to highlight the misspelled char
c_display_->setPosition(error_pos-1,QTextCursor::MoveAnchor);
c_display_->setPosition(error_pos,QTextCursor::KeepAnchor);
//apply formating to that character
c_display_->setCharFormat(*fmt_);
}
}
}
}
根据 OP 的要求,我发布了一个名为 CustomTextEdit
的 class 解决方案,它是 subclassing QTextEdit
。它挂接到 keyPressEvent()
并检查按下的键。如果它不是退格键,那么自定义信号 keyPressed()
将被发射。
class CustomTextEdit : public QTextEdit {
Q_OBJECT
public:
explicit CustomTextEdit(QWidget *parent = nullptr) : QTextEdit(parent) {}
signals:
void keyPressed();
protected:
void keyPressEvent(QKeyEvent *e) override {
if (e->key() != Qt::Key_Backspace) {
emit keyPressed();
}
QTextEdit::keyPressEvent(e);
}
};