qt 伸展按钮的例子

Extension Example

/****************************finddialog.h****************************/

#ifndef FINDDIALOG_H

#define FINDDIALOG_H

#include <QDialog>

QT_BEGIN_NAMESPACE

//提前声明要用到的类

class QCheckBox;

class QDialogButtonBox;

class QGroupBox;

class QLabel;

class QLineEdit;

class QPushButton;

QT_END_NAMESPACE

//! [0]

class FindDialog : public QDialog

{

Q_OBJECT

public:

FindDialog(QWidget *parent = 0);

private:

QLabel *label;

QLineEdit *lineEdit;

QCheckBox *caseCheckBox;

QCheckBox *fromStartCheckBox;

QCheckBox *wholeWordsCheckBox;

QCheckBox *searchSelectionCheckBox;

QCheckBox *backwardCheckBox;

QDialogButtonBox *buttonBox;

QPushButton *findButton;

QPushButton *moreButton;

QWidget *extension;

};

//! [0]

#endif

/*************************finddialog.cpp**********************************/

#include <QtGui>

#include "finddialog.h"

//! [0]

FindDialog::FindDialog(QWidget *parent)

: QDialog(parent)

{

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

lineEdit = new QLineEdit;

label->setBuddy(lineEdit);

//当用户按下label指示的快捷键(shortcut key)后,

//键盘焦点转移到他的伙伴窗口lineEdit.

//Find &what:快捷键Alt+W。注意是&指定了快捷键

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

fromStartCheckBox = new QCheckBox(tr("Search from &start"));

fromStartCheckBox->setChecked(true);

//! [1]

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

findButton->setDefault(true);

//当用户按下回车键,当前默认按钮得到点击,

//而与当前对话框中那个窗口部件拥有键盘输入焦点无关。

//同一时刻只能有一个推动按钮被设置为默认按钮。

//这个按钮然后被显示有一个额外的框架(这依赖于图形用户界面风格)。

//http://www.kuqin.com/qtdocument/qpushbutton.html#default-prop

moreButton = new QPushButton(tr("&More"));

moreButton->setCheckable(true);

moreButton->setAutoDefault(false);

//当对话框接收到键盘输入焦点时,

//对话框中的自动默认按钮自动变为默认推动按钮。

//

buttonBox = new QDialogButtonBox(Qt::Vertical);//垂直分布

buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);

buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);

//! [1]

//! [2]

extension = new QWidget;

wholeWordsCheckBox = new QCheckBox(tr("&Whole words"));

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

searchSelectionCheckBox = new QCheckBox(tr("Search se&lection"));

//! [2]

//! [3]

//connect the More button's toggled() signal

//to the extension widget's setVisible() slot.

connect(moreButton, SIGNAL(toggled(bool)), extension, SLOT(setVisible(bool)));

//当按钮more被按下(toggled)拴住时,extension才显示被看见(Visible)

QVBoxLayout *extensionLayout = new QVBoxLayout;

extensionLayout->setMargin(0);//设置组件的边框和它的文本或子部件之间的空白(margin)。

extensionLayout->addWidget(wholeWordsCheckBox);

extensionLayout->addWidget(backwardCheckBox);

extensionLayout->addWidget(searchSelectionCheckBox);

extension->setLayout(extensionLayout);

//extension使用布局管理器extensionLayout

//! [3]

//! [4]

//topLeftLayout = label + lineEdit

QHBoxLayout *topLeftLayout = new QHBoxLayout;

topLeftLayout->addWidget(label);

topLeftLayout->addWidget(lineEdit);

//leftLayout = topLeftLayout + caseCheckBox + fromStartCheckBox

QVBoxLayout *leftLayout = new QVBoxLayout;

leftLayout->addLayout(topLeftLayout);

leftLayout->addWidget(caseCheckBox);

leftLayout->addWidget(fromStartCheckBox);

leftLayout->addStretch(1);

//mainLayout = leftLayout + buttonBox + extension

QGridLayout *mainLayout = new QGridLayout;

mainLayout->setSizeConstraint(QLayout::SetFixedSize);//设置为固定大小(FixedSize)

mainLayout->addLayout(leftLayout, 0, 0);

mainLayout->addWidget(buttonBox, 0, 1);

mainLayout->addWidget(extension, 1, 0, 1, 2);//跨越一行两列

setLayout(mainLayout);

setWindowTitle(tr("Extension"));//设置标题

//! [4] //! [5]

extension->hide();//extension默认隐藏

}

//! [5]

/*****************************main.cpp***************************************/

#include <QApplication>

#include "finddialog.h"

int main(int argc, char *argv[])

{

QApplication app(argc, argv);

FindDialog dialog;

return dialog.exec();

}

/*************************************************************************/

你可能感兴趣的:(框架,qt)