java 信号和槽_信号和插槽的工作

我对信号和插槽的实际工作方式有一个基本的疑问 . 这是我的代码段 .

finddialog.cpp:

#include "finddialog.h"

#include

#include

#include

FindDialog::FindDialog(QWidget *parent) : QDialog(parent) {

//VAR INITIALIZATIONS

label = new QLabel(tr("Find &what:"));

lineEdit = new QLineEdit;

label->setBuddy(lineEdit);

caseCheckBox = new QCheckBox(tr("Match &case"));

backwardCheckBox = new QCheckBox(tr("Search &backward"));

findButton = new QPushButton("&Find");

findButton->setDefault(true);

findButton->setEnabled(false);

closeButton = new QPushButton(tr("&Quit"));

//SIGNALS & SLOTS

connect (lineEdit, SIGNAL(textChanged(const QString&)),this, SLOT(enableFindButton(const QString&)));

connect (findButton, SIGNAL(clicked()), this, SLOT(findClicked()));

connect (closeButton,SIGNAL(clicked()), this, SLOT(close()));

//Layout

QHBoxLayout *topLeftLayout = new QHBoxLayout;

topLeftLayout->addWidget(label);

topLeftLayout->addWidget(lineEdit);

QVBoxLayout *leftLayout = new QVBoxLayout;

leftLayout->addLayout(topLeftLayout);

leftLayout->addWidget(caseCheckBox);

leftLayout->addWidget(backwardCheckBox);

QVBoxLayout *rightLayout = new QVBoxLayout;

rightLayout->addWidget(findButton);

rightLayout->addWidget(closeButton);

rightLayout->addStretch();

QHBoxLayout *mainLayout = new QHBoxLayout;

mainLayout->addLayout(leftLayout);

mainLayout->addLayout(rightLayout);

//Complete window settings

setLayout(mainLayout);

setWindowTitle(tr("Find"));

setFixedHeight(sizeHint().height());

}

//Function Definition

void FindDialog::findClicked() {

QString text = lineEdit->text();

Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;

if(backwardCheckBox->isChecked())

emit findPrevious(text, cs);

else

emit findNext(text,cs);

}

void FindDialog::enableFindButton(const QString &text1) {

findButton->setEnabled(!text1.isEmpty());

}

使用此代码,编译器如何知道传递给enableFindButton(QString&)函数的内容 . 没有函数调用enableFindButton() . 在connect语句中有一个对enableFindButton()的引用,但它不是更像原型,因为我们没有提供在其参数中使用的变量的名称?

connect (lineEdit, SIGNAL(textChanged(const QString&)),this, SLOT(enableFindButton(const QString&)));

这里只有(const QString&)是参数,并且没有给出变量 . 如果没有明确地传递它,应用程序如何知道它的参数是什么?

void FindDialog::enableFindButton(const QString &text1) {

findButton->setEnabled(!text1.isEmpty());

}

这里也&text1是一个参考参数 . 但到了什么?在输入所有内容后我现在什么都不懂! : - |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值