概述: 把多個控件組合成一個自定義控件,方便使用.
核心步驟:
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)));
}
效果展示: