第三讲 对话框的创建

本文概要:简要讲述对话框的概念已经分类以及如何通过Qt编写一个简单的对话框程序

// finddialog.h 头文件
#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
    Q_OBJECT  // Q_OBJECT为Qt的宏,定义了信号与槽的类必须含有该宏

public:
    FindDialog(QWidget *parent = 0);

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.cpp
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
    label = new QLabel(tr("Find &what:")); // 设置快捷键 Alt+W
    lineEdit = new QLineEdit;
    // 设置lineEdit为label的伙伴,当按下快捷时,程序的焦点会转移到lineEdit上
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));  // 设置快捷键 Alt+C
    backwardCheckBox = new QCheckBox(tr("Search &backward")); // 设置快捷键 Alt+B

    findButton = new QPushButton(tr("&Find"));
    // 设置findButton为整个对话框(只有在对话框中才有默认按钮)的默认按钮
    // (那么如果用户在对话框中敲击回车键,这个推动按钮将被按下)
    findButton->setDefault(true);  
    // 禁用按钮,当禁用时不会与用户发生交换,显示为灰色
    findButton->setEnabled(false); 
    closeButton = new QPushButton(tr("Close"));

    /* 设置信号与槽的连接 */
    // 设置lineEdit与findButton之间的通信
    connect(lineEdit, SIGNAL(textChanged(const QString &)),
        this, SLOT(enableFindButton(const QString &)));
    // 当findButton被点击时,调用findClicked()函数
    connect(findButton, SIGNAL(clicked()),
        this, SLOT(findClicked()));
    // 当点击closeButton按钮时调用close()函数,隐藏对话框
    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"));
    // 设置高度固定且高度为Qt建议的大小,在QtWidget类中含有建议大小属性 
    setFixedHeight(sizeHint().height()); 
}

void FindDialog::findClicked()
{
    // 该函数获取lineEdit的text与caseCheckBox的选项设置,
    // 再根据backwardCheckBox的选项来选择发送向前查找或者
    // 向后查找信号
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs =
        caseCheckBox->isChecked() ? Qt::CaseSensitive
        : Qt::CaseInsensitive;
    // emit为Qt的关键字,为Qt的扩展语法,表示发送信号的含义
    // 在这里可能会发送findPrevious或者findNext信号,但是
    // 由于没有设置相应的槽接受,所以不会调用其他函数
    if (backwardCheckBox->isChecked())
        emit findPrevious(text, cs);
    else
        emit findNext(text, cs);
}

void FindDialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}
// main.cpp
#include <QApplication>
#include "finddialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    FindDialog *dialog = new FindDialog;
    dialog->show();
    return app.exec();
}

 

程序运行效果图:

  

 


对话框:

  • 概念:在图形用户界面中,对话框是一种特殊的视窗, 用来在用户界面中向用户显示信息,或者在需要的时候获得用户的响应。之所以称之为“对话框”是因为它们使计算机和用户之间构成了一个对话——或者是通知用户一些信息,或者是请求用户的输入,或者两者皆有。
  • 分类
    • 无模式对话框:非强制性回应对话框(如notepad++的查找对话框),用于向用户请求非必须资料。即可以不理会这种对话框或不向其提供任何信息而继续进行当前工作,所以窗口均可打开并处于活动状态或是获得焦点(人机交互热点)。一类无模式对话框表现为工具栏,比如设置用于文字颜色的设置。查找/替换对话框可算是无模式对话框,也可以设计为工具栏。
    • 有模式对话框
      特点:这种对话框强制要求用户回应,否则用户不能再继续进行操作,直到与该对话框完成交互。这种对话框设计用于需要一些额外的信息,然后才可以继续进行的操作,或可能只想确认使用者想要进行一项具有潜在危险性的操作。有模式对话框一般被视为坏的设计方案,因为以一种用户不喜欢看到方式出现,或是被习惯不去注意对话框提示的用户忽略,导致无法避免危险操作。
    • 有模式对话框分类:有模式对话框一般分为系统级和应用程序级。

                                    系统级有模式对话框出现时,用户在完成与这个对话框交互之前不能进行其它操作,比如关机对话框、Windows Vista 中的 UAC。

                                    应用程序级有模式对话框则只对它的母程序有所限制,但可能在允许运行多个实际的不同软件中有不同的表现:只限定其中的一个

                                    程序窗口使之无法操作或全部限定。

 

Qt中对象的new与delete:

在Qt中,当删除一个父对象时,会自动删除其子对象,不需要在父对象的析构函数中显式的删除其子对象。在该例程中,QPushButton类,QCheckBox类的子窗口部件与QHBoxLayout,QVBoxLayout的布局管理器子对象被new创建,但是没有在析构函数中显式delete,Qt会在删除finddialog对象时自动删除作为其子孙的窗口部件与子布局管理器。

 

Tab键的顺序:

在Qt中,Tab键的顺序为创建窗口部件的顺序,并且可以通过显式的调用QWidget::setTabOrder()来改变

posted @ 2015-04-12 09:37  Leptus  阅读(408)  评论(0编辑  收藏  举报