创建一个 FindDialog,功能还没有添加…以后再搞吧
代码:
findDialog.h
#ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QtGui/QDialog> #include<QCheckBox> #include<QLabel> #include<QLineEdit> #include<QPushButton> namespace Ui { class FindDialogClass; } class FindDialog : public QDialog { Q_OBJECT public: FindDialog(QWidget *parent = 0); ~FindDialog(); signals: void findNext(const QString &str,Qt::CaseSensitivity cs); void findPrev(const QString &str,Qt::CaseSensitivity cs); private slots: void findClicked(); void enableFindButton(const QString &text); private: Ui::FindDialogClass *ui; QLabel *label; QLineEdit *lineEdit; QCheckBox *caseCheckBox; QCheckBox *backwardCheckBox; QPushButton *findButton; QPushButton *closeButton; }; #endif
|
findDialog.cpp
#include<QtGui> #include<QHBoxLayout> #include "finddialog.h" #include "ui_finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent), ui(new Ui::FindDialogClass) { ui->setupUi(this); label=new QLabel(tr("&Input the word you want to find")); lineEdit=new QLineEdit(); label->setBuddy(lineEdit); caseCheckBox=new QCheckBox(tr("Match &Case")); backwardCheckBox=new QCheckBox(tr("Search &backward")); findButton=new QPushButton(tr("Find")); 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); this->setLayout(mainLayout); this->setWindowTitle(tr("Find")); this->setFixedHeight(sizeHint().height()); } FindDialog::~FindDialog() { delete ui; } void FindDialog::findClicked() { QString text=lineEdit->text(); Qt::CaseSensitivity cs=caseCheckBox->isChecked()?Qt::CaseSensitive : Qt::CaseInsensitive; if(backwardCheckBox->isChecked()) emit findPrev(text,cs); else emit findNext(text,cs); } void FindDialog::enableFindButton(const QString &text) { findButton->setEnabled(!text.isEmpty()); }
|
main.cpp
#include <QtGui/QApplication> #include "finddialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); FindDialog *findDialog=new FindDialog; findDialog->show(); return app.exec(); }
|
运行结果:
PS 编译过程出现这个错误: :-1: error: collect2: ld returned 1 exit
status ,但是当我程序写完的时候问题就解决了 ….orz,顺便说下,QtCreator
的补全功能还是很不错的….1.0 果然比 0.9 好太多了….赞一个