QScrollBar & QSlider & QDial
Posted kaizen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
顺序 选择 循环 总结
以上是关于QScrollBar & QSlider & QDial的主要内容,如果未能解决你的问题,请参考以下文章