具体代码如下:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
smallwidget.cpp \
widget.cpp
HEADERS += \
smallwidget.h \
widget.h
FORMS += \
smallwidget.ui \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "smallwidget.h"
#include "ui_smallwidget.h"
#include <QDebug>
smallWidget::smallWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::smallWidget)
{
ui->setupUi(this);
//信号和槽中不需要写函数名后的() 注意重载 使用函数指针或者类型转换来表示
connect(ui->spinBox,(void (QSpinBox:: *)(int))&QSpinBox::valueChanged,ui->horizontalSlider,&QSlider::setValue);
connect(ui->horizontalSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
}
smallWidget::~smallWidget()
{
delete ui;
}
void smallWidget::getNum()
{
qDebug()<<ui->spinBox->value();
}
void smallWidget::setNum(int n )
{
ui->horizontalSlider->setValue(50);
}
#ifndef SMALLWIDGET_H
#define SMALLWIDGET_H
#include <QWidget>
namespace Ui {
class smallWidget;
}
class smallWidget : public QWidget
{
Q_OBJECT
public:
explicit smallWidget(QWidget *parent = nullptr);
~smallWidget();
public:
void getNum();
void setNum(int n);
private:
Ui::smallWidget *ui;
};
#endif // SMALLWIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->btn_get,&QPushButton::clicked,ui->widget,&smallWidget::getNum);
connect(ui->btn_set,&QPushButton::clicked,ui->widget,&smallWidget::setNum);
}
Widget::~Widget()
{
delete ui;
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
运行结果:
获取进度 下方qDebug输出进度
设置一般设置到50
拖动进度条和上下调节数字相互关联.
注意重载(信号和槽) 使用函数指针或者类型转换.