本文共 2875 字,大约阅读时间需要 9 分钟。
本例子涉及到了快捷键,信号,槽,读者自己看代码,我给出了框架。
find.h文件代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #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代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #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
1 2 3 4 5 6 7 8 9 10 | #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(); } |
转载地址:http://oubbl.baihongyu.com/