概述: 把多个控件组合成一个自定义控件,方便使用.
核心步骤:
1.新建QT设计师界面类,继承Widget.
2.在界面托出一个Widget,然后右键提升为新建的类.
3.在新建类的构造函数中初始化系统空间.
示例组件:
示例代码:
#include "myWidget.h"
#include <QSpinBox>
#include <QSlider>
#include <QHBoxLayout>
myWidget::myWidget(QWidget *parent) : QWidget(parent)
{
auto layout=new QHBoxLayout(this);
auto spinBox=new QSpinBox(this);
auto slider=new QSlider(Qt::Horizontal,this);
layout->addWidget(spinBox,1);
layout->addWidget(slider,6);
spinBox->setRange(0,100);
slider->setValue(1);
connect(spinBox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
connect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));
}
效果展示: