一个基于Qt的简单查找对话框

4 篇文章 0 订阅

最近学习Qt时设计了一个简单的查找对话框,大致界面如下:

在这里插入图片描述
当用户输入查找目标,按下查找按钮后,对话框会发射一个信号,信号中包含着查找目标,反向查找标志,匹配大小写标志等关键信息。
代码如下:

//下面是QFindDialog的头文件
#ifndef __QFINDDIALOG_H__
#define __QFINDDIALOG_H__

#include <QDialog>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>

class QFindDialog : public QDialog
{
	Q_OBJECT

protected:
	QLabel *label;
	QLineEdit *lineEdit;
    QGroupBox *groupBox;
	QCheckBox *caseCheckBox;
	QCheckBox *backwardCheckBox;
	QPushButton *findButton;
	QPushButton *closeButton;

signals:
    void clicked(const QString &str, Qt::CaseSensitivity cs, bool direct);
	
protected slots:
    void onTextChanged(const QString &text);
    void onClickedFindButton();
	
protected:
    QFindDialog(QWidget *parent = NULL);
    bool construct();
    bool initFindDialog();
    bool initLeftLayout(QHBoxLayout *layout);
    bool initGroupBox();
    bool initRightLayout(QHBoxLayout *layout);
	
public:
    static QFindDialog* NewInstance(QWidget *parent = NULL);
    virtual ~QFindDialog();
};

#endif
//下面是QFindDialog的实现文件
#include "QFindDialog.h"

void QFindDialog::onTextChanged(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}

void QFindDialog::onClickedFindButton()
{
    emit clicked(lineEdit->text(), caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive, !backwardCheckBox->isChecked());
}

QFindDialog::QFindDialog(QWidget *parent) : QDialog(parent)
{
	label = NULL;
	lineEdit = NULL;
    groupBox = NULL;
	caseCheckBox = NULL;
	backwardCheckBox = NULL;
	findButton = NULL;
	closeButton = NULL;

    setWindowTitle("查找");
    resize(500, 250);
}

bool QFindDialog::construct()
{
	bool ret = true;
	
	ret = ret && (label = new QLabel(this));
	ret = ret && (lineEdit = new QLineEdit(this));
    ret = ret && (groupBox = new QGroupBox(this));
	ret = ret && (caseCheckBox = new QCheckBox(this));
	ret = ret && (backwardCheckBox = new QCheckBox(this));
	ret = ret && (findButton = new QPushButton(this));
	ret = ret && (closeButton = new QPushButton(this));
	
	if(ret)
	{
        label->setText("查找目标(&F):");
		label->setBuddy(lineEdit);

        lineEdit->clear();
        connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));

        ret = ret && initGroupBox();

        caseCheckBox->setText("匹配大小写");
        caseCheckBox->setChecked(true);

        backwardCheckBox->setText("反向查找");
        backwardCheckBox->setChecked(false);

        findButton->setText("查找");
        findButton->setDefault(true);
        findButton->setEnabled(false);
        connect(findButton, SIGNAL(clicked()), this, SLOT(onClickedFindButton()));

        closeButton->setText("关闭");
        connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

        ret = ret && initFindDialog();
    }
	
	return ret;
}

bool QFindDialog::initFindDialog()
{
    bool ret = true;
    QHBoxLayout *topLayout = new QHBoxLayout();

    ret = ret && topLayout;

    if(ret)
    {
        topLayout->setSpacing(4);
        //topLayout->setParent(this);

        ret = ret && initLeftLayout(topLayout);
        ret = ret && initRightLayout(topLayout);
        
        setLayout(topLayout);
        //qDebug() << "topLayout->parent = " << topLayout->parent();
    }

    return ret;
}

bool QFindDialog::initLeftLayout(QHBoxLayout *layout)
{
    bool ret = true;
    QVBoxLayout *leftLayout = NULL;
    QHBoxLayout *hLayout = NULL;
    ret = ret && (leftLayout = new QVBoxLayout());
    ret = ret && (hLayout = new QHBoxLayout());

    if(ret)
    {
        leftLayout->setSpacing(4);
        //leftLayout->setParent(layout);

        hLayout->setSpacing(4);
        //hLayout->setParent(leftLayout);
        hLayout->addWidget(label, 1, Qt::AlignCenter);
        hLayout->addWidget(lineEdit, 1, Qt::AlignCenter);

        leftLayout->addLayout(hLayout, 1);
        leftLayout->addWidget(groupBox, 2);

        layout->addLayout(leftLayout, 1);
        //qDebug() << "leftLayout->parent = " << leftLayout->parent();
    }

    return ret;
}

bool QFindDialog::initGroupBox()
{
    bool ret = true;
    QVBoxLayout *vLayout = new QVBoxLayout();
    ret = ret && vLayout;

    if(ret)
    {
        vLayout->setSpacing(4);
        vLayout->setContentsMargins(20, 0, 20, 0);
        vLayout->addWidget(backwardCheckBox, 1, Qt::AlignLeft);
        vLayout->addWidget(caseCheckBox, 1, Qt::AlignLeft);

        groupBox->setLayout(vLayout);
    }

    return ret;
}

bool QFindDialog::initRightLayout(QHBoxLayout *layout)
{
    bool ret = true;
    QVBoxLayout *rightLayout = new QVBoxLayout();

    ret = ret && rightLayout;

    if(ret)
    {
        rightLayout->setSpacing(4);
        //rightLayout->setParent(layout);
        rightLayout->addWidget(findButton, 1, Qt::AlignCenter);
        rightLayout->addWidget(closeButton, 1, Qt::AlignCenter);
        rightLayout->addStretch(5);

        layout->addLayout(rightLayout, 1);
    }

    return ret;
}

QFindDialog* QFindDialog::NewInstance(QWidget *parent)
{
	QFindDialog *ret = new QFindDialog(parent);
	
    if(ret == NULL || !ret->construct())
	{
		delete ret;
		ret = NULL;
	}
	
	return ret;
}

QFindDialog::~QFindDialog()
{
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【资源介绍】 期末大作业项目--高分 基于QTC++实现的火车票管理系统源码+项目说明.zip 登录窗口 * 这里你可以选择乘客登录或者管理员登录 * **乘客登录窗口** 你需要输入乘客的姓名和身份证号(姓名可以是文字或数字,身份证号则必须是数字)。如果你曾经登录过,那么你输入的身份证号和姓名必须和第一次登录时一致。如果你第一次登录,则根据你的身份证号和姓名创建新的用户,但如果你未执行购票操作的话则不会在数据库中记录你的名字和身份证号。 点击取消退出程序。 * **管理员登录窗口** 你需要输入获知的管理员账号和密码。 点击取消退出程序。 乘客端 * 左侧顶部显示了你的姓名和身份证号,左侧中部是你可以执行的操作按钮,左侧下方默认显示购票界面,右侧是你的购票数据。点击购票按钮、退票按钮或改签按钮,左侧下方的界面会随即变更。点击退出按钮,回到登录窗口。 * **购票界面** 你需要在下拉框中选择始发站和终点站,城市会随着省份的变化而变化,目前只有河北省和北京市的若干城市可供选择(默认的北京市在城市一栏不会显示北京,你需要先在省份一栏中选择河北省,再选回北京市,这时城市一栏中才会显示出北京)。 之后你还需要选择席别,有硬座、卧铺、站票和高铁可选,程序会根据席别自动确定单张票价,依次是150、300、50和500元。 然后选择购票张数,你只被允许在1至5内进行选择。 最后在日历中选择您的乘车日期,注意只能选择今天或今天之后的日期。 全部选择好后你可以点击显示价格来查看价格,价格是根据席别和购票张数的乘积进行计算的,与站点无关 确认无误后点击购票购买,即可在右侧的表格中查看购票记录 购票时间是你购票的日期,由系统自动获取 * **退票界面** 首先会弹出一个对话框进行引导操作。在输入框内输入购票记录的序号,再点击退票。这时会弹出一个对话框确认删除,注意退票不可恢复。 * **改签界面** 首先会弹出一个对话框进行引导操作。先在右侧表格中选择你要改签车票的始发时间单元格,再在左边日历中选择你想改签的日期,点击提交即可。注意改签日期必须是今天或今天之后的日期。 管理员端 * 左侧顶部是你可以执行的操作按钮,左侧下方默认显示添加界面,右侧是所有的购票数据。点击添加、删除、查询或统计按钮,左侧下方的界面会随即变更。点击恢复、保存或修改按钮,会弹出对话框指引操作或进行提示。点击返回按钮,回到登录窗口。 添加界面 和乘客端操作基本一致,在此基础上你需要输入乘客的姓名和身份证号,还删去了显示价格的按钮。 删除界面 和乘客端操作基本一致,但是删除后只要没有保存都可以恢复。如果需要真的在数据库里删除,需要点击保存按钮。 恢复按钮 点击即可恢复删除的数据。 查询按钮 在下拉框中你可以选择按身份证号查找或按姓名查找。然后在输入框中输入身份证号或姓名,点击查询即在右面的表格中显示查询结果。如果没有对应的数据,则会弹出对话框进行提示。点击显示全部即可查看所有数据。 修改按钮 在右侧表格中进行修改,再点击保存。 保存按钮 点击即可在数据库中保存数据。 统计按钮 在日历中选择日期,再点击统计。前两行显示的是你选择的那天的购票数量和总价格。在统计单价的下拉框中你可以选择要进行统计的票价区间。点击统计后,就会显示你选择的票价区间的购票张数和总价格。 【备注】 该项目是个人毕设/课设/大作业项目,代码都经过本地调试测试,功能ok才上传,高分作品,可快速上手运行!欢迎下载使用,可用于小白学习、进阶。 该资源主要针对计算机、通信、人工智能、自动化等相关专业的学生、老师或从业者下载使用,亦可作为期末课程设计、课程大作业、毕业设计等。 项目整体具有较高的学习借鉴价值!基础能力强的可以在此基础上修改调整,以实现不同的功能。 欢迎下载使用,也欢迎交流学习~

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值