#ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QDialog> #include <QLabel> #include <QCheckBox> #include <QLineEdit> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QMessageBox> class FindDialog:public QDialog { //对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必需的 Q_OBJECT public: FindDialog(QWidget *parent = 0); public: void myfindNext(const QString &str, Qt::CaseSensitivity cs); void myfindPrevious(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 // FINDDIALOG_H
#include "finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent) { //在字符串周围的tr()函数调用是把它们翻译成其他语言的标记(这是一个好习惯) label = new QLabel( tr( "Find &what" ) ); lineEdit = new QLineEdit; //标签伙伴:一个窗口部件,当你按下Alt+w时,焦点就会移动到这个行编辑器(该标签的伙伴)上 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" ) ); //当文本改变时候,使find按钮有效 connect( lineEdit, SIGNAL( textChanged( const QString & ) ), this, SLOT( enableFindButton( const QString & ) ) ); //当find按钮按下时候,触发findClicked函数 connect( findButton, SIGNAL( clicked() ), this, SLOT( findClicked() ) ); //当close按钮按下时候,触发close函数 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 myfindPrevious( text, cs ); //发射信号 } else { emit myfindNext( text, cs ); //发射信号 } } void FindDialog::enableFindButton( const QString &text ) { findButton->setEnabled( !text.isEmpty() ); } void FindDialog::myfindNext(const QString &str, Qt::CaseSensitivity cs) { if ( cs == Qt::CaseSensitive ){ QMessageBox::about( this, tr( "提示信息" ), tr( "向前搜索字符串:" ) + str + tr( "。并且大小写敏感!" ) ); } else{ QMessageBox::about( this, tr( "提示信息" ), tr( "向前搜索字符串:" ) + str + tr( "。并且大小写不敏感!" ) ); } } void FindDialog::myfindPrevious(const QString &str, Qt::CaseSensitivity cs) { if ( cs == Qt::CaseSensitive ){ QMessageBox::about( this, tr( "提示信息" ), tr( "向后搜索字符串:" ) + str + tr( "。并且大小写敏感!" ) ); } else{ QMessageBox::about( this, tr( "提示信息" ), tr( "向后搜索字符串:" ) + str + tr( "。并且大小写不敏感!" ) ); } }
#include "finddialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); FindDialog *dialog = new FindDialog; dialog->show(); return a.exec(); }
connect()语句通常如下:
connect( sender, SIGNAL( signal ), receiver, SLOT( slot ) );
connect( slider, SIGNAL( valueChanged( int ) ), spinBox, SLOT( setValue( int ) ) ); connect( slider, SIGNAL( valueChanged( int ) ), this, SLOT( updateStatusBarIndicator( int ) ) );
connect( lcd, SIGNAL( overflow() ), this, SLOT( handleMatchError() ) ); connect( calculator, SIGNAL( divisionByZero() ), this, SLOT( handleMatchError() ) );
connect( lineEdit, SIGNAL( textChanged( const QString & ) ), this, SIGNAL( updateRecord( const QString & ) ) );
disconnect( lcd, SIGNAL( overflow() ), this, SLOT( handleMatchError() ) );
要把信号成功连接到槽(或者连接到另外一个信号),它们的参数必须具有相同的顺序和相同的类型(毕竟这些参数要从信号传递到槽,或者传递到另外一个信号)。
#ifndef DIALOG_H #define DIALOG_H #include <QMainWindow> namespace Ui { class Dialog; } class Dialog : public QMainWindow { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private: Ui::Dialog *ui; private slots: void on_lineEdit_textChanged(const QString &); }; #endif // DIALOG_H
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QMainWindow(parent), ui(new Ui::Dialog) { ui->setupUi(this); QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); ui->lineEdit->setValidator(new QRegExpValidator(regExp, this)); connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(on_lineEdit_textChanged(QString))); connect(ui->okButton, SIGNAL(clicked()), this, SLOT(accept())); connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject())); } void Dialog::on_lineEdit_textChanged(const QString &text) { ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput()); } Dialog::~Dialog() { delete ui; }
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }