转载来自 https://blog.csdn.net/what951006/article/details/51507801
ui->horizontalSliderBrightness->installEventFilter(this);
bool QLedDisplayCommonTabWidget::eventFilter(QObject* watched, QEvent* event)
{
if(watched == ui->horizontalSliderBrightness || watched == ui->horizontalSliderTemperature)
{
if (event->type()==QEvent::MouseButtonPress) //判断类型
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::LeftButton) //判断左键
{
QSlider* pSlider = (QSlider*)watched;
int dur = pSlider->maximum() - pSlider->minimum();
int pos = pSlider->minimum() + dur * ((double)mouseEvent->x() / pSlider->width());
if(pos != pSlider->sliderPosition())
{
pSlider->setValue(pos);
}
}
}
}
return QObject::eventFilter(watched,event);
}