1.使用事件过滤器:
通过鼠标位置获取鼠标滑过的QListWidget项:
this->setMouseTracking(true);
this->ui->reasonList->installEventFilter(this);
bool VisitReasonWindow::eventFilter(QObject *obj, QEvent *evn)
{
if(obj==this->ui->reasonList&&evn->type()==QEvent::HoverMove)
{
auto item=this->ui->reasonList->
itemAt(this->ui->reasonList->viewport()->mapFromGlobal(this->cursor().pos()));
if(item!=nullptr)
{
//超出10个字提示全部文字:
if(item->text().count()>10)
{
QToolTip::showText(this->cursor().pos(), item->text(), this);
}
else
{
QToolTip::showText(QPoint(10000,10000), item->text(), this);
}
}
}
return false;
}
2.void QListWidget::itemEntered(QListWidgetItem *item)
只有当鼠标左键处于按下状态,且滑过item时,才会触发此信号。
最终效果: