0

我编写了一个自定义类来覆盖 QSlider 的 seek 方法。基本上,此类允许在用户单击的滑块上进行搜索。

tbslider.h

#ifndef TBSLIDER_H
#define TBSLIDER_H

#include <QSlider>
#include <QObject>

class Tbslider:  public QSlider
{
    Q_OBJECT
public:
    explicit Tbslider(QWidget *parent=0);

signals:
    void tbJump(int);
protected:
    void mousePressEvent(QMouseEvent *event);
private:
    QSlider *qslider;
};
#endif // TBSLIDER_H

tbslider.cpp

#include <iostream>
#include <QEvent>
#include <QPoint>
#include <QMouseEvent>
#include "tbslider.h"

Tbslider::Tbslider(QWidget *parent):QSlider(parent)
{

}
void Tbslider::mousePressEvent(QMouseEvent *event)
{
QSlider::mousePressEvent(event);

    std::cout<<"\nX co-ordinate"<<event->x();
    std::cout<<"\nY co-ordinate"<<event->y();
    int value =(minimum() + ((maximum()-minimum()) * event->x()) / width() ) ;
    setValue(value);
    emit tbJump(value);
}

main.cpp 中的信号和槽

  QApplication app(argc, argv);
  MainWindow player;
  Tbslider tbslider;
  GSTEngine listen
  app.connect(&tbslider, SIGNAL(tbJump(int)), &listen, SLOT(jump(int)));

上面的代码应该可以工作。还有什么我需要做的吗?

4

1 回答 1

0

函数应该是

void Tbslider::mousePressEvent(QMouseEvent *event)
{


    std::cout<<"\nX co-ordinate"<<event->x();
    std::cout<<"\nY co-ordinate"<<event->y();
    int value =(minimum() + ((maximum()-minimum()) * event->x()) / width() ) ;
    setValue(value);
    emit tbJump(value); 
    QSlider::mousePressEvent(event);
}

在开始时调用 QSlider::mousePressEvent(event) 会导致基类中的 return 语句。

于 2012-06-21T10:03:20.320 回答