第二章 创建对话框 2.1子类化QDialog(2)

  现在来看类的源文件

finddialog.cpp

#include<QtGui> //包括了很多,但是很大,不是很好的编程风格
#include"finddialog.h"
FindDialog::FindDialog(QWidget *parent)
             :QDialog(parent)     //构造函数 C++知识 把parent 参数传递给基类的构造函数
                                        {
                label=new QLabel(tr("Find &what")); //tr()函数的处理过的字符串可以使用工具提取出来翻译成其他语言
                lineEdit =new QLineEdit;  //行编辑器部件
                label->setBuddy(lineEdit);      // 设置伙伴     字符串中的&代表快捷键   用快捷键激活时,焦点传递给lineEdit

                caseCheackBox=new QCheckBox(tr("Match &case"));
                backwardCheckBox=new QCheckBox(tr("Search &backward"));

                findButton=new QPushButton(tr("&Find"));
                findButton->setDefault(true); //该函数让find按钮成为默认按钮 ,默认就是enter键下的按钮
                findButton->setEnabled(false);//禁用find按钮 灰色

                closeButton=new QPushButton(tr("close"));
                 //三个连接信号槽
                connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString&)));
                connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
                connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));//close 从QWideget继承过来,默认从用户视野隐藏

            //下面的布局管理器 可以从下网上看,先给我们的主类加上一个mainLayout,在mainLayout的左边加上一个leftLayout,
                //右边加上一个rightLayout, 一次在分别网上加,知道所有私有对象全部得到布局
            QHBoxLayout *topLeftLayout=new QHBoxLayout;
            topLeftLayout->addWidget(label);
            topLeftLayout->addWidget(lineEdit);

            QVBoxLayout *leftLayout=new QVBoxLayout;
            leftLayout->addLayout(topLeftLayout);
            leftLayout->addWidget(caseCheackBox);
            leftLayout->addWidget(backwardCheckBox);


            QVBoxLayout *rightLayout=new QVBoxLayout;
            rightLayout->addWidget(findButton);
            rightLayout->addWidget(closeButton);
            rightLayout->addStretch();

            QHBoxLayout *mainLayout =new QHBoxLayout; //主布局
            mainLayout->addLayout(leftLayout);//添加左
            mainLayout->addLayout(rightLayout);//添加右
            setLayout(mainLayout);//安装在FindDialog中

setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());//返回一个理想的尺寸大小

                                        }

//对话框中所用到的槽
void FindDialog::findClicked()
{QString text =lineEdit->text();
Qt::CaseSensitivity cs=caseCheackBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;

if(backwardCheckBox->isChecked()){
    emit findPrevious(text,cs);

}
else{
    emit findNext(text,cs);
}

}


void FindDialog::enableFindButton(const QString &text){
findButton->setEnabled(!text.isEmpty());
}
在这一节对findClicked()槽的解释不是很多,这会在第三章得到应用,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值