1、自动连接

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    QPushButton *pushButton = new QPushButton(this);
    pushButton->setObjectName("TestButton");
    ui->setupUi(this);
}



Widget::~Widget()
{
    delete ui;
}


void Widget::on_TestButton_clicked()
{
    close();
}

如果槽函数命名方式为:on_控件名称_clicked,则不需要再手动connect连接信号与槽函数,此时QT内部会有相应的机制,将控件与对应的槽函数关联起来。

 

2、手动关联

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    QPushButton *pushButton =new QPushButton(this);
    pp->setObjectName("TestButton");
    ui->setupUi(this);
    connect(pp,SIGNAL(clicked()),this,SLOT(mySlot()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::mySlot(){
    qDebug() << "Hello World";
}

槽函数并不是按 on_控件名称_clicked 方式命名的,此时想将控件与该槽函数关联的话,可以用手动connect,让它们关联起来。

 

3、设计模式关联

在QT Creator 的 design 模式下,点击下图中提示的按钮,可以进入信号槽编辑模式,可直接在两个控件之间拖动,在两者添加信号槽关联机制

Qt 信号与槽connect方式

 

相关文章:

  • 2021-08-30
  • 2021-11-20
  • 2022-12-23
  • 2021-08-11
  • 2021-06-18
猜你喜欢
  • 2022-01-22
  • 2022-12-23
  • 2021-06-18
  • 2021-08-20
  • 2021-06-07
  • 2022-01-11
相关资源
相似解决方案