QSlider解决点击不能到该位置问题

方法一:可以继承重写一个QSlider

方法二:1.instaneventfiliter(this);slider安装一个事件过滤器

              2.在eventfilter(QObject *,QEvent*)中拦截它,很简单吧

 

例子:

	slider=new QSlider(this); 
	slider->installEventFilter(this);

bool trayIconMenu::eventFilter(QObject *obj, QEvent *event)
{
    if(obj==slider)
    {
        if (event->type()==QEvent::MouseButtonPress)           //判断类型
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)	//判断左键
            {
               int dur = slider->maximum() - slider->minimum();
               int pos = slider->minimum() + dur * ((double)mouseEvent->x() / slider->width());
               if(pos != slider->sliderPosition())
                {
                  slider->setValue(pos);
                }
            }

        }
    }
    return QObject::eventFilter(obj,event);
}
okay!即可以实现slider点哪里就到哪里

http://blog.csdn.net/what951006/article/details/51507801

posted @ 2016-06-12 21:40  findumars  Views(3076)  Comments(0Edit  收藏  举报