QScrollBar & QSlider & QDial

【1】滚动条 & 滑块 & 表盘

Qt示例工程:

(1)slidergroup.h

 1 #include <QGroupBox>
 2
 3 QT_BEGIN_NAMESPACE
 4 class QDial;
 5 class QScrollBar;
 6 class QSlider;
 7 QT_END_NAMESPACE
 8
 9 class SlidersGroup : public QGroupBox
10 {
11     Q_OBJECT
12
13 public:
14     SlidersGroup(Qt::Orientation orientation,
15                  const QString &title,
16                  QWidget *parent = 0);
17
18 signals:
19     void valueChanged(int value);
20
21 public slots:
22     void setValue(int value);
23     void setMinimum(int value);
24     void setMaximum(int value);
25     void invertAppearance(bool invert);
26     void invertKeyBindings(bool invert);
27
28 private:
29     QSlider *m_pSlider;
30     QScrollBar *m_pScrollBar;
31     QDial *m_pDial;
32 };
33
34 #endif

(2)slidergroup.cpp

 1 #include <QtWidgets>
 2 #include "SlidersGroup.h"
 3
 4 SlidersGroup::SlidersGroup(Qt::Orientation orientation,
 5                            const QString &title,
 6                            QWidget *parent)
 7     : QGroupBox(title, parent)
 8 {
 9     m_pSlider = new QSlider(orientation, this);
10     m_pSlider->setFocusPolicy(Qt::StrongFocus);
11     m_pSlider->setTickPosition(QSlider::TicksBothSides);
12     m_pSlider->setTickInterval(10);
13     m_pSlider->setSingleStep(1);
14
15     m_pScrollBar = new QScrollBar(orientation, this);
16     m_pScrollBar->setFocusPolicy(Qt::StrongFocus);
17
18     m_pDial = new QDial(this);
19     m_pDial->setFocusPolicy(Qt::StrongFocus);
20
21     connect(m_pSlider, &QSlider::valueChanged, m_pScrollBar, &QScrollBar::setValue);
22     connect(m_pScrollBar, &QScrollBar::valueChanged, m_pDial, &QDial::setValue);
23     connect(m_pDial, &QDial::valueChanged, m_pSlider, &QSlider::setValue);
24     connect(m_pDial, &QDial::valueChanged, this, &SlidersGroup::valueChanged);
25
26     QBoxLayout::Direction direction = (Qt::Horizontal == orientation) ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
27
28     QBoxLayout *m_pSlidersLayout = new QBoxLayout(direction, this);
29     m_pSlidersLayout->addWidget(m_pSlider);
30     m_pSlidersLayout->addWidget(m_pScrollBar);
31     m_pSlidersLayout->addWidget(m_pDial);
32     setLayout(m_pSlidersLayout);
33 }
34
35 void SlidersGroup::setValue(int value)
36 {
37     m_pSlider->setValue(value);
38 }
39
40 void SlidersGroup::setMinimum(int value)
41 {
42     m_pSlider->setMinimum(value);
43     m_pScrollBar->setMinimum(value);
44     m_pDial->setMinimum(value);
45 }
46
47 void SlidersGroup::setMaximum(int value)
48 {
49     m_pSlider->setMaximum(value);
50     m_pScrollBar->setMaximum(value);
51     m_pDial->setMaximum(value);
52 }
53
54 void SlidersGroup::invertAppearance(bool invert)
55 {
56     m_pSlider->setInvertedAppearance(invert);
57     m_pScrollBar->setInvertedAppearance(invert);
58     m_pDial->setInvertedAppearance(invert);
59 }
60
61 void SlidersGroup::invertKeyBindings(bool invert)
62 {
63     m_pSlider->setInvertedControls(invert);
64     m_pScrollBar->setInvertedControls(invert);
65     m_pDial->setInvertedControls(invert);
66 }

(3)window.h

 1 #ifndef WINDOW_H
 2 #define WINDOW_H
 3
 4 #include <QWidget>
 5
 6 QT_BEGIN_NAMESPACE
 7 class QCheckBox;
 8 class QComboBox;
 9 class QGroupBox;
10 class QLabel;
11 class QSpinBox;
12 class QStackedWidget;
13 QT_END_NAMESPACE
14 class SlidersGroup;
15
16 class Window : public QWidget
17 {
18     Q_OBJECT
19
20 public:
21     Window();
22
23 private:
24     QSpinBox *createSpinBox();
25     void createControls(const QString &title);
26
27 private:
28     SlidersGroup *m_pHorizontalSliders;
29     SlidersGroup *m_pVerticalSliders;
30     QStackedWidget *m_pStackedWidget;
31
32     QGroupBox *m_pControlsGroup;
33     QLabel *m_pMinimumLabel;
34     QLabel *m_pMaximumLabel;
35     QLabel *m_pValueLabel;
36     QCheckBox *m_pInvertedAppearance;
37     QCheckBox *m_pInvertedKeyBindings;
38     QSpinBox *m_pMinimumSpinBox;
39     QSpinBox *m_pMaximumSpinBox;
40     QSpinBox *m_pValueSpinBox;
41     QComboBox *m_pOrientationCombo;
42 };
43
44 #endif

(4)window.cpp

 1 #include <QtWidgets>
 2
 3 #include "slidersgroup.h"
 4 #include "window.h"
 5
 6 Window::Window()
 7 {
 8     m_pHorizontalSliders = new SlidersGroup(Qt::Horizontal, tr("Horizontal"), this);
 9     m_pVerticalSliders = new SlidersGroup(Qt::Vertical, tr("Vertical"), this);
10
11     m_pStackedWidget = new QStackedWidget(this);
12     m_pStackedWidget->addWidget(m_pHorizontalSliders);
13     m_pStackedWidget->addWidget(m_pVerticalSliders);
14
15     createControls(tr("Controls"));
16
17     connect(m_pHorizontalSliders, &SlidersGroup::valueChanged,
18             m_pVerticalSliders, &SlidersGroup::setValue);
19     connect(m_pVerticalSliders, &SlidersGroup::valueChanged,
20             m_pValueSpinBox, &QSpinBox::setValue);
21     typedef void (QSpinBox::*valueChangedInt)(int);
22     connect(m_pValueSpinBox, static_cast<valueChangedInt>(&QSpinBox::valueChanged),
23             m_pHorizontalSliders, &SlidersGroup::setValue);
24
25     QHBoxLayout *pHLayout = new QHBoxLayout(this);
26     pHLayout->addWidget(m_pControlsGroup);
27     pHLayout->addWidget(m_pStackedWidget);
28     setLayout(pHLayout);
29
30     m_pMinimumSpinBox->setValue(0);
31     m_pMaximumSpinBox->setValue(20);
32     m_pValueSpinBox->setValue(5);
33
34     setWindowTitle(tr("MySliders"));
35 }
36
37 void Window::createControls(const QString &title)
38 {
39     m_pControlsGroup = new QGroupBox(title, this);
40
41     m_pMinimumLabel = new QLabel(tr("Minimum value:"), this);
42     m_pMaximumLabel = new QLabel(tr("Maximum value:"), this);
43     m_pValueLabel = new QLabel(tr("Current value:"), this);
44
45     m_pInvertedAppearance = new QCheckBox(tr("Inverted appearance"), this);
46     m_pInvertedKeyBindings = new QCheckBox(tr("Inverted key bindings"), this);
47
48     m_pMinimumSpinBox = createSpinBox();
49     m_pMaximumSpinBox = createSpinBox();
50     m_pValueSpinBox = createSpinBox();
51
52     m_pOrientationCombo = new QComboBox(this);
53     m_pOrientationCombo->addItem(tr("Horizontal slider-like widgets"));
54     m_pOrientationCombo->addItem(tr("Vertical slider-like widgets"));
55
56     typedef void (QComboBox::*activatedInt)(int);
57     connect(m_pOrientationCombo, static_cast<activatedInt>(&QComboBox::activated),
58             m_pStackedWidget, &QStackedWidget::setCurrentIndex);
59     typedef void (QSpinBox::*valueChangedInt)(int);
60     connect(m_pMinimumSpinBox, static_cast<valueChangedInt>(&QSpinBox::valueChanged),
61             m_pHorizontalSliders, &SlidersGroup::setMinimum);
62     connect(m_pMinimumSpinBox, static_cast<valueChangedInt>(&QSpinBox::valueChanged),
63             m_pVerticalSliders, &SlidersGroup::setMinimum);
64     connect(m_pMaximumSpinBox, static_cast<valueChangedInt>(&QSpinBox::valueChanged),
65             m_pHorizontalSliders, &SlidersGroup::setMaximum);
66     connect(m_pMaximumSpinBox, static_cast<valueChangedInt>(&QSpinBox::valueChanged),
67             m_pVerticalSliders, &SlidersGroup::setMaximum);
68     connect(m_pInvertedAppearance, &QCheckBox::toggled,
69             m_pHorizontalSliders, &SlidersGroup::invertAppearance);
70     connect(m_pInvertedAppearance, &QCheckBox::toggled,
71             m_pVerticalSliders, &SlidersGroup::invertAppearance);
72     connect(m_pInvertedKeyBindings, &QCheckBox::toggled,
73             m_pHorizontalSliders, &SlidersGroup::invertKeyBindings);
74     connect(m_pInvertedKeyBindings, &QCheckBox::toggled,
75             m_pVerticalSliders, &SlidersGroup::invertKeyBindings);
76
77     QGridLayout *pControlsLayout = new QGridLayout(this);
78     pControlsLayout->addWidget(m_pMinimumLabel, 0, 0);
79     pControlsLayout->addWidget(m_pMaximumLabel, 1, 0);
80     pControlsLayout->addWidget(m_pValueLabel, 2, 0);
81     pControlsLayout->addWidget(m_pMinimumSpinBox, 0, 1);
82     pControlsLayout->addWidget(m_pMaximumSpinBox, 1, 1);
83     pControlsLayout->addWidget(m_pValueSpinBox, 2, 1);
84     pControlsLayout->addWidget(m_pInvertedAppearance, 0, 2);
85     pControlsLayout->addWidget(m_pInvertedKeyBindings, 1, 2);
86     pControlsLayout->addWidget(m_pOrientationCombo, 3, 0, 1, 3);
87     m_pControlsGroup->setLayout(pControlsLayout);
88 }
89
90 QSpinBox* Window::createSpinBox()
91 {
92     QSpinBox *pValueSpinBox = new QSpinBox(this);
93     pValueSpinBox->setRange(-100, 100);
94     pValueSpinBox->setSingleStep(1);
95     return pValueSpinBox;
96 }

(5)main.cpp

 1 #include <QApplication>
 2 #include "window.h"
 3
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication app(argc, argv);
 7     Window window;
 8     window.show();
 9     return app.exec();
10 }

注意:代码已很简洁,没有再加注释。

【2】运行效果

水平滚动条:

垂直滚动条:

Good Good Study, Day Day Up.

顺序 选择 循环 总结

时间: 2024-10-20 03:39:45

QScrollBar & QSlider & QDial的相关文章

Qslider

滑块部件:QAbstractSlider      它可以由滑块定位来确定一个区间内的任意值,这个抽象基类有2个子类,QScrollBar,QSlider,Qdial, ScrollBar用在ScrollArea中来实现滚动区域,QSlider用来实现音量控制或者播放器的进度,QDial是一个表盘.QScrollBar:    该组件提供垂直或者水平方向的滑动条.    构造函数:QScrollBar ( Qt::Orientation orientation, QWidget * parent

Qt4与Qt3的主要不同

1)QT4 中提供了大量新控件,虽然它也保持了旧的控件,并命名为Qt3XXX,但是这样的控件没准在今后的哪个QT版本中就不被支持了,所以还是换吧,控件替换的 工作是巨大的,这些新的控件使用了新的方法.属性.和事件名称,虽然是比以前更规范了,但是对于升级旧代码来说,则增加了大量工作. 2)既然要更新控件,那么你就需要使用QDesigner工具来重新画界面了,OK,重画只是纯体力劳动,好说.界 面画好后,使用uic来根据界面生成代码文件,QT3的uic可以自动生成.h和.cpp文件,假设你在制作一个

PyQt5 控件学习(一个一个学习之QScrollBar)

QScrollBar 的继承图: QScrollBar 的描述: QScrollBar 的继承: 它继承与QAbstractSlider QScrollBar 的功能作用: from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QScrollBar的学习") self.resiz

QSlider滑块类和QSpinBox微调框类

QSlider   滑块类         信号函数  void  valueChanged(int val); QSpinBox  微调框类         槽函数    void  setValue(int val);         槽函数就是一个带有特殊修饰的成员函数 ---------------------------------------------------------------        连接带参的信号和槽时  连接函数中 要出现信号和槽参数的类型.

QSlider解决点击不能到该位置问题

方法一:可以继承重写一个QSlider 方法二:1.instaneventfiliter(this);slider安装一个事件过滤器 2.在eventfilter(QObject *,QEvent*)中拦截它,很简单吧 例子: slider=new QSlider(this); slider->installEventFilter(this); bool trayIconMenu::eventFilter(QObject *obj, QEvent *event) { if(obj==slider

Qt自定义带游标的slider,在滑块正上方显示当前值(非常有意思,继承QSlider之后增加一个QLabel,然后不断移动它)

首先自定义QSlider的子类MyCustomSlider,如下所示. mycustomslider.h [cpp] view plain copy #ifndef MYCUSTOMSLIDER_H #define MYCUSTOMSLIDER_H #include <QSlider> #include <QLabel> #include <QMouseEvent> class MyCustomSlider : public QSlider { public: MyCu

QSpinBox 和 QSlider 联合使用方法

在Qt中,有时候我们想要联合QSpinBox 和 QSlider,使得移动滑块,QSpinBox中的数据会变化,或者我们在QSpinBox中输入一个数值,响应的滑块也会变化,如下图所示: 那么我们可以用如下的代码来实现: ui.setupUi(this); ui.spinBox->setRange(0, 100); ui.slider->setRange(0, 100); connect(ui.spinBox,SIGNAL(valueChanged(int)), ui.slider,SLOT(

QtGui.QSlider

A QtGui.QSlider is a widget that has a simple handle. This handle can be pulled back and forth. This way we are choosing a value for a specific task. Sometimes using a slider is more natural than entering a number or using a spin box. In our example

Qt之QSlider

简述 QSlider部件提供了一个垂直或水平滑动条. 滑块是一个用于控制有界值的典型部件.它允许用户沿水平或垂直方向移动滑块,并将滑块所在的位置转换成一个合法范围内的值. 简述 详细描述 刻度位置 基本使用 效果 源码 QSS 刻度位置及间隔 效果 源码 详细描述 QSlider很少有自己的函数,大部分功能在QAbstractSlider中.最有用的函数是setValue(),用来设置滑块的当前值:triggerAction()来模拟点击的效果(对快捷键有用),setSingleStep().s