Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top