您的位置:首页 > 编程语言 > Qt开发

第二章:创建对话框

2016-02-24 17:47 246 查看

1、手动创建对话框

1)finddialog.h头文件

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog:public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent = 0);
public:
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

2)finddialog.cpp

<pre name="code" class="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() );
}

void FindDialog::findNext(const QString &str, Qt::CaseSensitivity cs)
{
if ( cs == Qt::CaseSensitive )
{
QMessageBox::about( this, tr( "information tips " ), tr( "Previous:" ) +
str + tr( "Sensitivite!" ) );
}
else{
QMessageBox::about( this, tr( "information tips " ), tr( "Previous" ) +
str + tr( "InSensitivite!" ) );
}
}

void FindDialog::findPrevious(const QString &str, Qt::CaseSensitivity cs)
{
if ( cs == Qt::CaseSensitive ){
QMessageBox::about( this, tr( "information tips " ), tr( "Backward" ) +
str + tr( "Sensitivite!" ) );
}
else{
QMessageBox::about( this, tr( "information tips " ), tr( "Backward" ) +
str + tr( "InSensitivite!" ) );
}
}

<pre name="code" class="cpp" style="font-size: 11.8181819915771px;">在finddialog.cpp源文件中,
Qmessage::about()函数具体入口参数, QMessageBox::about(NULL, "string", "string1");
这样就会在最后就会出现提示窗口。


3)main.cpp源文件

#include "finddialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();

return app.exec();
}
运行结果为:

在编辑框输入相应数据时反馈结果为:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt