QT学习笔记(c++ GUI编程)find对话框

find对话框是c++ GUI QT4编程里的内容,之前已经手敲了一遍,

现在准备加深理解。

1.需要的窗口部件

以Dialog为主窗口,需要按钮,复选框,标签,先对这先需要使用的部件进行声明

#include<QDialog>
class QPushButton;
class QLabel;
class QCheckBox;
class QLineEdit;//单行文本编辑框

2.创建find窗口的类

class Find
{
public:
    Find(QWidget * parent=0);//构造函数,表示默认Find是一个父窗口
    ~Find();//析构函数 
private:
    QPushButton *FindButton;
    QPushButton *closeButton;
    QLabel *label;
    QLineEdit *lineedit;
    QCheckBox *caseCheckBox;
    QCheckBox *matchcaseBox;
    
};

3.跟据需要的功能确定槽和信号

在这之前,我们需要了解QT::CaseSensitivity的一些知识

这是一种枚举类型,可取值,CaseSenSitive 和Caseinsensitive

比较字符串时CaseSenSitive区分大小写,而Caseinsensitive不支持大小写。

所以我们的复选框应该有一个,match case,当然在书中还有一个复选框

是向前查询的复选框。

所以第一个槽是当用户点击复选框时候的槽

还有如果用户勾选复选框,那么就需要发送信号,所以这里需要两个信号

public slots:
    void findClicked();
    signals:
        void findNext(const QString &ts,Qt::CaseSensitivity cs);//如果用户选择,那么发射这个信号
        void findPrevious(const QString &tr,Qt::CaseSensitivity cs);

4.具体实现

这一段代码主要是对窗口部件的命名工作

 FindButton =new QPushButton(tr("&Find"));
    closeButton =new QPushButton(tr("close "));
    setWindowTitle(tr("Find"));//为主窗口创建名字
    label =new QLabel(tr("Find &What"));//这里把what设为快捷键
    lineedit =new QLineEdit;
    label->setBuddy(lineedit);//这里把label表示为输入框的伙伴,当使用快捷键时,自动把焦点跳到对话框上
    caseCheckBox= new QCheckBox(tr("search &case"));
    matchcaseBox= new QCheckBox(tr("match case"));
    FindButton->setDefault(true);//设置find为默认按钮
    FindButton->setEnabled(false);//当输入框里没有文字时自动禁用finf按钮
    

这一段主要是需要实现的操作

 connect(lineedit,SIGNAL(textChanged(const QString &text)),this,SLOT(buttonenable(const QString &str)));
    connect(FindButton,SIGNAL(clicked()),this,SLOT(findClicked()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

下面是自定义信号和槽

void Find::findClicked()
{
    QString Text=lineedit->text();
    Qt::CaseSensitivity cs= matchcaseBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;//检测是否选中
    if(caseCheckBox->isChecked())
        emit findNext(Text,cs);
    else {
        emit findPrevious(Text,cs);
    }
    
}
void Find::buttonenable(const QString &str)
{
    FindButton->setEnabled(!str.isEmpty());//如果不为空就使用按钮
}

最后我们就要实现布局

5.布局

QHBoxLayout :水平布局

QVBoxlayout  :垂直布局

QGridLayout  :格子显示布局

QHBoxLayout *toplelayout=new QHBoxLayout;
    toplelayout->addWidget(label);
    toplelayout->addWidget(lineedit);
    QVBoxLayout *leftlayout=new QVBoxLayout;
    leftlayout->addLayout(toplelayout);
    leftlayout->addWidget(matchcaseBox);
    leftlayout->addWidget(caseCheckBox);
    QVBoxLayout *rightlayout=new QVBoxLayout;
    rightlayout ->addWidget(FindButton);
    rightlayout->addWidget(closeButton);
    QHBoxLayout *mainlayout=new QHBoxLayout;
    mainlayout->addLayout(leftlayout);
    mainlayout->addLayout(rightlayout);
    setLayout(mainlayout);//使用mainlayout布局
    setFixedHeight(sizeHint().height());//setFixedHight把最小和最大尺寸都设为s防止窗口不断变大或变小

运行结果如下

运行效果

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值