百度空间 | 百度首页 
 
查看文章
 
The Find Dialog
2009-06-20 19:34

Creating Find Dialogs
1.
finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H //protect the header file against multiple inclusions

#include <QDialog> //QDialog is the base class for dialog in Qt,it is derived from QWidget

class QCheckBox; //forward declarations of Qt classes that will use to implement the dialog
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
//the Q_OBJECT macro definnition is necessary for all class that
//difine signals or slots
Q_OBJECT
public:
FindDialog(QWidget * parent = 0);
signals:
   //declare two signals that the dialog emit
   //Qt::CaseSensitivity is an enum type that can take the values Qt::CaseSensitive and Qt::CaseInSensitive
   void findNext(const QString &str, Qt::CaseSensitivity cs);
   void findPrevious(const QString &str, Qt::CaseSensitivity cs);//it works when search backward is enable
private slots:
void findChecked();
void enableFindButton(const QString &text);

private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;

  
};
#endif


2.
finddialog.cpp
#include <QtGui>
#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
//ampersand indicate the shortcut key
label = new QLabel( tr("Find &what:") );
lineEdit = new QLineEdit;
label->setBuddy( lineEdit );

//thr tr() function calls around string literals mark them for stranslation to other language
caseCheckBox = new QCheckBox( tr("Match &case") );
backwardCheckBox = new QCheckBox( tr("Search &backward") );

findButton = new QPushButton( tr("&find") );
findButton->setDefault(true);//make the findButton the dialog's default button
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::findChecked()
{
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());
}

3.
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 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu