持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第28天,点击查看活动详情
📒博客首页:何名取 的个人主页 - 文章 - 掘金 (juejin.cn)
🎉欢迎关注🔎点赞👍收藏⭐️留言📝
❤️期待一起交流!
🙏作者水平很有限,如果发现错误,求告知,多谢!
🌺有问题可私信交流!!!
选择题组件绘制
前言
前面一节讲述了题库管理系统中的组合表的查询方式。利用外键,很容易地关联到多个表。在这几个表中,PDF电子书信息表对应的展示方式是书库页,题目信息表没有对应的展示方式,选择题信息表的对应展示方式是选择题组件,也就是我们常见的题目-选项a-选项b-选项c-选项d这种方式。如下图所示:
本节就来制作选择题的对应展示组件。
创建自定义类
创建一个名为ChoiceQstWidget的C++类作为选择题展示组件。
创建完成后修改choiceqstwidget.h头文件和choiceqstwidget.cpp源文件代码。
布局
选择题展示组件由:
- 题目,对应的是QLineEdit
- 选项,对应的是一个QLabel和QRadioButton合成的组件
修改choiceqstwidget.h头文件:
#ifndef CHOICEQSTWIDGET_H
#define CHOICEQSTWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
class ChoiceQstWidget : public QWidget
{
Q_OBJECT
public:
explicit ChoiceQstWidget(QWidget *parent = nullptr);
QString info_str;
QString op1_str;
QString op2_str;
QString op3_str;
QString op4_str;
char answ;
private:
QRadioButton *op1; //选项1
QRadioButton *op2; //选项2
QRadioButton *op3; //选项3
QRadioButton *op4; //选项4
QLineEdit *info; //题目
QLabel *lab_a; //标号a
QLabel *lab_b; //标号b
QLabel *lab_c; //标号c
QLabel *lab_d; //标号d
};
#endif // CHOICEQSTWIDGET_H
修改choiceqstwidget.cpp源文件:
#include "choiceqstwidget.h"
ChoiceQstWidget::ChoiceQstWidget(QWidget *parent)
: QWidget{parent}
{
op1 = new QRadioButton;
op2 = new QRadioButton;
op3 = new QRadioButton;
op4 = new QRadioButton;
info = new QLineEdit;
lab_a = new QLabel;
lab_b = new QLabel;
lab_c = new QLabel;
lab_d = new QLabel;
//新建水平布局
QHBoxLayout *lay_a = new QHBoxLayout;
lab_a->setText("A");
lab_a->setFixedSize(40,40);
op1->setText(op1_str);
lay_a->addWidget(lab_a);
lay_a->addWidget(op1);
//新建水平布局
QHBoxLayout *lay_b = new QHBoxLayout;
lab_b->setText("B");
lab_b->setFixedSize(40,40);
op2->setText(op2_str);
lay_b->addWidget(lab_b);
lay_b->addWidget(op2); //新建水平布局
QHBoxLayout *lay_c = new QHBoxLayout;
lab_c->setText("C");
lab_c->setFixedSize(40,40);
op3->setText(op3_str);
lay_c->addWidget(lab_c);
lay_c->addWidget(op3); //新建水平布局
QHBoxLayout *lay_d = new QHBoxLayout;
lab_d->setText("D");
lab_d->setFixedSize(40,40);
op4->setText(op4_str);
lay_d->addWidget(lab_d);
lay_d->addWidget(op4);
//新建垂直布局
QVBoxLayout *lay1 = new QVBoxLayout;
info->setText(info_str);
info->setMinimumHeight(80);
lay1->addWidget(info);
lay1->addLayout(lay_a);
lay1->addLayout(lay_b);
lay1->addLayout(lay_c);
lay1->addLayout(lay_d);
}