First, customize the subclass of the Qslider Mycustomslider as shown below.
Mycustomslider.h
[CPP]View PlainCopy
- #ifndef Mycustomslider_h
- #define Mycustomslider_h
- #include <QSlider>
- #include <QLabel>
- #include <QMouseEvent>
- Class Mycustomslider: Public qslider
- {
- Public
- Mycustomslider (Qwidget *parent=0);
- ~mycustomslider ();
- Protected
- virtual void Mousepressevent (Qmouseevent *event);
- virtual void Mousereleaseevent (Qmouseevent *event);
- virtual void Mousemoveevent (Qmouseevent *event);
- Private
- qlabel* M_displaylabel;
- };
- #endif//Mycustomslider_h
Mycustomslider.cpp
[CPP]View PlainCopy
- #include "Mycustomslider.h"
- #include <QPalette>
- Mycustomslider::mycustomslider (Qwidget *parent): Qslider (parent)
- {
- m_displaylabel=New Qlabel (this);
- M_displaylabel->setfixedsize (Qsize (20,20));
- //Set cursor background to white
- M_displaylabel->setautofillbackground (true);
- Qpalette Palette;
- Palette.setcolor (Qpalette::background, qt::white);
- M_displaylabel->setpalette (palette);
- M_displaylabel->setalignment (Qt::aligncenter);
- M_displaylabel->setvisible (false);
- M_displaylabel->move (0,3);
- }
- Mycustomslider::~mycustomslider ()
- {
- }
- void Mycustomslider::mousepressevent (Qmouseevent *event)
- {
- if (!m_displaylabel->isvisible ())
- {
- M_displaylabel->setvisible (true);
- M_displaylabel->settext (Qstring::number (this->value ()));
- }
- Qslider::mousepressevent (event);
- }
- void Mycustomslider::mousereleaseevent (Qmouseevent *event)
- {
- if (m_displaylabel->isvisible ())
- {
- M_displaylabel->setvisible (false);
- }
- Qslider::mousereleaseevent (event);
- }
- void Mycustomslider::mousemoveevent (Qmouseevent *event)
- {
- M_displaylabel->settext (Qstring::number (this->value ()));
- M_displaylabel->move ((this->width ()-m_displaylabel->width ()) *This->value ()/(this-> Maximum ()-this->minimum ()), 3);
- Qslider::mousemoveevent (event);
- }
The interface Qslider control is then promoted to Mycustomslider, as shown in the elevation method.
Elevation of control classes in QT
As shown in the final effect, when the mouse clicks the slider or drags the slider, the cursor directly above the slider displays the current value in real time, and when the mouse is released, the cursor is hidden.
SOURCE Link: See http://blog.csdn.net/caoshangpa/article/details/51973902
SOURCE Link: http://download.csdn.net/detail/caoshangpa/9581687
QT Custom slider with cursors, display the current value above the slider (very interesting, add a qlabel after inheriting Qslider, and then move it constantly)