#include "widget7.h" #include Widget7::Widget7(QWidget *parent) : QWidget(parent) { setWindowTitle("题目6"); setFixedSize(800,600); vct = new QVector(); 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(this,SIGNAL(my_signal(const QString &)),this,SLOT(my_handler(const QString &))); connect(btn1,&QPushButton::clicked,[&]{ QString str = btn1->text(); emit this->my_signal(str); emit this->studentSelected(str); btn1->setEnabled(false); }); connect(btn2,&QPushButton::clicked,[&]{ QString str = btn2->text(); emit this->my_signal(str); emit this->studentSelected(str); btn2->setEnabled(false); }); connect(btn3,&QPushButton::clicked,[&]{ QString str = btn3->text(); emit this->my_signal(str); emit this->studentSelected(str); btn3->setEnabled(false); }); connect(btn4,&QPushButton::clicked,[&]{ QString str = btn4->text(); emit this->my_signal(str); emit this->studentSelected(str); btn4->setEnabled(false); }); connect(btn5,&QPushButton::clicked,[&]{ QString str = btn5->text(); emit this->my_signal(str); emit this->studentSelected(str); btn5->setEnabled(false); }); // 打印学生 connect(btn_show,&QPushButton::clicked,[&]()->void{ QVector::iterator it = vct->begin(); while(it != vct->end()){ qDebug()<<*it; it++; } // for(int i=0;isize();i++){ // qDebug()<at(i); // } }); connect(btn_restart,&QPushButton::clicked,[&]()->void{ vct->clear(); // 清空容器 btn1->setEnabled(true); // 还原按钮可用性 btn2->setEnabled(true); btn3->setEnabled(true); btn4->setEnabled(true); btn5->setEnabled(true); }); } void Widget7::my_handler(const QString &str) { // qDebug()<count(str) == 0) // 当本字符串尚未加入 vct 时,将其加入 vct vct->append(str); // 向 QVector 中添加学生 }