(1)界面搭建 :
(2)控件间的逻辑关系实现 :
(3)重要的关于界面创建时 QToolBox 的使用 ui_widget . h 的内容如下,太长,给出源代码 :
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QToolBox>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QVBoxLayout *verticalLayout_3;
QToolBox *toolBox;
QWidget *page;
QVBoxLayout *verticalLayout_2;
QPushButton *pushButton;
QLabel *label;
QWidget *page_2;
QVBoxLayout *verticalLayout;
QGroupBox *groupBox;
QGridLayout *gridLayout;
QRadioButton *radioButtonBlack;
QRadioButton *radioButtonRed;
QLineEdit *lineEdit;
void setupUi(QWidget * Widget) //形参是窗体 QWidget对象
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(292, 232);
QFont font;
font.setPointSize(14);
Widget->setFont(font); //这是对本程序中 QWidget的设置
Widget->setContextMenuPolicy(Qt::CustomContextMenu);
verticalLayout_3 = new QVBoxLayout(Widget); //给窗体加个垂直布局
verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
toolBox = new QToolBox(Widget); //要加入到窗体里的工具箱 QToolBox
toolBox->setObjectName(QString::fromUtf8("toolBox"));
toolBox->setFrameShape(QFrame::Panel); //这些边框的设置,是为了醒目一点
toolBox->setFrameShadow(QFrame::Sunken);
toolBox->setLineWidth(9);
toolBox->setMidLineWidth(0);
page = new QWidget(); //这些新的 QWidget是要加入到工具箱 QToolBox里的
page->setObjectName(QString::fromUtf8("page"));
page->setGeometry(QRect(0, 0, 262, 142));
verticalLayout_2 = new QVBoxLayout(page); //给 page1 设置个垂直布局
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
pushButton = new QPushButton(page); //给 page1添加个按钮
pushButton->setObjectName(QString::fromUtf8("pushButton"));
verticalLayout_2->addWidget(pushButton); //把 按钮加入到 page1里
label = new QLabel(page); //生成标签并加入到 page1里
label->setObjectName(QString::fromUtf8("label"));
verticalLayout_2->addWidget(label);
toolBox->addItem(page, QString::fromUtf8("Page 1")); //把完整的 page1加入到工具箱里
page_2 = new QWidget(); //要加入到工具箱里的 page2
page_2->setObjectName(QString::fromUtf8("page_2"));
page_2->setGeometry(QRect(0, 0, 262, 142));
verticalLayout = new QVBoxLayout(page_2); //page2 也采用垂直布局
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
groupBox = new QGroupBox(page_2); //把两个单选按钮放入一个容器里
groupBox->setObjectName(QString::fromUtf8("groupBox"));
gridLayout = new QGridLayout(groupBox); //容器采用了网格布局
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
radioButtonBlack = new QRadioButton(groupBox); //生成黑色字体按钮
radioButtonBlack->setObjectName(QString::fromUtf8("radioButtonBlack"));
radioButtonBlack->setChecked(true);
gridLayout->addWidget(radioButtonBlack, 0, 0, 1, 1);
radioButtonRed = new QRadioButton(groupBox); //生成红色字体按钮,并加入容器
radioButtonRed->setObjectName(QString::fromUtf8("radioButtonRed"));
gridLayout->addWidget(radioButtonRed, 0, 1, 1, 1);
lineEdit = new QLineEdit(groupBox); //把行文本框也加入 QGroupBox
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
gridLayout->addWidget(lineEdit, 1, 0, 1, 2);
verticalLayout->addWidget(groupBox); //把制作好的 QGroupBox加入 page2
toolBox->addItem(page_2, QString::fromUtf8("Page 2")); //把 page2窗体加入工具箱
verticalLayout_3->addWidget(toolBox); //把工具箱加入大窗体中
retranslateUi(Widget);
toolBox->setCurrentIndex(1); //让工具箱默认显示第一个页面
QMetaObject::connectSlotsByName(Widget); //允许通过函数名来连接信号与槽
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget ->setWindowTitle(QCoreApplication::translate("Widget", "H", nullptr));
groupBox ->setTitle (QString());
pushButton ->setText(QCoreApplication::translate("Widget",
"PushButton", nullptr));
label ->setText(QCoreApplication::translate("Widget",
"TextLabel" , nullptr));
radioButtonBlack->setText(QCoreApplication::translate("Widget",
"\351\273\221", nullptr));
radioButtonRed ->setText(QCoreApplication::translate("Widget",
"\347\272\242", nullptr));
lineEdit ->setText(QCoreApplication::translate("Widget",
"\345\223\210\345\223\210\345\223\210", nullptr));
toolBox ->setItemText(toolBox->indexOf(page),
QCoreApplication::translate("Widget", "Page 1", nullptr));
toolBox ->setItemText(toolBox->indexOf(page_2),
QCoreApplication::translate("Widget", "Page 2", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
(4)
谢谢