对话框

本例子涉及到了快捷键,信号,槽,读者自己看代码,我给出了框架。

find.h文件代码如下:

#ifndef FIND_H
#define FIND_H

#include <QtGui>
#include "ui_find.h"

class Find : public QDialog
{
	Q_OBJECT

public:
	Find(QWidget *parent = 0, Qt::WFlags flags = 0);
	~Find();

signals:
	void findNext1(const QString &str,Qt::CaseSensitivity cs);
	void findPrevious1(const QString &str,Qt::CaseSensitivity cs);

private slots:
	void findClicked();
	void enableFindButton(const QString &str);

private:
	Ui::FindClass ui;

	QLabel *label;
	QLineEdit *textLine;
	QCheckBox *matchBox;
	QCheckBox *searchBox;
	QPushButton *findButton;
	QPushButton *quitButton;
};

#endif // FIND_H

 find.cpp代码如下:

#include "find.h"

Find::Find(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);

	label=new QLabel(tr("Find &What:"));
	textLine=new QLineEdit();
	label->setBuddy(textLine);
	QHBoxLayout *hlayout=new QHBoxLayout();
	hlayout->addWidget(label);
	hlayout->addWidget(textLine);

	findButton=new QPushButton(tr("&Find"));
	findButton->setEnabled(false);
	findButton->setDefault(true);
	quitButton=new QPushButton(tr("&Quit"));
	QVBoxLayout *vlayout=new QVBoxLayout();
	vlayout->addWidget(findButton);
	vlayout->addWidget(quitButton);

	matchBox=new QCheckBox(tr("Match &Case"));
	searchBox=new QCheckBox(tr("Search &Backword"));
	QVBoxLayout *vlayout1=new QVBoxLayout();
	vlayout1->addWidget(matchBox);
	vlayout1->addWidget(searchBox);

	QHBoxLayout *mainLayout=new QHBoxLayout();
	mainLayout->addLayout(hlayout);
	mainLayout->addLayout(vlayout);
	mainLayout->addLayout(vlayout1);
	setLayout(mainLayout);

	setWindowTitle(tr("Find"));
	setFixedHeight(sizeHint().height());

	connect(textLine,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString&)));
	connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
	connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
}

Find::~Find()
{

}

// void Find::findNext1(const QString &str,Qt::CaseSensitivity cs){
//     //do something here
// }
// 
// void Find::findPrevious1(const QString &str,Qt::CaseSensitivity){
//            //do something here
// }

void Find::findClicked(){
	QString text=textLine->text();
	Qt::CaseSensitivity cs=matchBox->isChecked() ? Qt::CaseSensitive:Qt::CaseInsensitive;
	if(searchBox->isChecked()){
		emit findPrevious1(text,cs);
	}else{
		emit findNext1(text,cs);
	}
}

void Find::enableFindButton(const QString &str){
	findButton->setEnabled(!(str.isEmpty()));
}

 main.cpp

#include "find.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	Find w=new Find();
	w.show();
	return a.exec();
}
原文地址:https://www.cnblogs.com/rollenholt/p/2262233.html