下面是一个滑动条的例子:
#include
#include
#include
#include
#include
#include
#include
#include
class SpinSlider:public QWidget
{
public:
SpinSlider(QWidget *parent=0);
};
SpinSlider::SpinSlider(QWidget *parent):QWidget(parent)
{
QSpinBox *spinbox = new QSpinBox();
spinbox->setRange(0,100);
spinbox->setValue(0);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0,100);
slider->setValue(0);
connect(spinbox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
connect(slider,SIGNAL(valueChanged(int)),spinbox,SLOT(setValue(int)));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinbox);
layout->addWidget(slider);
setLayout(layout);
}
class MyWidget:public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent):QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit!"));
quit->setFont( QFont("Times",18,QFont::Bold) );
SpinSlider *spinslider = new SpinSlider;
connect(quit,SIGNAL(clicked()),qApp,SLOT(quit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(spinslider);
setLayout(layout);
setWindowTitle("Quit SpinBox Slider");
}
int main(int argc,char **argv)
{
QApplication app(argc,argv);
MyWidget widget;
widget.show();
return app.exec();
}
设计SpinSlider类封装了SpinBox和Slider就是输入框和滑动条,它们是安照横向排列;SpinSlider和quit进行纵向排列。
阅读(2746) | 评论(0) | 转发(2) |