• 使用C++编写的一个Find(查找)对话框例子


    //findDialog.h 头文件

     1 //finddialog.h
     2 #ifndef FINDDIALOG_H
     3 #define FINDDIALOG_H
     4 
     5 #include <QDialog>
     6 
     7 class QCheckBox;
     8 class QLabel;
     9 class QLineEdit;
    10 class QPushButton;
    11 
    12 
    13 class FindDialog : public QDialog
    14 {
    15     Q_OBJECT
    16 
    17 public:
    18     FindDialog(QWidget *parent = 0);
    19 signals:
    20     void findNext(const QString &str,Qt::CaseSensitivity cs);
    21     void findPrevious(const QString &str,Qt::CaseSensitivity cs);
    22 private slots:
    23     void findClicked();
    24     void enableFindButton(const QString &text);
    25 private:
    26     QLabel *label;
    27     QLineEdit *lineEdit;
    28     QCheckBox *caseCheckBox;
    29     QCheckBox *backwardCheckBox;
    30     QPushButton *findButton;
    31     QPushButton *closeButton;
    32 };
    33 
    34 #endif // FINDDIALOG_H

    //findDialog.cpp 实现文件

     1 //finddialog.cpp
     2 #include <QtGui>
     3 #include "finddialog.h"
     4 
     5 FindDialog::FindDialog(QWidget *parent):QDialog(parent)
     6 {
     7     label = new QLabel(tr("Find &what")); //tr()函数是把他们翻译成其他语言的标记
     8     lineEdit = new QLineEdit;
     9     label->setBuddy(lineEdit);  //设置了行编辑器作为标签的伙伴(buddy)
    10                                 //所谓“buddy”是指一个窗口部件,它可以在按下标签的快捷键时接受焦点(focus)
    11     caseCheckBox = new QCheckBox(tr("Match &case"));
    12     backwardCheckBox = new QCheckBox(tr("Search &backward"));
    13 
    14     findButton = new QPushButton(tr("&Find"));
    15     findButton->setDefault(true);  //setDefault()让Find按钮成为对话框的默认按钮(default button)。
    16                                    //default button 就是指当用户按下Enter健时能够按下对应的按钮
    17     findButton->setEnabled(false); //默认禁止,显示灰色
    18     closeButton = new QPushButton(tr("Close"));
    19     connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
    20     connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
    21     connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
    22 
    23     QHBoxLayout *topLeftLayout = new QHBoxLayout;
    24     topLeftLayout->addWidget(label);
    25     topLeftLayout->addWidget(lineEdit);
    26     QVBoxLayout *leftLayout = new QVBoxLayout;
    27     leftLayout->addLayout(topLeftLayout);
    28     leftLayout->addWidget(caseCheckBox);
    29     leftLayout->addWidget(backwardCheckBox);
    30     QVBoxLayout *rightLayout = new QVBoxLayout;
    31     rightLayout->addWidget(findButton);
    32     rightLayout->addWidget(closeButton);
    33     rightLayout->addStretch();  //添加分隔符(Stretch)
    34 
    35     QHBoxLayout *mainLayout = new QHBoxLayout;
    36     mainLayout->addLayout(leftLayout);
    37     mainLayout->addLayout(rightLayout);
    38     this->setLayout(mainLayout);
    39 
    40     setWindowTitle(tr("Find"));
    41     setFixedHeight(sizeHint().height()); //让窗口具有一个固定的高度,对话框
    42 
    43 };
    44 
    45 void FindDialog::findClicked()
    46 {
    47     QString text = lineEdit->text();  //取编辑框中的文本保存在一个String中
    48     Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;//Qt::CaseSensitivity 是一个枚举类型
    49     if(backwardCheckBox->isChecked())
    50         emit findPrevious(text,cs);
    51     else
    52         emit findNext(text,cs);
    53 }
    54 
    55 void FindDialog::enableFindButton(const QString &text)
    56 {
    57     findButton->setEnabled(!text.isEmpty());
    58 }

    //main.cpp 测试函数

     1 //main.cpp
     2 #include <QApplication>
     3 #include "finddialog.h"
     4 
     5 int main(int argc,char *argv[])
     6 {
     7     QApplication app(argc,argv);
     8     FindDialog *dialog = new FindDialog;
     9     dialog->show();
    10 
    11     return app.exec();
    12 }

    //执行结果

  • 相关阅读:
    postgresql远程连接不上
    Linux安装配置
    exportfs rv报错
    PostgreSQL14中预定义角色pg_write_all_data和pg_read_all_data
    Windows Server 2016上安装SQL Server 2019 AlwaysOn Availability Groups(一)
    C#学习教程:LINQ to Entities无法识别方法’System.String Split(Char )’方法
    Git 配置 sshkey 免密登录
    RedmiBook pro 15 笔记本 Manjaro 下网卡(8852AE)驱动安装记录
    vue 中 computed 、created 、mounted 的先后顺序
    .net开源框架开源类库(整理)
  • 原文地址:https://www.cnblogs.com/SamRichard/p/3520216.html
Copyright © 2020-2023  润新知