1.什么是信号和槽?
信号:信号就是指控件发出的特定的信号。
查看一个控件的信号可以点击”帮助->索引下收索控件名称(这里是QPushButton)->Signals“。
槽就是槽函数的意思,我们可以把槽函数绑定在某一个控件的信号上。
2.怎么关联信号和槽
<1>自动关联
选择控件,右键转到槽,自动关联会给我们添加如下内容:
槽函数只能声明到private slots或者public slots下面。
<2>手动关联
手动关联使用connect函数。
同时列举一个简单页面之间切换的案例,本QT实例的下载刻点击此资源
主要的widget.cpp代码如下:
#include "widget.h"
#include "ui_widget.h"
#include"form2.h"
#include<QString>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->Login,SIGNAL(clicked()),this,SLOT(login_slot()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_registered_clicked()
{
qDebug("hahhahah");
}
void Widget::login_slot()
{
// qDebug("dengrucaohanshu");
QString userpassword=ui->password->text();
QString userName=ui->acount_number->text();
if(userpassword=="123" && userName=="123")
{
Form2 *form=new Form2;
form->setGeometry(this->geometry());
form->show();
}
}