用两个控件实现一个水平布局的效果
1.新建一个无ui界面的工程,其基类为widget
2.代码实现
widget.h头文件不用做任何改变
widget.cpp
#include
"widget.h"
#include
<QSlider>
#include
<QSpinBox>
#include
<QLayout>
Widget
::Widget(QWidget
*parent
)
: QWidget(parent
)
{
QSpinBox
* SpinBox
= new QSpinBox();
QSlider
* Slider
= new QSlider(Qt
::Horizontal
);
SpinBox
->setValue(46);
Slider
->setMaximum(100);
Slider
->setMinimum(0);
Slider
->setValue(50);
Slider
->update();
QHBoxLayout
* layout
= new QHBoxLayout;
layout
->addWidget(SpinBox
);
layout
->addWidget(Slider
);
this->setLayout(layout
);
connect(Slider
,SIGNAL(valueChanged(int)),SpinBox
,SLOT(setValue(int)));
connect(SpinBox
,SIGNAL(valueChanged(int)),Slider
,SLOT(setValue(int)));
}
Widget
::~Widget()
{
}
main.cpp
#include
"widget.h"
#include
<QApplication>
#include
<QSlider>
#include
<QSpinBox>
#include
<QLayout>
int main(int argc
, char *argv
[])
{
QApplication
a(argc
, argv
);
Widget w
;
w
.setWindowTitle("水平布局");
w
.resize(300,80);
w
.show();
return a
.exec();
}
3.效果展示
转载请注明原文地址: https://win8.8miu.com/read-2047.html