惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
月光博客
月光博客
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
腾讯CDC
B
Blog RSS Feed
博客园 - Franky
爱范儿
爱范儿

博客园 - 云编程的梦

OpenGL超级宝典笔记5 C++读写XML文件(libxml2库) libxml2对XML文件的创建、解析、查找、修改 [置顶] 我的设计模式学习笔记------>单例模式(Singleton) libxml2.7.8 c++ 解析xml文件 Head First设计模式-模板方法模式 嵌入式开发之工具移植--openssl移植 嵌入式开发之工具移植--wpa_suppliant工具的移植和使用 linux下嵌入式wifi开发工具移植 IOS面试题(英文) java提高篇-----理解java的三大特性之继承 staruml使用教程 黑马程序员:HTML习题1 Cocos2d-x-->CCSprite 动画 地址栏传参中文乱码详解 Lync 2010 升级到2013 之部署2013前端服务器! 100层楼摔鸡蛋问题 Lync 2010升级到Lync 2013之更新CU2! eCos启动过程详解,基于Cortex-M架构
Qt学习第二天
云编程的梦 · 2013-10-17 · via 博客园 - 云编程的梦

源代码及注释

头文件: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();
}