如果我们想用代码生成改变形状对话框怎么实现,琢磨着试着一步步实现,用Designer设计出来的改变形状对话框如下图所示:
从上面初步了解到,创建改变形状对话框是由PushButton、GroupBox、Label、ComboBox、Spacer等窗口部件组成。将窗口布局成这样还有栅格布局管理器和垂直布局管理器配合使用,有了大概了解开始尝试着做,双击打开develop编辑器,点击“工程”,选择“新建工程”,在/root下目录创建工程名为sort工程,模板选择空或主要工程,其他默认,点击确定。
点击右键刚才创建的sort.pro项目工程,弹出弹出菜单对话框,点击添加新项
添加项目工程sortdialog.cpp和main.cpp源文件和sortdialog.h头文件,文件包含:源文件、头文件、对话框、资源、翻译。文件名输入:sortdialog.cpp,位置选择刚才创建项目目录,点击浏览。就是位置行编辑器后面的那个Button按钮,点击会弹出一个对话框,然后选择刚才创建的工程目录,点击选择。然后点击添加新项的确定按钮,这时弹出一个Question MessageBox提问消息框,询问是否在同一位置创建头文件,点击“是”,用同样方法创建一个main.cpp源文件,当弹出Question MessageBox消息框是选择“否”,只创建main.cpp源文件就行。
点击左边sort.pro项目文件下的main.cpp源文件,编辑程序的主函数,每个程序都有一个主函数。Main.cpp源文件中包含每个Qt图形化应用程序的QApplication类的定义,创建SortDialog对话框头文件的说明,一个管理资源的QApplication的对象app,显示SortDialog对话框定义dialog对象的窗口部件的show()函数,最后是将用户发生的事件处理结果用Qt图形化应用程序app对象的exec()函数在退出时将值返回
先创建一个对话框窗体,点击sortdialog.h头文件,定义一个窗口部件QWidget类,声明SortDialog类是继承窗口部件的公共类。初始化SortDialog对话框,,parent参数指针为空说明SortDialog窗口部件类的定义没有父对象,也就是没有父控件
定义一个QtGui用于初始化类的头文件类的声明,第4行QWidget(parent) 是把parent参数传递给了基类的构造函数
点击编译,选择编译或直接按
F7,
然后点击调试
---
选择启动程序,接着弹出一个对话框询问是否保存文件,点击确定,然后启动
debeg,
执行可执行文件弹出一个没有标题空对话框。
接下来创建右上角的三个按钮
OK
、
Cancel
、
More,
和一个垂直分隔符,用垂直布局管理器布局三个按钮窗口部件和一个垂直分隔符,然后,栅格布局管理器指定管理垂直布局摆放位置。下面为创建这些部件做前期准备,先声明窗口部件的类分别是栅格布局管理器类(
QGridLayout
)、垂直布局管理器类
(QVBoxLayou)
、按钮类
(QPushButton)
、分隔符类
(QSpacerItem),
然后定义各个类对应的私有对象。栅格布局的对象是
gridLayout,
垂直分隔符对应的对象是
vboxLayou,
按钮对应的对象是
okButton
、
cancelButton
、
moreButton
等。垂直分隔符对应的对象是
spacerItem.
头文件添加类和指定对象,然后点击
sortdialog.cppC++
源文件,实现预想的功能,引用三个按钮的对象的声明,并调用
tr()
函数设置三个按钮的文本属性分别为
:OK
、
Cancel
、
More
,
OK
按钮默认为真,设置
More
按钮检测启动功能为真。接着添加一个垂直分隔符对象,用来调整各部件的空间位置,使窗口部件摆布跟协调,美观。宽度为
20
,长度为
0
。尺寸大小设置为最小,尺寸类型为
QSizePolicy::Expanding
(扩展类型)。最后定义两个布局管理器来管理窗口部件。栅格布局的特点是指定窗口部件摆放位置,就像一个坐标一样。设置开始坐标为(
0,1
)终点坐标为(
2,1
)这两点是矩形对角线的点。最后将布局设置为
gridLayout
栅格布局。调用
setWindowTitle
()函数设置对话框标题。
敲完测试一下效果是不是预想的那样,点击编译
—
重新构建(或按
F6
,刚才编译了所有要清除文件再编译)
—
点击启动程序
(Shirt+F5),
弹出的下面对话框,说明初见成效了。
创建一个群组框
(GroupBox)
、两个标签
(Label)
、两个组合框
(ComboBox)
及一个水平分隔符
(Horizontal Spacer)
,惯例要创建先声明窗口部件的类,然后给
GroupBox
、
Label
、
ComboBox
及
Horizontal Spacer
指定私有对象
.primaryGroupBox
与
secondaryGroupBox
之间还有一个垂直分隔符,在定义一个垂直分隔符对象。
声明要用到的对象,添加分隔符到布局和在组合框添加项目都是用addItem()函数添加,设置标签和群主框文本属性可以调用tr()函数来设定。添加到部件中的窗口部件后面跟的四个数字是起点和终点坐标,两点是对角点。最后将排放标签、组合框、分隔符的栅格布局添加到群主框。接着将组合框添加到总的栅格布局管理器
点击编译
---
重新构建
---
接着启动程序运行执行文件,出来了,看到的效果跟最终运行出来的效果差不多,但接下来添加剩下的两个群组框。
第一个群组框做出来了,做第二,第三个相同的群组框就很容易了。用创建第一个群组框的方法创建。最后创建完了,重新构建
à
再启动程序
,
运行效果出来了,没有添加
Tab
顺序及信号及槽的链接。
设置
Tab
顺序,当第一个部件键盘焦点移到第二个部件是,
Tab
键被按下。链条顺序,从窗体的最上面到下面依次排列每个组合框,然后到窗体右侧的
OK
、
Cancel
和
More
按钮。调用
QWidget::setTabOrder()
函数来设置。
Tab顺序排好了,接着定义OK,Cancel,More三个Button按钮的信号与槽的连接。建立okButton和accept()槽的链接,cancelButton和reject()槽的链接。建立moreButton和secondaryGroupBox及tertiaryGroupBox群组框之间的连接,toggled(bool)作为信号,setVisible(bool)作为槽
重新构建
—
启动程序
—
点击
More
按钮,效果如下图,由于没有设置
groupBox
指定的大小所以有些变形了。但没有多大影响。
接着实现迈向成功的最后的最后一步,将
secondaryGroupBox
和
tertiaryGroupBox
隐藏,给
ComBoBox
指定数值范围。在
sortdialog.h
头文件声明一个设置范围的函数,函数跟两个字符参数,在
sortdialog.cpp
源文件中实现,组合框隐藏及值的范围设定功能,最后可以在
main.cpp
主函数源文件中设置局部值的范围,显示范围将按主函数局部变量显示。
定义范围的全局变量,及实现群组框隐藏的功能
设置显示范围为
’C’
à
’F’
运行效果出来了,可以看到预想的效果实现的
点击More按钮,弹出隐藏群组框。点击组合框弹出下来范围选择。
改变对话框代码,SortDialog.h头文件
#ifndef __SORTDIALOG_H__
#define __SORTDIALOG_H__
#include <QDialog>
class QGridLayout;
class QVBoxLayout;
class QPushButton;
class QSpacerItem;
class QGroupBox;
class QLabel;
class QComboBox;
#define __SORTDIALOG_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:
QSpacerItem *spacerItem;
QGridLayout *gridLayout;
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;
{
Q_OBJECT
public:
SortDialog(QWidget *parent = 0);
void setColumnRange(QChar first,QChar last);
private:
QSpacerItem *spacerItem;
QGridLayout *gridLayout;
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;
};
// place your code here
// place your code here
#endif // __SORTDIALOG_H__
实现功能sortdialog.cpp源文件
#include <QtGui>
#include "sortdialog.h"
#include "sortdialog.h"
SortDialog::SortDialog(QWidget *parent)
:QDialog(parent)
{
okButton = new QPushButton(tr("OK"));
okButton->setDefault(true);
cancelButton = new QPushButton(tr("Cancel"));
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
spacerItem = new QSpacerItem(20, 0, QSizePolicy::Minimum,
QSizePolicy::Expanding);
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);
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()));
:QDialog(parent)
{
okButton = new QPushButton(tr("OK"));
okButton->setDefault(true);
cancelButton = new QPushButton(tr("Cancel"));
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
spacerItem = new QSpacerItem(20, 0, QSizePolicy::Minimum,
QSizePolicy::Expanding);
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);
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);
secondaryGroupBox->hide();
tertiaryGroupBox->hide();
layout()->setSizeConstraint(QLayout::SetFixedSize);
setColumnRange('A','Z');
}
void SortDialog::setColumnRange(QChar first,QChar last)
{
primaryColumnCombo->clear();
secondaryColumnCombo->clear();
tertiaryColumnCombo->clear();
{
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;
}
}
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;
}
}
main.cpp主函数源文件
#include "sortdialog.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
SortDialog *dialog = new SortDialog;
dialog->setColumnRange('C','F');
dialog->show();
return app.exec();
}
{
QApplication app(argc,argv);
SortDialog *dialog = new SortDialog;
dialog->setColumnRange('C','F');
dialog->show();
return app.exec();
}
呵呵。。。好像蛮简单的。。。
本文转自 chen138 51CTO博客,原文链接:http://blog.51cto.com/chenboqiang/319363,如需转载请自行联系原作者