c++ qt--信号与槽(二) (第四部分)

c++ qt–信号与槽(二) (第四部分)

信号与槽的关系

1.一对一

2.一对多

3.多对一

4.多对多

还可以进行传递 信号->信号->槽

一个信号控制多个槽的例子(通过水平滑块控制两个组件)

1.应用的组件

在这里插入图片描述

注意这里最下面的组件进行了一些调整 如下

在这里插入图片描述

2.两个槽函数的声明和定义

1.声明

在mainwindow.h的类中写下面代码

public slots://槽函数的访问修饰符可以是public,protected或者private,三种访问修饰符都可以,不影响槽函数的使用
    void slots_time(int);
    void slots_progress(int);
2.定义

在mainwindow.cpp中写下面代码

void MainWindow::slots_time(int x)
{
    //QTime 是qt里写好的时间的类
    QTime time(0,0);//零点
    time=time.addSecs(6*6*24*x);//增加秒数
	
    ui->timeEdit->setTime(time);//将更新后的时间,设置到组件上


}

void MainWindow::slots_progress(int x)
{

    ui->progressBar->setValue(x);//进度条设置值

}

3.绑定链接

在mainwindow.cpp的构造函数中写下面代码

    ui->horizontalSlider->setRange(0,100);//设置水平滑块的范围
    ui->timeEdit->setDisplayFormat("hh-mm:ss");//设置显示的格式
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_time(int)));
    pun=connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_progress(int)));


    if(pun){
        qDebug()<<"成功";
    }
    else{
         qDebug()<<"失败";
    }

多个信号控制一个槽的例子(两个组件控制一个界面是否显示)

1.应用的组件

在这里插入图片描述

2.槽函数

1.声明

在dialog.h的类中写下面代码(这是上一篇博客创建的)

public slots:
    void slots_IsLIght();
2.定义

在dialog.cpp中写下面代码(这是上一篇博客创建的)

void Dialog::slots_IsLIght()
{
    //    if(isVisible()){
	//        hide();
	//    }
	//    else{
	//        show();
    //    }
    //上面的代码可以写成三目运算符
    
    isVisible()?hide():show();//三目运算符
	
}

3.绑定连接

在main.cpp中写下面代码

QObject::connect(w.Getui()->pb_door,SIGNAL(clicked()),&dia,SLOT(slots_IsLIght()));
QObject::connect(w.Getui()->pb_bed,SIGNAL(clicked()),&dia,SLOT(slots_IsLIght()));

信号->信号->槽的例子()

1.应用的组件

在这里插入图片描述

在这里插入图片描述

2.声明信号

在mainwindow.h的类中写下面代码

signals://声明信号的关键字
    void signals_XinHao(int);//仅声明即可

3.连接两个信号

在mainwindow.cpp的构造函数中写下面代码

connect(ui->spinBox,SIGNAL(valueChanged(int)),this,SIGNAL(signals_XinHao(int)));

4.槽函数

1.声明

在dialog.h的类中写下面代码(这是上一篇博客创建的)

public slots:
    void slots_Lcd(int);
};
2.定义

在dialog.cpp中写下面代码(这是上一篇博客创建的)

void Dialog::slots_Lcd(int num)
{
    ui->lcdNumber->display(num);//显示数字
}

5.绑定连接信号和槽函数

在main.cpp中写下面代码

QObject::connect(&w,SIGNAL(signals_XinHao(int)),&dia,SLOT(slots_Lcd(int)));

将信号与槽断开连接

1.应用的组件

在这里插入图片描述

通过”check’ box“的组件将滑块与滑块下面两个组件的连接断开

2.转到槽函数(这里用qt自带的功能创建槽函数)

定义

在mainwindow.cpp中写下面代码

//原连接
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_time(int)));
pun=connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_progress(int)));//
//pun用来接返回值

//看当前连接信息是否成功
if(pun){
        qDebug()<<"成功";
 }
 else{
        qDebug()<<"失败";
 }


//pun是在在mainwindow.cpp类中进行声明,声明如下
/*punlic:
QMetaObject::Connection pun;
*/

//进行断开连接
void MainWindow::on_cb_cancelValue_stateChanged(int arg1)
{
    if(arg1==Qt::CheckState::Checked){//取消连接
        disconnect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_time(int)));
    }
    else{//重新连接
        connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_time(int)));
    }
}


void MainWindow::on_cb_cancelProgress_stateChanged(int arg1)
{
    if(arg1==Qt::CheckState::Checked){//取消连接
        disconnect(pun);
    }
    else{
        pun=connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(slots_time(int)));
    }
}

猜你喜欢

转载自blog.csdn.net/m0_73483024/article/details/132439577