Qt自带的滑动条QSlider和Android中自带的滑动条控件相比实在是具有天壤之别,此处使用eventFilter事件过滤器来完成一个鼠标点击滑动条滑块定位的细节功能。
代码段:
bool Device::eventFilter(QObject *obj, QEvent *event) //slider滑块根据鼠标位置
{
if(obj==pSlider_l)
{
if (event->type()==QEvent::MouseButtonPress) //判断类型
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::LeftButton) //判断左键
{
int dur = pSlider_l->maximum() - pSlider_l->minimum();
int pos = pSlider_l->minimum() + dur * ((double)mouseEvent->x() / pSlider_l->width());
if(pos < (pSlider_l->sliderPosition())||pos > (pSlider_l->sliderPosition()))
{
pSlider_l->setValue(pos);
}
}
}
}
return QObject::eventFilter(obj,event);
}