C++ Exercises(三)

// finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include 
<QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;


class FindDialog : public QDialog
{
      Q_OBJECT
public:
       FindDialog(QWidget 
*parent = 0);
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 &str);
private:
        QLabel 
*lable;
        QLineEdit 
*lineEdit;
        QCheckBox 
*caseCheckBox;
        QCheckBox 
*backwardCheckBox;
        QPushButton 
*findBtn;
        QPushButton 
*closeBtn;
};

#endif

// finddialog.cpp
#include <QtGui>
#include 
"finddialog.h"

FindDialog::FindDialog(QWidget 
*parent) : QDialog(parent)
{
   
    
this->lable = new QLabel(tr("Find &what:"),this);
    
this->lineEdit = new QLineEdit(this);
    lable
->setBuddy(lineEdit);
    
    caseCheckBox 
= new QCheckBox(tr("Match &case"),this);
    backwardCheckBox 
= new QCheckBox(tr("Search &backward"),this);
    
    findBtn 
= new QPushButton(tr("&Find"),this);
    findBtn
->setDefault(true);
    findBtn
->setEnabled(false);
    closeBtn 
= new QPushButton(tr("Close"),this);
    
    QObject::connect(lineEdit,SIGNAL(textChanged(
const QString &)),this,SLOT(enableFindButton(const QString &)));
    QObject::connect(findBtn,SIGNAL(clicked()),
this,SLOT(findClicked()));
    QObject::connect(closeBtn,SIGNAL(clicked()),
this,SLOT(close()));

    QHBoxLayout 
*topLeftLayout = new QHBoxLayout;
    topLeftLayout
->addWidget(lable);
    topLeftLayout
->addWidget(lineEdit);
    
    QVBoxLayout 
*leftLayout = new QVBoxLayout;
    leftLayout
->addLayout(topLeftLayout);
    leftLayout
->addWidget(caseCheckBox);
    leftLayout
->addWidget(backwardCheckBox);
    
    QVBoxLayout 
*rightLayout = new QVBoxLayout;
    rightLayout
->addWidget(findBtn);
    rightLayout
->addWidget(closeBtn);
    rightLayout
->addStretch(1);
    
    QHBoxLayout 
*mainLayout = new QHBoxLayout(this);
    mainLayout
->setMargin(11);
    mainLayout
->setSpacing(5);
    mainLayout
->addLayout(leftLayout);
    mainLayout
->addLayout(rightLayout);
    
this->setWindowTitle(tr("Find"));
    
this->setFixedHeight(sizeHint().height());
}

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 &str)
{
     findBtn
->setEnabled(!str.isEmpty());
}

//main.cpp
#include <QApplication>
#include 
"finddialog.h"

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    FindDialog 
*dialog = new FindDialog;
    dialog
->show();
    
return app.exec();
}

posted on   Phinecos(洞庭散人)  阅读(720)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

统计

点击右上角即可分享
微信分享提示