QScrollBar用於顯示控件上不能徹底顯示的區域,與Slider相似,可是QScrollBar控件可在底部或者右側使用。能夠將其移動到特定區域ide
一、新建QTGUI應用,基類選擇QWidget,取消建立界面this
二、widget.h中:3d
#ifndef WIDGET_H #define WIDGET_H #include <QScrollBar> #include <QLabel> #include <QAbstractSlider> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QScrollBar *vscrollbar[3]; QScrollBar *hscrollbar[3]; QLabel *lbl[3]; private slots: void valueChanged1(int value); void valueChanged2(int value); void valueChanged3(int value); }; #endif // WIDGET_H
widget.cpp中code
#include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { int xpos = 10; int ypos = 50; for(int i = 0 ; i < 3 ; i++) { vscrollbar[i] = new QScrollBar(Qt::Vertical, this); vscrollbar[i]->setRange(0, 100); vscrollbar[i]->setGeometry(xpos, 30, 20, 200); lbl[i] = new QLabel(QString("%1").arg(vscrollbar[i]->value()), this); lbl[i]->setGeometry(xpos + 2, 220, 30, 30); xpos += 50; hscrollbar[i] = new QScrollBar(Qt::Horizontal, this); hscrollbar[i]->setRange(0, 100); hscrollbar[i]->setGeometry(150, ypos, 200, 20); ypos += 30; } connect(vscrollbar[0], SIGNAL(valueChanged(int)), this, SLOT(valueChanged1(int))); connect(vscrollbar[1], SIGNAL(valueChanged(int)), this, SLOT(valueChanged2(int))); connect(vscrollbar[2], SIGNAL(valueChanged(int)), this, SLOT(valueChanged3(int))); } void Widget::valueChanged1(int value) { lbl[0]->setText(QString("%1").arg(value)); hscrollbar[0]->setValue(vscrollbar[0]->value()); } void Widget::valueChanged2(int value) { lbl[1]->setText(QString("%1").arg(value)); hscrollbar[1]->setValue(vscrollbar[1]->value()); } void Widget::valueChanged3(int value) { lbl[2]->setText(QString("%1").arg(value)); hscrollbar[2]->setValue(vscrollbar[2]->value()); } Widget::~Widget() { }