mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QtWidgets/QDialog>
#include "ui_mydialog.h"
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class MyDialog : public QDialog
{
Q_OBJECT
public:
MyDialog(QWidget *parent = 0);
~MyDialog();
signals:
void findNext(const QString *str, Qt::CaseSensitivity cs);
void findPrevious(const QString *str, Qt::CaseSensitivity cs);
private:
Ui::MyDialogClass ui;
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
private slots:
void findClicked();
void enableFindButton(const QString &text);
};
#endif // MYDIALOG_H
mydialog.cpp
#include "mydialog.h"
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qcheckbox.h>
#include <qlineedit.h>
#include <qstring.h>
MyDialog::MyDialog(QWidget *parent)
: QDialog(parent)
{
//函数tr()全名是 QObject::tr(),被它处理的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用。
//为什么我们没有写QObject::tr(),而仅仅是tr()呢?原来,tr()函数是定义在Object里面的,所有使用了Q_OBJECT宏的类都自动具有tr()函数。
//你可以从mydialog.h中看到Q_OBJCET的宏。
//&代表快捷键,&what快捷键是ALT+w,可以快速定位或者点击
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
//当被定位时,将焦点传给“伙伴”lineEdit
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"));
//设置信号和回调函数
QObject::connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));
QObject::connect(findButton ,SIGNAL(clicked()), this, SLOT(findClicked()));
QObject::connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
//布局 H-Horizontal V-Vertical
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);
//addStretch会自动拉伸,把button对齐。
rightLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
//这是系统自带的窗口形式,标题是myapp。我们不用它。
//ui.setupUi(this);
}
MyDialog::~MyDialog()
{
}
void MyDialog::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 MyDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp
#include "mydialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog w;
w.show();
return a.exec();
}
运行:
说明:
1、首先,在新建项目中基类(baseclass)选择QDialog。
2、编码中,
(1)label = new QLabel(tr("Find &what:"));
//函数tr()全名是 QObject::tr(),被它处理的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用。
//为什么我们没有写QObject::tr(),而仅仅是tr()呢?原来,tr()函数是定义在Object里面的,所有使用了Q_OBJECT宏的类都自动具有tr()函数。
//你可以从mydialog.h中看到Q_OBJCET的宏。
//&代表快捷键,&what快捷键是ALT+w,可以快速定位或者点击
(2) //当被定位时,将焦点传给“伙伴”lineEdit
label->setBuddy(lineEdit);
(3) //这是系统自带的窗口形式,标题是myapp。我们不用它。
//ui.setupUi(this);
(4)setFixedHeight(sizeHint().height());
这个函数是用来设置系统自带的组件的大小的,如果没有设置,则出现:
细心一点,对比原来的,你会发现组件之间的距离变大了。
(5)//addStretch会自动拉伸,把button对齐。
rightLayout->addStretch();
这句是把组件自动拉伸成均分的形式,再次细心对比原来的,你会发现右边的两个button和坐标对得不齐了。这就是addStretch的作用。
(6)emit
如果定义了用户信号,总会在一定情况下需要发出信号,emit就是用来发出用户信号的。
3、main.cpp不用改动,已经自动创建一个mydialog的对象了。