0

I have written a custom class for overriding the seek method of QSlider. Basically this class allows seeking on the slider where ever the user clicks .

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);
}

Signal and slots in main.cpp

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

The above code was supposed to work . Is there anything else I need to do ?

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
  • To make your component as compatible with `QSlider` as possible, use it's signals - there is no need for you to create a signal, use `valueChanged(int value)`. Which is already emitted by `setValue(int)`. – cmannett85 Jun 21 '12 at 09:34
  • I m emitting the signal to call public slot of another class. – Vihaan Verma Jun 21 '12 at 09:42

1 Answers1

0

The function should be

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);
}

Calling QSlider::mousePressEvent(event) in the beginning resulted in a return statement in the base class.

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126