qt作业day2
- 手机
- 2025-09-13 18:09:02

1:在注册登录的练习里面,追加一个QListWidget 项目列表 要求:点击注册之后,将账号显示到 listWidget上面去 以及,在listWidget中双击某个账号的时候,将该账号删除
.h
#ifndef WIDGET_H #define WIDGET_H #include "QFile" #include <QWidget> #include <QListWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); void save_data(const QString &filename,const QString &data); QString load_data(const QString& filename); ~Widget(); private slots: void on_lineEdit_2_textChanged(const QString &arg1); void on_lineEdit_textChanged(const QString &arg1); void on_pushButton_3_pressed(); void on_pushButton_3_released(); void on_checkBox_stateChanged(int arg1); void on_pushButton_clicked(); // 注册按钮点击 void on_lineEdit_returnPressed(); // 输入框回车 void myslot_2(); // 信号示例 void loadAccountInfo(); // 加载账号信息 void deleteAccount(QListWidgetItem* item); // 删除账号 private: Ui::Widget *ui; QFile file; QString accountFilePath; // 账号信息文件路径 QList<QString> accounts; // 存储账号信息 }; #endif // WIDGET_H.cpp
#include "widget.h" #include "ui_widget.h" #include "QFile" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); QString name = load_data("F:/hqyjian/mm.txt"); ui->lineEdit->setText(name); accountFilePath = "F:/hqyjian/mm.txt"; // 加载账号信息 loadAccountInfo(); // 连接信号和槽 connect(ui->pushButton, &QPushButton::clicked, this, &Widget::on_pushButton_clicked); connect(ui->lineEdit_2, &QLineEdit::returnPressed, this, &Widget::on_lineEdit_returnPressed); connect(ui->listWidget, &QListWidget::itemDoubleClicked, this, &Widget::deleteAccount); } Widget::~Widget() { QString name = ui->lineEdit_2->text(); QString password = ui->lineEdit->text(); save_data("F:/hqyjian/mm.txt",name); save_data("F:/hqyjian/mm.txt",password); delete ui; } void Widget::save_data(const QString &filename,const QString &data) { file.setFileName(filename); file.open(QFile::WriteOnly); file.write(data.toLocal8Bit()); file.close(); } QString Widget::load_data(const QString &filename) { file.setFileName(filename); file.open(QFile::ReadOnly); QByteArray arr = file.readAll(); QString str = QString::fromLocal8Bit(arr); file.close(); return str; } void Widget::on_lineEdit_2_textChanged(const QString &arg1) { QString name = ui->lineEdit->text(); QString pswd = ui->lineEdit_2->text(); if(name.isEmpty() || pswd.isEmpty()){ ui->pushButton->setEnabled(0); ui->pushButton_2->setEnabled(0); }else{ ui->pushButton->setEnabled(1); ui->pushButton_2->setEnabled(1); } } void Widget::on_lineEdit_textChanged(const QString &arg1) { QString name = ui->lineEdit->text(); QString pswd = ui->lineEdit_2->text(); if(name.isEmpty() || pswd.isEmpty()){ ui->pushButton->setEnabled(0); ui->pushButton_2->setEnabled(0); }else{ ui->pushButton->setEnabled(1); } } void Widget::on_pushButton_3_pressed() { ui->lineEdit->setEchoMode(QLineEdit::Normal); } void Widget::on_pushButton_3_released() { ui->lineEdit->setEchoMode(QLineEdit::Password); } void Widget::on_checkBox_stateChanged(int arg1) { } void Widget::on_pushButton_clicked() { QString str = ui->lineEdit->text(); if (!str.isEmpty()) { accounts.append(str); ui->listWidget->addItem(str); // 添加到 QListWidget ui->textEdit->append(str); // 添加到 QTextEdit ui->lineEdit->clear(); } } void Widget::on_lineEdit_returnPressed() { on_pushButton_clicked(); // 回车键触发注册按钮点击事件 } void Widget::myslot_2() { ui->textEdit->append("dda"); } void Widget::loadAccountInfo() { QFile file(accountFilePath); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); accounts.append(line); ui->listWidget->addItem(line); // 添加到 QListWidget ui->textEdit->append(line); // 添加到 QTextEdit } file.close(); } } void Widget::deleteAccount(QListWidgetItem* item) { if (item) { QString account = item->text(); accounts.removeAll(account); ui->listWidget->removeItemWidget(item); delete item; // 保存更新后的账号信息 QFile file(accountFilePath); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); for (const QString& acc : accounts) { out << acc << "\n"; } file.close(); } } }main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }2.完成一个计算器,ui界面如下 完成 +-*/计算功能并且在对应位置显示对应的数据
.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "QString" #include "QIcon" #include "math.h" #include "QPixmap" #include <QPainter> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); void paintEvent(QPaintEvent *event); public slots: void push_Botton(); void push_Botton1(); void push_Botton2(); void push_Botton3(); void push_Botton4(); void push_Botton5(); void push_Botton6(); void push_Botton7(); void push_Botton8(); void push_Botton9(); void bnt_add(); void bnt_sub(); void bnt_mul(); void bnt_div(); void bnt_d(); void bnt_over(); private slots: void on_clear_clicked(bool checked); void on_clearall_clicked(bool checked); void on_delete_2_clicked(bool checked); void on_fenshu_clicked(bool checked); void on_pow_clicked(bool checked); void on_genhao_clicked(bool checked); void on_aors_clicked(bool checked); void on_yu_clicked(bool checked); private: Ui::Widget *ui; QString symbol = ""; QString num1 = ""; QString num2 = ""; QString over = ""; QString msg = ""; bool isSave01 = true; bool isSave02 = true; }; #endif // WIDGET_H.cpp
#include "widget.h" #include "QIcon" #include "QString" #include "QDebug" #include "QLabel" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->setFixedSize(430,550); this->setWindowTitle("计算器"); this->setWindowIcon(QIcon(":/image/computer.webp")); //使用setStyleSheet方法设置标签颜色 ui->label_msg->setStyleSheet("QLabel { color: grey; }"); connect(ui->add,SIGNAL(clicked(bool)),this,SLOT(bnt_add())); connect(ui->div,SIGNAL(clicked(bool)),this,SLOT(bnt_div())); connect(ui->sub,SIGNAL(clicked(bool)),this,SLOT(bnt_sub())); connect(ui->mul,SIGNAL(clicked(bool)),this,SLOT(bnt_mul())); connect(ui->over,SIGNAL(clicked(bool)),this,SLOT(bnt_over())); connect(ui->btn_d,SIGNAL(clicked(bool)),this,SLOT(bnt_d())); connect(ui->push_Botton_0,SIGNAL(clicked(bool)),this,SLOT(bnt_num0())); connect(ui->push_Botton_1,SIGNAL(clicked(bool)),this,SLOT(bnt_num1())); connect(ui->push_Botton_2,SIGNAL(clicked(bool)),this,SLOT(bnt_num2())); connect(ui->push_Botton_3,SIGNAL(clicked(bool)),this,SLOT(bnt_num3())); connect(ui->push_Botton_4,SIGNAL(clicked(bool)),this,SLOT(bnt_num4())); connect(ui->push_Botton_5,SIGNAL(clicked(bool)),this,SLOT(bnt_num5())); connect(ui->push_Botton_6,SIGNAL(clicked(bool)),this,SLOT(bnt_num6())); connect(ui->push_Botton_7,SIGNAL(clicked(bool)),this,SLOT(bnt_num7())); connect(ui->push_Botton_8,SIGNAL(clicked(bool)),this,SLOT(bnt_num8())); connect(ui->push_Botton_9,SIGNAL(clicked(bool)),this,SLOT(bnt_num9())); } Widget::~Widget() { delete ui; } void Widget::paintEvent(QPaintEvent *event) { QPainter painter(this); // painter.drawPixmap(0,0,width(),height(),QPixmap(":/image/bizhi.jpg")); } //数字0代码段 void Widget::bnt_num0() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("0"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("0"); ui->label_idet->setText(num2); } } //数字1代码段 void Widget::bnt_num1() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("1"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("1"); ui->label_idet->setText(num2); } } //数字2代码段 void Widget::bnt_num2() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("2"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("2"); ui->label_idet->setText(num2); } } //数字3代码段 void Widget::bnt_num3() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("3"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("3"); ui->label_idet->setText(num2); } } //数字4代码段 void Widget::bnt_num4() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("4"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("4"); ui->label_idet->setText(num2); } } //数字5代码段 void Widget::bnt_num5() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("5"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("5"); ui->label_idet->setText(num2); } } //数字6代码段 void Widget::bnt_num6() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("6"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("6"); ui->label_idet->setText(num2); } } //数字7代码段 void Widget::bnt_num7() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("7"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("7"); ui->label_idet->setText(num2); } } //数字8代码段 void Widget::bnt_num8() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("8"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("8"); ui->label_idet->setText(num2); } } //数字9代码段 void Widget::bnt_num9() { if(symbol == "") { if(num1 == "0") { num1.chop(1); } num1.append("9"); ui->label_idet->setText(num1); } else { if(num2 == "0") { num2.chop(1); } num2.append("9"); ui->label_idet->setText(num2); } } //小数点代码段 void Widget::bnt_d() { if(symbol == "") { num1.append("."); ui->label_idet->setText(num1); } else { num2.append("."); ui->label_idet->setText(num2); } } //加法 void Widget::bnt_add() { double n1 = num1.toDouble(); double n2 = num2.toDouble(); double x = 0.0; if(symbol == "+") { //计算三位以上的式子 x = n1 + n2; num1 = QString::number(x); qDebug()<<"x="<<x; } else if (symbol =="-") { x = n1 - n2; num1 = QString::number(x); } else if (symbol =="×") { x = n1 * n2; num1 = QString::number(x); } else if (symbol =="÷") { x = n1 / n2; num1 = QString::number(x); } else if(symbol == "%") { if(num1 == "") { ui->label_msg->setText("0"); } else if(num2 != "") { num1 = QString::number(x); } } num2 = ""; msg = ""; //将式子放入信息栏中 if(isSave01) { msg.append(num1); } symbol = "+"; msg.append(symbol); ui->label_msg->setText(msg); isSave01 = true; } //减法 void Widget::bnt_sub() { double n1 = num1.toDouble(); double n2 = num2.toDouble(); double x = 0.0; if(symbol == "-") { //计算三位以上的式子 x = n1 - n2; num1 = QString::number(x); qDebug()<<"x="<<x; } else if (symbol =="+") { x = n1 + n2; num1 = QString::number(x); } else if (symbol =="×") { x = n1 * n2; num1 = QString::number(x); } else if (symbol =="÷") { x = n1 / n2; num1 = QString::number(x); } else if(symbol == "%") { if(num1 == "") { ui->label_msg->setText("0"); } else if(num2 != "") { num1 = QString::number(x); } } num2 = ""; msg = ""; //将式子放入信息栏中 if(isSave01) { msg.append(num1); } symbol = "-"; msg.append(symbol); ui->label_msg->setText(msg); isSave01 = true; } //乘法 void Widget::bnt_mul() { double n1 = num1.toDouble(); double n2 = num2.toDouble(); double x = 0.0; if(symbol == "×") { //计算三位以上的式子 x = n1 * n2; num1 = QString::number(x); qDebug()<<"x="<<x; } else if (symbol =="-") { x = n1 - n2; num1 = QString::number(x); } else if (symbol =="+") { x = n1 + n2; num1 = QString::number(x); } else if (symbol =="÷") { x = n1 / n2; num1 = QString::number(x); } else if(symbol == "%") { if(num1 == "") { ui->label_msg->setText("0"); } else if(num2 != "") { num1 = QString::number(x); } } num2 = ""; msg = ""; //将式子放入信息栏中 if(isSave01) { msg.append(num1); } symbol = "×"; msg.append(symbol); ui->label_msg->setText(msg); isSave01 = true; } //除法 void Widget::bnt_div() { double n1 = num1.toDouble(); double n2 = num2.toDouble(); double x = 0.0; if(symbol == "÷") { //计算三位以上的式子 x = n1 / n2; num1 = QString::number(x); qDebug()<<"x="<<x; } else if (symbol =="-") { x = n1 - n2; num1 = QString::number(x); } else if (symbol =="×") { x = n1 * n2; num1 = QString::number(x); } else if (symbol =="+") { x = n1 + n2; num1 = QString::number(x); } else if(symbol == "%") { if(num1 == "") { ui->label_msg->setText("0"); } else if(num2 != "") { x = n1 / n2; num1 = QString::number(x); } } num2 = ""; msg = ""; //将式子放入信息栏中 if(isSave01) { msg.append(num1); } symbol = "÷"; msg.append(symbol); ui->label_msg->setText(msg); isSave01 = true; } //等于 void Widget::bnt_over() { if(isSave02) { msg.append(num2); } double n1 = num1.toDouble(); double n2 = num2.toDouble(); double x = 0; if(symbol == "+") { //计算按下等于之后的结果 x = n1 + n2; } else if(symbol == "-") { x = n1 - n2; } else if(symbol == "×") { x = n1 * n2; } else if(symbol == "÷") { if (n2 != 0) { x = n1 / n2; } else { ui->label_idet->setText("除数不能为0"); return; } } else if(symbol == "%") { //判断num1存在的同时num2也存在 if(num1 == "") { ui->label_msg->setText("0"); } else if(num2 != "") { } } else if(symbol == "") { x = n1; } msg.append("="); ui->label_msg->setText(msg); QString str = QString::number(x); ui->label_idet->setText(str); num1 = str; num2 = ""; symbol = ""; msg = ""; isSave02 = true; } void Widget::on_clear_clicked(bool checked) { //删除最近一项 if(symbol == "") { num1 = ""; ui->label_idet->setText("0"); } else { num2 = ""; ui->label_idet->setText("0"); } } void Widget::on_clearall_clicked(bool checked) { //删除所有项 num1 = ""; num2 = ""; symbol = ""; ui->label_idet->setText("0"); msg = ""; ui->label_msg->clear(); } void Widget::on_delete_2_clicked(bool checked) { //删除一个数字 if(symbol == "") { num1.chop(1); ui->label_idet->setText(num1); } else { num2.chop(1); ui->label_idet->setText(num2); } } void Widget::on_fenshu_clicked(bool checked) { //化为分数 if(num1 == 0) { ui->label_idet->setText("分母不能为0"); } else { if(symbol == "") { double n1 = num1.toDouble(); msg.append("1/("); msg.append(QString::number(n1)); msg.append(")"); ui->label_msg->setText(msg); n1 = 1 / num1.toDouble(); QString str = QString::number(n1); num1 = str; ui->label_idet->setText(str); } else { double n2 = num2.toDouble(); isSave02=false; msg.append("1/("); msg.append(QString::number(n2)); msg.append(")"); ui->label_msg->setText(msg); n2 = sqrt(num2.toDouble()); qDebug() << "n2:" << n2 << endl; QString str = QString::number(n2); num2 = str; ui->label_idet->setText(str); } } } void Widget::on_pow_clicked(bool checked) { //平方 if(symbol == "") { double n1 = num1.toDouble(); msg.append("sqr("); msg.append(QString::number(n1)); msg.append(")"); ui->label_msg->setText(msg); n1 = num1.toDouble() * num1.toDouble(); QString str = QString::number(n1); num1 = str; ui->label_idet->setText(str); } else { double n2 = num2.toDouble(); isSave02=false; msg.append("sqr("); msg.append(QString::number(n2)); msg.append(")"); ui->label_msg->setText(msg); n2 = num2.toDouble() * num2.toDouble(); qDebug() << "n2:" << n2 << endl; QString str = QString::number(n2); num2 = str; ui->label_idet->setText(str); } } void Widget::on_genhao_clicked(bool checked) { //开根号 if(symbol == "") { double n1 = num1.toDouble(); msg.append("根号("); msg.append(QString::number(n1)); msg.append(")"); ui->label_msg->setText(msg); n1 = sqrt(num1.toDouble()); QString str = QString::number(n1); num1 = str; ui->label_idet->setText(str); } else { double n2 = num2.toDouble(); isSave02=false; msg.append("根号("); msg.append(QString::number(n2)); msg.append(")"); ui->label_msg->setText(msg); n2 = sqrt(num2.toDouble()); qDebug() << "n2:" << n2 << endl; QString str = QString::number(n2); num2 = str; ui->label_idet->setText(str); } } void Widget::on_aors_clicked(bool checked) { //转换正负数 if(symbol == "") { double n1 = num1.toDouble(); n1 = -n1; num1 = QString::number(n1); ui->label_idet->setText(num1); qDebug() << "符号1:" << num1; } else { double n2 = num2.toDouble(); n2 = -n2; num2 = QString::number(n2); ui->label_idet->setText(num2); qDebug() << "符号2:" << num2; } } void Widget::on_yu_clicked(bool checked)//有问题 { //72 + 5% =3.6 //72的5%是3.6,所以72+3.6就是75.6 //将式子放入信息栏中 double n1 = num1.toDouble(); double n2 = num2.toDouble(); double x; if(num1 != "" && num2 != "") { n2 = (n2 / 100) * n1 ; num2 = QString::number(n2); qDebug() << "n2 = "<<n2; } else if(num2 != "") { ui->label_msg->setText("0"); } if(symbol == "÷") { //计算三位以上的式子 x = n1 / n2; num1 = QString::number(x); qDebug()<<"x="<<x; } else if (symbol =="-") { x = n1 - n2; num1 = QString::number(x); } else if (symbol =="×") { x = n1 * n2; num1 = QString::number(x); } else if (symbol =="+") { x = n1 + n2; num1 = QString::number(x); } msg.append(num2); num2 = ""; }