Qt学习第二天

源代码及注释
头文件:finddialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H
 
 
#include<QDialog>

//一下数行前置声明了一些要用到的类,之所以用前置声明,是因为这样可以编译速度加快
 
 
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

//子类化QDialog,以下是类的定义
 
 
class FindDialog:public QDialog
{
    Q_OBJECT             //因为用到了信号和槽,因此必须包含Q_OBJECT宏定义
 
 
public:
    FindDialog(QWidget *parent=0);   //构造函数,且指定该类没有父对象

//声明了一些信号,Qt
::CaseSensitivity cs设置大小写字母的敏感性
signals:
    void findNext(const QString &str,Qt::CaseSensitivity cs);
    void findPrevious(const QString &str,Qt::CaseSensitivity cs);

//声明了一些槽函数
private slots:
    void findClicked();
    void enableFindButton(const QString &text);

//声明了一些指针,分别指向不同的对象
private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};
 
 
#endif // FINDDIALOG_H


实现文件:

//QtGui头文件中包含了很多类,基本上只要包含了该头文件,就可以省去很多类的声明
#include<QtGui>
#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent)
    :QDialog(parent)
{
    label=new QLabel(tr("Find &What:"));//&号用来设置快捷键,当按下Alt+W时,就可以选中
    lineEdit=new QLineEdit();
    label->setBuddy(lineEdit);          //设置伙伴关系

    caseCheckBox=new QCheckBox(tr("Match &Case"));
    backwardCheckBox=new QCheckBox(tr("Search &Backward"));
    findButton=new QPushButton(tr("&Find"));

    findButton->setDefault(true);
    findButton->setEnabled(false);     //使其变灰,即不可以被按
    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()));
    //水平布局管理器
    QHBoxLayout *topLeftLayout=new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);
    //垂直布局管理器
    QVBoxLayout *leftLayout=new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    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);
    setWindowTitle(tr("Find"));   //设置窗口的标题
    setFixedHeight(sizeHint().height()); //设置窗口的高度,
sizeHint().height()返回理想的高度
}
void FindDialog::findClicked()
{
    QString text=lineEdit->text();
    //C++中的三元运算符
    Qt::CaseSensitivity cs=caseCheckBox->isChecked()?Qt::CaseInsensitive:Qt::CaseInsensitive;
    if(backwardCheckBox->isChecked())
    {
        emit findPrevious(text,cs);  //发射信号
    }
    else
    {
        emit findNext(text,cs);
    }
}
void FindDialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());   //设置findButton的活性
}


主函数:

#include<QApplication>
#include "finddialog.h"
int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    FindDialog *dialog=new FindDialog;
    dialog->show();
    return app.exec();
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值