Qt自定义带游标的slider,在滑块正上方显示当前值
首先自定义QSlider的子类MyCustomSlider,如下所示。
mycustomslider.h
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
include "mycustomslider.h"
include “QPalette”
MyCustomSlider::MyCustomSlider(QWidget *parent):QSlider(parent)
{
m_displayLabel=new QLabel(this);
m_displayLabel->setFixedSize(QSize(20,20));
//设置游标背景为白色
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);
}
- 分享
- 举报
-
浏览量:3785次2020-09-20 21:19:24
-
浏览量:15168次2020-11-12 21:55:56
-
浏览量:5822次2020-09-23 23:07:37
-
浏览量:2963次2020-05-06 15:52:54
-
浏览量:2781次2020-08-14 18:40:18
-
浏览量:2257次2020-08-20 11:19:28
-
浏览量:2183次2020-08-03 12:02:37
-
浏览量:4807次2021-09-08 16:03:36
-
浏览量:1825次2020-08-03 12:01:28
-
浏览量:8121次2020-12-12 17:55:00
-
浏览量:8363次2020-12-12 17:47:04
-
浏览量:2506次2024-02-28 15:36:09
-
浏览量:9689次2020-08-19 22:41:20
-
浏览量:33919次2021-03-03 17:25:19
-
浏览量:4857次2021-06-28 15:59:34
-
浏览量:2247次2020-08-14 18:33:44
-
浏览量:4381次2021-09-13 13:47:51
-
浏览量:2041次2020-08-03 13:33:48
-
浏览量:13977次2021-08-13 16:08:47
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
big_anana
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明