现在的位置: 首页 > 综合 > 正文

3)~Qt~find对话框

2013年03月24日 ⁄ 综合 ⁄ 共 2496字 ⁄ 字号 评论关闭

注:全盘copy

来源C++ GUI Programming with Qt 4

 

 

头文件

 

 

 

#ifndef STUDY_2_H

 

***************************************************************

CPP

下面的程序中一大部分都是布局的,核心的地方还是信号槽的机制问题还有

尽管没有实现find的功能,不过findClock()函数值得借鉴。

#include <QtGui>

#include "study_2.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());
 }
##############################################
最后main.cpp

 

#include <QApplication>
 #include "study_2.h"
 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     finddialog *dialog = new finddialog;
     dialog->show();
     return app.exec();
 }
比较简略,先这样吧。
#define STUDY_2_H
#include<QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class finddialog : public QDialog
{
    Q_OBJECT;
public:
    finddialog(QWidget *parent=0);//父类参数为0
signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs) ;
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);//后者是一个枚举类型{0,1}
private slots:
     void findClicked();
    void enableFindButton(const QString &text);
    private:
     QLabel *label;
    QLineEdit *lineEdit;
        QCheckBox *caseCheckBox;
      QCheckBox *backwardCheckBox;
        QPushButton *findButton;
         QPushButton *closeButton;
};
#endif // STUDY_2_H

抱歉!评论已关闭.