C++ GUI Qt4编程(04)-2.1findDialog

finddialog.h

/*
 * 未实现findNextSignal和findPreviousSignal
 */
#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>

class QLabel;
class QLineEdit;
class QCheckBox;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;

class FindDialog : public QDialog
{
    Q_OBJECT

public:
    /*构造函数*/
    FindDialog(QWidget *parent = 0);
    /*析构函数*/
    ~FindDialog();

signals:
    void findNextSignal(const QString &str, Qt::CaseSensitivity cs);
    void findPreviousSignal(const QString &str, Qt::CaseSensitivity cs);

private slots:
    void findClickedSlot();
    void enableFindButtonSlot(const QString &text);

private:
    QLabel 		*label;
    QLineEdit 	*lineEdit;
    QPushButton *findButton;
    QPushButton *closeButton;
    QCheckBox 	*caseCheckBox;
    QCheckBox 	*backwardCheckBox;
    QVBoxLayout *leftLayout;
    QVBoxLayout *rightLayout;
    QHBoxLayout *topLeftLayout;
    QHBoxLayout *mainLayout;
};

#endif

 

finddialog.cpp

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include "finddialog.h"

/*构造函数*/
FindDialog::FindDialog(QWidget *parent)
    : QDialog(parent)	/*基类*/
{
    /*不设置伙伴关系的时候,运行时W下不显示一条横线,而是显示&What*/
    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(enableFindButtonSlot(const QString &)));
	connect(findButton, SIGNAL(clicked()),  \
	        this, SLOT(findClickedSlot()));
	connect(closeButton, SIGNAL(clicked()),  \
	        this, SLOT(close()));

    topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
	rightLayout->addStretch();

    mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);

    setLayout(mainLayout);
	
	setWindowTitle(tr("Find"));
}

/*析构函数*/
FindDialog::~FindDialog()
{

}

/**/
void FindDialog::findClickedSlot()
{
	QString text = lineEdit->text();
	
	/*判断大小写是否要匹配*/
	Qt::CaseSensitivity cs =   \
		caseCheckBox->isChecked() ? Qt::CaseSensitive  \
		                          : Qt::CaseInsensitive;
	
	if (backwardCheckBox->isChecked())
	{
		emit findPreviousSignal(text, cs);		/*发送信号*/
	}
	else
	{
		emit findNextSignal(text, cs);
	}
}

/*启用或禁用Find按钮*/
void FindDialog::enableFindButtonSlot(const QString &text)
{
	findButton->setEnabled(!text.isEmpty());
}

 

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();
}

 

posted @ 2017-06-22 22:18  seifguo  阅读(323)  评论(1编辑  收藏  举报