注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学习笔记

正确的方法如同学习书法,开始的时候要临摹,临摹好了然后创造自己的风格。

 
 
 

日志

 
 

[Qt笔记]对话框-派生类对话框  

2011-09-06 22:53:51|  分类: QT |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

目录

        实现查找对话框,我们把这个对话框实现为一个类,这样它就是一个独立的控件,拥有自己的信号和槽。下面是对话框的层次布局:

[Qt笔记]对话框-派生类对话框 - Fantity Wei - FootPrints

 
FindDialog
|--------QHBoxDialog(mainLayout)
                       |--------QHBoxDialog(leftLayout)
                                                 |--------QHBoxDialog(topLeftLayout) : Qlabel label && QlineEdit lineEdit
                                                 |--------QCheckBox caseCheckBox && QCheckBox backwardCheckBox
                       |--------QHBoxDialog(rightLayout): QPushButton findButton && QPushButton closeButton && addStretch


//finddialog.h

#ifndef FINDDIALOG_H

#define FINDDIALOG_H

 

#include <QDialog>

 

class QCheckBox;

class QLabel;

class QLineEdit;

class QPushButton;

 

class FindDialog: public QDialog

{

    Q_OBJECT

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_H

 

//finddialog.cpp

#include <QtGui>

#include "finddialog.h"

 

FindDialog::FindDialog(QWidget *parent):QDialog(parent)

{

    label=new QLabel(tr("Find &what:"));

    lineEdit=new QLineEdit;

    label->setBuddy(lineEdit);   // 设置设定了这个标签的友好控件为文本框,见Reference 1

    caseCheckBox=new QCheckBox(tr("Match &case"));

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

    findButton=new QPushButton(tr("&Find"));

    findButton->setDefault(true);       // 见Reference 2

    findButton->setEnabled(false);  // 设置find按钮是否为灰色,不可按

    closeButton=new QPushButton(tr("Close"));

 

    QObject::connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(enableFindButton(QString)));

    QObject::connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));

    QObject::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();  // 见Reference 3

    QHBoxLayout *mainLayout=new QHBoxLayout;

    mainLayout->addLayout(leftLayout);

    mainLayout->addLayout(rightLayout);

    setLayout(mainLayout);
   

    setWindowTitle(tr("Find")); // 见Reference 4

    setFixedHeight(sizeHint().height());        // 见Reference 4

}

 

void FindDialog::findClicked()

{

    QString text=lineEdit->text();

    Qt::CaseSensitivity cs=caseCheckBox->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());

}

 

//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();

Reference:

        A button with this property set to true (i.e., the dialog's default button,) will automatically be pressed when the user presses enter, with one exception: if an autoDefault button currently has focus, the autoDefault button is pressed. When the dialog has autoDefault buttons but no default button, pressing enter will press either the autoDefault button that currently has focus, or if no button has focus, the next autoDefault button in the focus chain.

        In a dialog, only one push button at a time can be the default button. This button is then displayed with an additional frame (depending on the GUI style).

        The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus.

        If the default property is set to false on the current default button while the dialog is visible, a new default will automatically be assigned the next time a pushbutton in the dialog receives focus.

        This property's default is false.

        Access functions:

bool isDefault () const

void setDefault ( bool )

 

运行后的效果:

初始效果

[Qt笔记]对话框-派生类对话框 - Fantity Wei - FootPrints
 

 输入要查找的字符后的效果,Find由灰色变为可操作

[Qt笔记]对话框-派生类对话框 - Fantity Wei - FootPrints
 勾选可选项之后
[Qt笔记]对话框-派生类对话框 - Fantity Wei - FootPrints
  

Reference:
1. 所谓友好控件,就是说当标签的快捷键被按下时,该标签的友好控件就会得到焦点。

2. A button with this property set to true (i.e., the dialog's default button,) will automatically be pressed when the user presses enter, with one exception: if an autoDefault button currently has focus, the autoDefault button is pressed. When the dialog has autoDefault buttons but no default button, pressing enter will press either the autoDefault button that currently has focus, or if no button has focus, the next autoDefault button in the focus chain.

        In a dialog, only one push button at a time can be the default button. This button is then displayed with an additional frame (depending on the GUI style).

        The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus.

        If the default property is set to false on the current default button while the dialog is visible, a new default will automatically be assigned the next time a pushbutton in the dialog receives focus.

        This property's default is false.

        Access functions:

bool isDefault () const

void setDefault ( bool )

3. Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.


4. 为对话框的窗体设置标题和准确的高度,使得所有控件都不会占据额外控件,这样会更加紧凑。QWidget::sizeHint()方法会返回一个控件的理想尺寸。QWidget::sizeHint()方法会返回一个控件的理想尺寸。

  评论这张
 
阅读(1476)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017