头文件:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
//前置申明了一些将要用到的类
class QGridLayout; //网格布局
class QVBoxLayout; //垂直布局
class QPushButton; //按钮
class QSpacerItem; //分隔符
class QGroupBox; //组合框
class QLabel; //标签
class QComboBox; //下拉框
class SortDialog:public QDialog
{
Q_OBJECT
public:
SortDialog(QWidget *parent = 0); //该类的构造函数
void setColumnRange(QChar first,QChar last);
private:
QGridLayout *gridLayout; //调整整个窗口的布局
QSpacerItem *spacerItem; //右侧按钮组合框
QVBoxLayout *vboxLayout;
QPushButton *okButton;
QPushButton *cancelButton;
QPushButton *moreButton;
QGroupBox *primaryGroupBox; //左侧第一个组合框
QGridLayout *gridLayout1;
QLabel *primaryColumnLabel;
QComboBox *primaryColumnCombo;
QSpacerItem *spacerItem1;
QLabel *primaryOrderLabel;
QComboBox *primaryOrderCombo;
QSpacerItem *spacerItem2; //左侧第一个组合框和第二个组合框之间的分隔符
QGroupBox *secondaryGroupBox; //左侧第二个组合框
QGridLayout *gridLayout2;
QLabel *secondaryColumnLabel;
QComboBox *secondaryColumnCombo;
QSpacerItem *spacerItem3;
QLabel *secondaryOrderLabel;
QComboBox *secondaryOrderCombo;
QGroupBox *tertiaryGroupBox; //左侧第三个组合框
QGridLayout *gridLayout3;
QLabel *tertiaryColumnLabel;
QComboBox *tertiaryColumnCombo;
QSpacerItem *spacerItem4;
QLabel *tertiaryOrderLabel;
QComboBox *tertiaryOrderCombo;
};
#endif // DIALOG_H
实现文件:
#include<QtGui>
#include "dialog.h"
SortDialog::SortDialog(QWidget *parent)
:QDialog(parent) //将parent传给父类
{
okButton = new QPushButton(tr("OK"));
okButton->setDefault(true);
cancelButton = new QPushButton(tr("Cancel"));
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true); //默认设置为false
spacerItem = new QSpacerItem(20, 0, QSizePolicy::Minimum,QSizePolicy::Expanding);
//设置宽度固定为20,高度可变
vboxLayout = new QVBoxLayout;
vboxLayout->addWidget(okButton);
vboxLayout->addWidget(cancelButton);
vboxLayout->addItem(spacerItem);
vboxLayout->addWidget(moreButton);
primaryGroupBox = new QGroupBox(tr("&Primary Key"));
primaryColumnLabel = new QLabel(tr("Column:"));
primaryColumnCombo = new QComboBox;
primaryColumnCombo->addItem(tr("None"));
spacerItem1 = new QSpacerItem(20,20,QSizePolicy::Minimum,QSizePolicy::Expanding);
//设置宽度最小为20,高度可变
primaryOrderLabel = new QLabel(tr("Order:"));
primaryOrderCombo = new QComboBox;
primaryOrderCombo->addItem(tr("Ascending"));
primaryOrderCombo->addItem(tr("Descending"));
gridLayout1 = new QGridLayout;
gridLayout1->addWidget(primaryColumnLabel,0,0,1,1);
gridLayout1->addWidget(primaryColumnCombo,0,1,1,1);
gridLayout1->addItem(spacerItem1,0,2,1,1);
gridLayout1->addWidget(primaryOrderLabel,1,0,1,1);
gridLayout1->addWidget(primaryOrderCombo,1,1,1,2);
primaryGroupBox->setLayout(gridLayout1);
spacerItem2 = new QSpacerItem(20, 0, QSizePolicy::Minimum,QSizePolicy::Expanding);
secondaryGroupBox = new QGroupBox(tr("&Secondary Key"));
secondaryColumnLabel = new QLabel(tr("Column:"));
secondaryColumnCombo = new QComboBox;
secondaryColumnCombo->addItem(tr("None"));
spacerItem3 = new QSpacerItem(20,20,QSizePolicy::Minimum,QSizePolicy::Expanding);
secondaryOrderLabel = new QLabel(tr("Order:"));
secondaryOrderCombo = new QComboBox;
secondaryOrderCombo->addItem(tr("Ascending"));
secondaryOrderCombo->addItem(tr("Descending"));
gridLayout2 = new QGridLayout;
gridLayout2->addWidget(secondaryColumnLabel,0,0,1,1);//前两个数字代表起始位置,后两个代表长度和高度
gridLayout2->addWidget(secondaryColumnCombo,0,1,1,1);
gridLayout2->addItem(spacerItem3,0,2,1,1);
gridLayout2->addWidget(secondaryOrderLabel,1,0,1,1);
gridLayout2->addWidget(secondaryOrderCombo,1,1,1,2);
secondaryGroupBox->setLayout(gridLayout2);
tertiaryGroupBox = new QGroupBox(tr("&Tertiary Key"));
tertiaryColumnLabel = new QLabel(tr("Column:"));
tertiaryColumnCombo = new QComboBox;
tertiaryColumnCombo->addItem(tr("None"));
spacerItem4 = new QSpacerItem(20,20,QSizePolicy::Minimum,QSizePolicy::Expanding);
tertiaryOrderLabel = new QLabel(tr("Order:"));
tertiaryOrderCombo = new QComboBox;
tertiaryOrderCombo->addItem(tr("Ascending"));
tertiaryOrderCombo->addItem(tr("Descending"));
gridLayout3 = new QGridLayout;
gridLayout3->addWidget(tertiaryColumnLabel,0,0,1,1);
gridLayout3->addWidget(tertiaryColumnCombo,0,1,1,1);
gridLayout3->addItem(spacerItem4,0,2,1,1);
gridLayout3->addWidget(tertiaryOrderLabel,1,0,1,1);
gridLayout3->addWidget(tertiaryOrderCombo,1,1,1,2);
tertiaryGroupBox->setLayout(gridLayout3);
gridLayout = new QGridLayout; //窗口总的布局
gridLayout->addLayout(vboxLayout,0,1,2,1);
gridLayout->addWidget(primaryGroupBox,0,0,1,1);
gridLayout->addItem(spacerItem2, 1, 0, 1, 1);
gridLayout->addWidget(secondaryGroupBox, 2, 0, 1, 1);
gridLayout->addWidget(tertiaryGroupBox, 3, 0, 1, 1);
setLayout(gridLayout);
QWidget::setTabOrder(primaryColumnCombo, primaryOrderCombo);//设置键的顺序
QWidget::setTabOrder(primaryOrderCombo, secondaryColumnCombo);
QWidget::setTabOrder(secondaryColumnCombo, secondaryOrderCombo);
QWidget::setTabOrder(secondaryOrderCombo, tertiaryColumnCombo);
QWidget::setTabOrder(tertiaryColumnCombo, tertiaryOrderCombo);
QWidget::setTabOrder(tertiaryOrderCombo, okButton);
QWidget::setTabOrder(okButton, cancelButton);
QWidget::setTabOrder(cancelButton, moreButton);
QObject::connect(moreButton, SIGNAL(toggled(bool)), secondaryGroupBox, SLOT(setVisible(bool)));
QObject::connect(moreButton, SIGNAL(toggled(bool)), tertiaryGroupBox, SLOT(setVisible(bool)));
QObject::connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
QObject::connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
QMetaObject::connectSlotsByName(this); //on_objectName_slot()
secondaryGroupBox->hide();
tertiaryGroupBox->hide();
layout()->setSizeConstraint(QLayout::SetFixedSize); //设置布局大小的原则
setColumnRange('A','Z');
}
void SortDialog::setColumnRange(QChar first,QChar last)
{
primaryColumnCombo->clear();
secondaryColumnCombo->clear();
tertiaryColumnCombo->clear();
secondaryColumnCombo->addItem(tr("None"));
tertiaryColumnCombo->addItem(tr("None"));
primaryColumnCombo->setMinimumSize(secondaryColumnCombo->sizeHint()); //同步大小
QChar ch = first;
while(ch <= last)
{
primaryColumnCombo->addItem(QString(ch));
secondaryColumnCombo->addItem(QString(ch));
tertiaryColumnCombo->addItem(QString(ch));
ch = ch.unicode() + 1; //这个函数会改变ch的值,因为这个函数用地址进行运算
}
}
主函数:
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
SortDialog *dialog = new SortDialog;
dialog->setColumnRange('C','F');//设置范围为C到F
dialog->show();
return app.exec();
}
别人的代码,自己加了一些注释,给出了一些解释。。。不喜勿喷...