find-dialog

转自(稍作修改):http://devbean.blog.51cto.com/448512/193918

查找对话框

首先新建一个工程,就叫FindDialog!
当然还是Qt Gui Application,然后最后一步注意,Base Dialog选择QDialog,而不是默认的QMainWindow,因为我建立对话框,名字就叫finddialog,Ganarate form还是不要的。然后Finish就好了。

打开finddialog.h,开始编写头文件。

#ifndef FIND_DIALOG_H
#define FIND_DIALOG_H

#include <QDialog>

class FindDialog:public QDialog
{
    Q_OBJECT

public:
    FindDialog(QWidget *parent = 0);
    ~FindDialog();

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 // FIND_DIALOG_H

说明

FindDialog,继承自QDialog。
Q_OBJECT。这是一个宏。凡是定义信号槽的类都必须声明这个宏。
然后是一个signal:这是Qt的关键字——Qt扩展了C++语言,因此它有自己的关键字——这是对信号的定义,也就是说,FindDialog有两个public的信号,它可以在特定的时刻发出这两个信号,如果用户点击了Find按钮,并且选中了Search backward,就会发出findPrevious(),否则发出findNext()。

紧接着是private slots:的定义,和前面的signal一样,这是私有的槽的定义。也就是说,FindDialog具有两个槽,可以接收某些信号,不过这两个槽都是私有的。
为了slots的定义,我们需要访问FindDialog的组件,因此,我们把其中的组件定义为成员变量以便访问。
正是因为需要定义这些组件,才需要对它们的类型前向声明。因为我们仅仅使用的是指针,并不涉及到这些类的函数,因此并不需要include它们的头文件——当然,你想直接引入头文件也可以,不过那样的话编译速度就会慢一些。

find_dialog.c

#include "find_dialog.h"

#include <QtGui>

#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include<QHBoxLayout>
#include <QCheckBox>
#include <QVBoxLayout>

FindDialog::FindDialog(QWidget *parent):QDialog(parent)
{
    label=new QLabel(tr("find"));
    line_edit=new QLineEdit(this);
    label->setBuddy(line_edit);

    find_button=new QPushButton(tr("find"));
    find_button->setDefault(true);
    find_button->setEnabled(false);
    connect(find_button,SIGNAL(clicked()),this,SLOT(findClicked()));

    close_button=new QPushButton(tr("close"));
    connect(close_button,SIGNAL(clicked()),this,SLOT(close()));

    case_checkbox = new QCheckBox(tr("Match"));
    backward_checkbox = new QCheckBox(tr("Search"));
    connect(line_edit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));

    QHBoxLayout *top_left_layout=new QHBoxLayout;
    top_left_layout->addWidget(label);
    top_left_layout->addWidget(line_edit);

    QVBoxLayout *left_layout=new QVBoxLayout;
    left_layout->addLayout(top_left_layout);
    left_layout->addWidget(case_checkbox);
    left_layout->addWidget(backward_checkbox);


    QVBoxLayout *right_layout=new QVBoxLayout;
    right_layout->addWidget(find_button);
    right_layout->addWidget(close_button);
    right_layout->addStretch();
    //Adds a stretchable space (a QSpacerItem) with zero minimum size
    //and stretch factor stretch to the end of this box layout.

    QVBoxLayout *main_layout=new QVBoxLayout;
    main_layout->addLayout(left_layout);
    main_layout->addLayout(right_layout);

    this->setLayout(main_layout);

    this->setWindowTitle(tr("find"));
    this->setFixedHeight(this->sizeHint().height());

}

FindDialog::~FindDialog()
{

}

void FindDialog::findClicked()
{
        QString text = line_edit->text();
        Qt::CaseSensitivity cs = case_checkbox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
        if(backward_checkbox->isChecked()) {
                emit findPrevious(text, cs);
        } else {
                emit findNext(text, cs);
        }
}

void FindDialog::enableFindButton(const QString &text)
{
        find_button->setEnabled(!text.isEmpty());
}

main.c

#include <QApplication>
#include "find_dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FindDialog w;
    w.show();
    return a.exec();
}

说明

QtGui

Qt是分模块的,在建工程的时候就会问你,使用哪些模块?QtCore?QtGui?QtXml?等等。
QtGui包括了QtCoreQtGui模块
不过,这并不是最好的做法,因为QtGui文件很大,包括了GUI的所有组件,但是很多组件我们根本是用不到的——就像Swing的import,你可以import到类,也可以使用,不过都不会建议使用,这里也是一样的。
我们最好只引入需要的组件。不过,那样会把文件变长,现在就先用QtGui,只要记得正式开发时不能这么用就好!

tr() 国际化

函数tr()全名是QObject::tr(),被它处理的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用。
Qt的最佳实践:如果你想让你的程序国际化,那么,所有用户可见的字符串都要使用QObject::tr()!
但是,为什么我们没有写QObject::tr(),而仅仅是tr()呢?原来,tr()函数是定义在Object里面的,所有使用了Q_OBJECT宏的类都自动具有tr()函数。

字符串中的&代表快捷键

字符串中的&代表快捷键。
注意看下面的findButton的&Find,它会生成Find字符串,当你按下Alt+F的时候,这个按钮就相当于被点击——这么说很难受,相信大家都明白什么意思。同样,前面label里面也有一个&,因此它的快捷键就是Alt+W。

setBuddy函数

这个label使用了setBuddy函数,它的意思是:当label获得焦点时,比如按下Alt+W,它的焦点会自动传给它的buddy,也就是lineEdit。这就是伙伴的含义(buddy英文就是伙伴的意思)。

slot

虽说是slot,但实际上它就是普通的函数,既可以和其他函数一样使用,又可以被系统回调。

emit(发出)信号

如果backwardCheckBox被选中emit(发出)信号findPrevious(),否则emit信号findNext。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值