QT 系統控件封裝 佈局設置

概述: 把多個控件組合成一個自定義控件,方便使用.

核心步驟:

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)));
}

效果展示:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章