qfedu-qt-level/homework-day1/widget7.cpp

80 lines
2.1 KiB
C++

#include "widget7.h"
#include <QDebug>
Widget7::Widget7(QWidget *parent) : QWidget(parent)
{
setWindowTitle("题目6");
setFixedSize(800,600);
vct = new QVector<QString>();
btn1 = new QPushButton("张三",this);
btn1->move(10,20);
btn2 = new QPushButton("李四",this);
btn2->move(10,70);
btn3 = new QPushButton("王五",this);
btn3->move(10,120);
btn4 = new QPushButton("赵六",this);
btn4->move(10,170);
btn5 = new QPushButton("孙七",this);
btn5->move(10,220);
btn_show = new QPushButton("打印",this);
btn_show->move(150,20);
btn_restart = new QPushButton("重新再来",this);
btn_restart->move(150,70);
connect(this,&Widget7::my_signal,this,&Widget7::my_handler);
connect(btn1,&QPushButton::clicked,[&]{
QString str = btn1->text();
emit this->my_signal(str);
emit this->studentSelected(str);
});
connect(btn2,&QPushButton::clicked,[&]{
QString str = btn2->text();
emit this->my_signal(str);
emit this->studentSelected(str);
});
connect(btn3,&QPushButton::clicked,[&]{
QString str = btn3->text();
emit this->my_signal(str);
emit this->studentSelected(str);
});
connect(btn4,&QPushButton::clicked,[&]{
QString str = btn4->text();
emit this->my_signal(str);
emit this->studentSelected(str);
});
connect(btn5,&QPushButton::clicked,[&]{
QString str = btn5->text();
emit this->my_signal(str);
emit this->studentSelected(str);
});
// 打印学生
connect(btn_show,&QPushButton::clicked,[&]()->void{
QVector<QString>::iterator it = vct->begin();
while(it != vct->end()){
qDebug()<<*it;
it++;
}
});
connect(btn_restart,&QPushButton::clicked,[&]()->void{
vct->clear(); // 清空容器
});
}
void Widget7::my_handler(const QString &str)
{
// qDebug()<<str;
vct->append(str); // 向 QVector 中添加学生
}