Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4241990
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-20 16:56:26

工程代码: 7.rar  
http://devbean.blog.51cto.com/448512/198971

  1. #ifndef FINDDIALOG_H
  2. #define FINDDIALOG_H

  3. #include <QDialog>

  4. //前向声明,否则不能编译通过
  5. class QCheckBox;
  6. class QLabel;
  7. class QLineEdit;
  8. class QPushButton;

  9. //定义 FindDialog ,继承与QDialog
  10. class FindDialog:public QDialog
  11. {
  12.     Q_OBJECT //信号槽 都需要

  13. public:
  14.     FindDialog(QWidget *parent=0);//构造函数
  15.     ~FindDialog();//析构函数

  16.  /*在特定的时刻发生这两个信号
  17.    当用户点击了find按钮,并且选中了search backward,
  18.    会发送findprevious,否则发送 findnext
  19. */
  20. signals:
  21.     void findNext(const QString &str, Qt::CaseSensitivity cs);
  22.     void findPrevious(const QString &str,Qt::CaseSensitivity cs);
  23. //
  24. private slots:
  25.     void findClick();
  26.     void enableFindButton(const QString &text);

  27. private:
  28.     QLabel *label; //标签 Find What
  29.     QLineEdit *lineEdit;
  30.     QCheckBox *caseCheckBox; //大小匹配 带文本标签的复选框
  31.     QCheckBox *backwardCheckBox; //向前向后 搜索
  32.     QPushButton *findButton; //搜索按钮
  33.     QPushButton *closeButton; //关闭按钮
  34. };
  35. #endif // FINDDIALOG_H

  1. //qtgui 包含了很过 头文件
  2. #include <QtGui>
  3. #include "finddialog.h""

  4. //构造函数有参数初始化列表,用来调用父类的构造函数
  5. FindDialog::FindDialog(QWidget *parent):QDialog(parent)
  6. {
  7. /*新建一个label 标签
  8.     tr函数全名QOject::tr(),被他处理的字符串可以工具提取出来翻译为其他语言
  9.     tr()函数是定义在object中,所有使用了Q_OBJEDT宏的类都自动具有tr()函数
  10.     &Find 字符串中的&代表快捷键
  11. */
  12.    label = new QLabel(tr("Find &what:"));
  13.     lineEdit = new QLineEdit;
  14.     label->setBuddy(lineEdit);//伙伴,设置焦点
  15. //QCheckBox窗口部件提供一个带文本标签的复选框
  16.     caseCheckBox = new QCheckBox(tr("Match &case"));
  17.     backwardCheckBox = new QCheckBox(tr("Search &backward"));
  18. //设置 find 按钮,为不可用,也就是灰色
  19.     findButton = new QPushButton(tr("&Find"));
  20. //This property holds whether the button border is raised.
  21.     findButton->setDefault(true);
  22.     findButton->setEnabled(false);
  23. //close 按钮
  24.     closeButton = new QPushButton(tr("Close"));

  25.     //当lineedit发出 textchanged后,enableFindButton
  26.     connect(lineEdit, SIGNAL(textChanged(QString)),this,SLOT(enableFindButton(QString)));
  27.     connect(findButton,SIGNAL(clicked()),this,SLOT(findClick()));
  28.     connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

  29. //水平布局 上左label lineEdit
  30.     QHBoxLayout *topleftlayout = new QHBoxLayout;
  31.     topleftlayout->addWidget(label);//添加组件
  32.     topleftlayout->addWidget(lineEdit);

  33. //垂直布局 topleftlayout caseCheck backwardcheckbox
  34.     QVBoxLayout *leftlayout = new QVBoxLayout;
  35.     leftlayout->addLayout(topleftlayout); //添加层
  36.     leftlayout->addWidget(caseCheckBox); //添加组件
  37.     leftlayout->addWidget(backwardCheckBox);

  38. //右垂直布局 findbutton closebutton
  39.     QVBoxLayout *rightlayout = new QVBoxLayout;
  40.     rightlayout->addWidget(findButton);
  41.     rightlayout->addWidget(closeButton);
  42.     rightlayout->addStretch();// ?? 向弹簧将上面的组件“顶起来”
  43. //设置全体布局
  44.     QHBoxLayout *mainlayout = new QHBoxLayout;
  45.     mainlayout->addLayout(leftlayout);
  46.     mainlayout->addLayout(rightlayout);
  47.     setLayout(mainlayout);

  48.     setWindowTitle(tr("Find));//设置对话框的标题
        setFixedHeight(sizeHint().height());//setFixedHeight设置成固定高度,sizehint返回最理想的高度
    }

    //析构函数
    FindDialog::~FindDialog()
    {

    }

    /*
    首先取出lineedit的输入的值,然后判断casecheckbox(大小写敏感)是不是选中,如果选中,就返回Qt::CaseInsensitive
    用于判断大小写敏感
    假如 backwardcheckbox, 向前,还是向后搜索
    */
    void FindDialog::findClick()
    {
        QString text = lineEdit->text();
        Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseInsensitive:Qt::CaseSensitive;
        if(backwardCheckBox->isChecked())
        {
            emit findPrevious(text,cs);
        }
        else
        {
            emit findNext(text,cs);
        }
    }

    /*
    根据lineedit中的内容是不是变化来设置findbutton是不是可以使用
    */
    void FindDialog::enableFindButton(const QString &text)
    {
        findButton->setEnabled(!text.isEmpty());
    }



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

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication app(argc, argv);
  6.     FindDialog *dialog = new FindDialog;
  7.     dialog->show();
  8.     return app.exec();
  9. }











阅读(764) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~