对信号与槽的理解:信号就好理解,槽就类似与事件。信号和槽先连接起来,一旦有信号发出,该信号就会触发对应的槽(也就是事件!!)
一个信号可以对应多个槽,槽会一个个被调用,但是槽被调用的顺序是随机的。。。这点有难理解。。。。
多个信号可以对应一个槽,只要有其中一个信号发出,该槽就会被调用!!
Qt中的布局是个好东西啊,即使没有可视化,也能用代码简单实现!!布局后,很容易忘记setLayout(...)...调试了很久啊。。。郁闷!!!
对应书上的代码打了一下!顺便熟悉一下Qt的语法,和Qt Create调试。
打开Qt Creator,新建一个空白工程。然后添加如下3个文件:
/*
* 文件名:finddialog.h
*
*/
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QtGui/QDialog>
//引用类对应头文件 比 直接声明类 费时间!!
//目测用声明类好一点
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT
public:
explicit FindDialog(QWidget *parent = 0);
~FindDialog();
//信号
signals:
void findNext(const QString &str,Qt::CaseSensitivity cs);
void findPrevious(const QString &str,Qt::CaseSensitivity cs);
//私有槽
private slots:
void findClicked();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif // FINDDIALOG_H
/*
* 文件名:finddialog.cpp
*
*/
#include <QtGui> //引入这个巨大的库是很费时间的!!!!
#include "finddialog.h"
#include "ui_finddialog.h"
FindDialog::FindDialog(QWidget *parent) :
QDialog(parent)
{
//ui->setupUi(this);
//QObject::tr()....用于国际化!!!!
//因为FindDialog类已经包含了Q_OBJECT宏,所以用tr()函数就行了!!
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backford"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
//连接信号和槽
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()));
//布局
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);
setLayout(mainLayout);//放置布局
setWindowTitle(tr("Find"));//标题
//设置固定高度
setFixedHeight(sizeHint().height());
}
FindDialog::~FindDialog()
{
//delete ui;
}
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
if(backwardCheckBox->isChecked()){
emit findPrevious(text,cs);
}else{
emit findNext(text,cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
//文本发生改变,且当前文本编辑框不为空时,就激活按钮
findButton->setEnabled(!text.isEmpty());
}
/*
* 文件名:main.cpp
*
*/
#include <QtGui/QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return a.exec();
}
接下来就是 Ctrl+S (保存) 和Ctrl+R (编译和运行)。。。。和VS有点区别。。。