动态布局的例子,dynamic layout;包括滑块,进度条。刻度盘,旋转框等子控件。
//**************************************************************
#include <QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog dialog;
return dialog.exec();
}
//*****************************************************************
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QQueue>
QT_BEGIN_NAMESPACE
class QComboBox;
class QDialogButtonBox;
class QGridLayout;
class QGroupBox;
class QLabel;
class QPushButton;
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
private slots:
void buttonsOrientationChanged(int index);
void rotateWidgets();
void help();
private:
void createRotableGroupBox();
void createOptionsGroupBox();
void createButtonBox();
QGroupBox *rotableGroupBox;
QQueue<QWidget *> rotableWidgets;//控件队列,实现控件循环显示
QGroupBox *optionsGroupBox;
QLabel *buttonsOrientationLabel;
QComboBox *buttonsOrientationComboBox;
QDialogButtonBox *buttonBox;
QPushButton *closeButton;
QPushButton *helpButton;
QPushButton *rotateWidgetsButton;
QGridLayout *mainLayout;
QGridLayout *rotableLayout;
QGridLayout *optionsLayout;
};
#endif
//**********************************************************
/****************************************************************************/
#include <QtGui>
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
createRotableGroupBox();
createOptionsGroupBox();
createButtonBox();
mainLayout = new QGridLayout;
mainLayout->addWidget(rotableGroupBox, 0, 0);
mainLayout->addWidget(optionsGroupBox, 1, 0);
mainLayout->addWidget(buttonBox, 2, 0);
setLayout(mainLayout);
mainLayout->setSizeConstraint(QLayout::SetMinimumSize);
setWindowTitle(tr("Dynamic Layouts"));
}
void Dialog::buttonsOrientationChanged(int index)
{
////布局的大小限制(Constraint)模式:无限制(NoConstraint)
mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
setMinimumSize(0, 0);
//获取方位(orientation)信息
Qt::Orientation orientation = Qt::Orientation(
buttonsOrientationComboBox->itemData(index).toInt());//转换成整数
if (orientation == buttonBox->orientation())//与原值一致则直接返回
return;
mainLayout->removeWidget(buttonBox);//删除按钮框
int spacing = mainLayout->spacing();//如果横向和纵向长度一致返回这个长度,否则返回-1
QSize oldSizeHint = buttonBox->sizeHint() + QSize(spacing, spacing);
buttonBox->setOrientation(orientation);//设置按钮框的布局方向(Orientation)
QSize newSizeHint = buttonBox->sizeHint() + QSize(spacing, spacing);
if (orientation == Qt::Horizontal) {
mainLayout->addWidget(buttonBox, 2, 0);
resize(size() + QSize(-oldSizeHint.width(), newSizeHint.height()));
} else {
mainLayout->addWidget(buttonBox, 0, 3, 2, 1);
resize(size() + QSize(newSizeHint.width(), -oldSizeHint.height()));
}
mainLayout->setSizeConstraint(QLayout::SetDefaultConstraint);
}
void Dialog::rotateWidgets()
{
Q_ASSERT(rotableWidgets.count() % 2 == 0);//断言:偶数个子部件
//foreach宏的实质就是在宏中定义循环申明部分,而不包括循环体。
//在使用的时候,紧接着写循环体就行了。
//foreach(variables ,container)关键字是Qt对c++的一个扩展,
// 主要用于按顺序历经容器(container)中的对象
foreach (QWidget *widget, rotableWidgets)
rotableLayout->removeWidget(widget);//删除子部件
rotableWidgets.enqueue(rotableWidgets.dequeue());//头部部件出队列后入队尾
const int n = rotableWidgets.count();
for (int i = 0; i < n / 2; ++i) {//变换控件显示的位置
rotableLayout->addWidget(rotableWidgets[n - i - 1], 0, i);
rotableLayout->addWidget(rotableWidgets[i], 1, i);
}
}
//帮助对话框
void Dialog::help()
{
QMessageBox::information(this, tr("Dynamic Layouts Help"),
tr("This example shows how to change layouts "
"dynamically."));
}
void Dialog::createRotableGroupBox()
{
rotableGroupBox = new QGroupBox(tr("Rotable Widgets"));
rotableWidgets.enqueue(new QSpinBox);//旋转框入队
rotableWidgets.enqueue(new QSlider);//滑块
rotableWidgets.enqueue(new QDial);//刻度盘
rotableWidgets.enqueue(new QProgressBar);//进度条
int n = rotableWidgets.count();//返回子部件数
for (int i = 0; i < n; ++i) {
connect(rotableWidgets[i], SIGNAL(valueChanged(int)),
rotableWidgets[(i + 1) % n], SLOT(setValue(int)));
}//四个部件首尾相连,关系是一个环形,一个改变便会改变其它几个
rotableLayout = new QGridLayout;
rotableGroupBox->setLayout(rotableLayout);
rotateWidgets();
}
void Dialog::createOptionsGroupBox()
{
optionsGroupBox = new QGroupBox(tr("Options"));//groupbox周围显示一个圈
buttonsOrientationLabel = new QLabel(tr("Orientation of buttons:"));
buttonsOrientationComboBox = new QComboBox;//Combo : 结合物, 联合体.下拉菜单
buttonsOrientationComboBox->addItem(tr("Horizontal"), Qt::Horizontal);//item:条目
buttonsOrientationComboBox->addItem(tr("Vertical"), Qt::Vertical);
//void QComboBox::addItem ( const QString & text,
// const QVariant & userData = QVariant() )
//Combo中的索引值(index)改变的信号连接到信号槽buttonsOrientationChanged(int)
connect(buttonsOrientationComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(buttonsOrientationChanged(int)));
optionsLayout = new QGridLayout;
optionsLayout->addWidget(buttonsOrientationLabel, 0, 0);
optionsLayout->addWidget(buttonsOrientationComboBox, 0, 1);
optionsLayout->setColumnStretch(2, 1);//设置第二列的延伸属性(stretch factor)
optionsGroupBox->setLayout(optionsLayout);
}
void Dialog::createButtonBox()
{
buttonBox = new QDialogButtonBox;
closeButton = buttonBox->addButton(QDialogButtonBox::Close);//标准关闭按钮
helpButton = buttonBox->addButton(QDialogButtonBox::Help);//标准帮助按钮
rotateWidgetsButton = buttonBox->addButton(tr("Rotate &Widgets"),
QDialogButtonBox::ActionRole);
connect(rotateWidgetsButton, SIGNAL(clicked()), this, SLOT(rotateWidgets()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(helpButton, SIGNAL(clicked()), this, SLOT(help()));
}