QTextEdit控件使用
QTextEdit *mpContentTextEdit = new QTextEdit(this); //设置占位符文本
mpContentTextEdit->setPlaceholderText(tr("Placeholder text")); //设置关联控件,用于QTextEdit控件捕获MouseButtonPress等事件
QWidget *mpContentWidget = mpContentTextEdit->viewport(); //设置事件过滤器
mpContentTextEdit->installEventFilter(this);
mpContentWidget->installEventFilter(this); //QTextEdit处理占位符及事件
bool eventFilter(QObject *obj, QEvent *pEvent)
{
if (obj == mpContentTextEdit)
{
if (pEvent->type() == QEvent::FocusIn)
{
//清空编辑框默认占位符
mpContentTextEdit->setPlaceholderText("");
}
else if (pEvent->type() == QEvent::FocusOut)
{
//设置编辑框默认占位符
mpContentTextEdit->setPlaceholderText(tr("Placeholder text"));
}
} if ((((QKeyEvent *)pEvent)->key() == Qt::Key_Tab || pEvent->type() == QEvent::MouseButtonPress)
&& mpContentWidget->hasFocus())
{
//通过关联控件,可间接拦截编辑框鼠标等事件操作
mpContentTextEdit->setText("");
} return QDialog::eventFilter(obj, pEvent);
}