效果:c++ GUI QT4编程 一书第二章第一个例子
代码如下:
findddialog.h
-
#ifndef FINDDIALOG_H
-
#define FINDDIALOG_H
-
-
#include <QDialog>
-
-
class QCheckBox;
-
class QLabel;
-
class QLineEdit;
-
class QPushButton;
-
-
class FindDialog : public QDialog
-
{
-
Q_OBJECT
-
-
public:
-
FindDialog(QWidget *parent = 0);
-
-
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
上面代码解析:1.预处理防止这个头文件的多重包含
2.前置声明几个类,提高程序效率
3.声明信号和槽函数 注意这个信号出现在了槽函数(往后看)中
4.私有变量
第二个文件 finddialog.cpp
-
#include <QtGui>
-
-
#include "finddialog.h"
-
-
FindDialog::FindDialog(QWidget *parent)
-
: QDialog(parent)
-
{
-
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(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());
-
}
-
-
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 &text)
-
{
-
findButton->setEnabled(!text.isEmpty());
-
}
注意1.
setBuddy这是伙伴关系,和那个tab换到下一个输入口差不多2.&这是快捷键设置
解析:1.创建部件2.信号和槽函数3.布局 四个布局去看那本书
4.两个槽函数,注意上面的槽函数里面有信号,扩展#####可以继续用这个信号connect(信号,槽)来继续扩展槽,看书上写的第三章有
接下来main.cpp
-
#include <QApplication>
-
-
#include "finddialog.h"
-
-
int main(int argc, char *argv[])
-
{
-
QApplication app(argc, argv);
-
FindDialog *dialog = new FindDialog;
-
dialog->show();
-
return app.exec();
-
}
总结:之所以会发这个例子,因为这个例子提供了三方面QT的基础:1.信号与曹2.布局怎么布局3.就是一般的怎么写QT
写的不完善,具体内容参见c++ GUI QT4这本书第二章一开始
阅读(705) | 评论(0) | 转发(0) |