Qt中使两个部件同步,这里为spin和slider

#include <qapplication.h>
#include <qhbox.h>
#include <qslider.h>
#include <qspinbox.h>

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);

    QHBox *hbox=new QHBox(0);
    hbox->setCaption("Enter your age:");
    hbox->setMargin(6);
    hbox->setSpacing(6);
   
    QSpinBox *spinBox=new QSpinBox(hbox);
    QSlider *slider = new QSlider(Qt::Horizontal,hbox);
    spinBox->setRange(0,130);
    slider->setRange(0,130);

    QObject::connect(spinBox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
    QObject::connect(slider,SIGNAL(valueChanged(int)),spinBox,SLOT(setValue(int)));
   
    spinBox->setValue(35);

    hbox->show();
    return app.exec();
}

首次编译的时候出现了这个error

 error: qhbox.h: 没有那个文件或目录

解决方法: 编译需要qt3-dev-tools , libqt3-mt-dev,我可能没有安装,装上以后,重新编译,ok,搞定

原文地址:https://www.cnblogs.com/shaoguangleo/p/2805858.html