当前位置: 首页 > article >正文

【QT】day6

在这里插入图片描述

#include "home.h"
#include "ui_home.h"

Home::Home(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Home)
{
    ui->setupUi(this);

    // 从配置文件读取用户名
    QSettings settings("kim", "ad");
    username = settings.value("username").toString();
    usertype = settings.value("usertype").toString();
    ui->usertypelabel->setText(usertype);
    if(usertype=="学生")
    {
        ui->listframe->hide();
        ui->setframe->hide();
        ui->mainframe->hide();
        QFrame *stuframe = new QFrame();
        stuframe->setParent(this);
        stuframe->setStyleSheet("width:100px;height:100px;background-color: rgb(250, 250, 250);");
        stuframe->move(100,0);

        teachEdit = new QLineEdit(this);
        teachEdit->move(200,50);
        teachEdit->setStyleSheet("background-color:#ffffff;color:#000000;width:200px;height:30px;border-radius:7px;color:#000000;padding-left:10px;");
        teachEdit->setPlaceholderText("请输入教师账号");

        adkeyEdit = new QLineEdit(this);
        adkeyEdit->move(200,90);
        adkeyEdit->setStyleSheet("background-color:#ffffff;color:#000000;width:200px;height:30px;border-radius:7px;color:#000000;padding-left:10px;");
        adkeyEdit->setPlaceholderText("请输入签到验证码");

        adkeybtn = new QPushButton(this);
        adkeybtn->move(200,170);
        adkeybtn->setText("签到");
        adkeybtn->setStyleSheet("background-color:#000000;color:#ffffff;width:200px;height:40px;border-radius:7px;");
        connect(adkeybtn, &QPushButton::clicked, this, &Home::onAdKeyBtnClicked);

        stuHintLable = new QLabel(this);
        stuHintLable->move(210,140);
        stuHintLable->setFixedWidth(200);
        stuHintLable->setStyleSheet("color:rgb(222, 60, 51);height:40px;");
    }
    ui->usernamelabel->setText(username);
    ui->usertypelabel->setAlignment(Qt::AlignCenter);
    closeAllpage();
    openAdPage();
}

Home::~Home()
{
    delete ui;
}

void Home::onAdKeyBtnClicked() {
    if(teachEdit->text()=="")
    {
        stuHintLable->setText("教师账号不能为空");
        stuHintLable->setStyleSheet("color:rgb(222, 60, 51);height:40px;");
    }else if(adkeyEdit->text()=="")
    {
        stuHintLable->setText("签到激活码不能为空");
        stuHintLable->setStyleSheet("color:rgb(222, 60, 51);height:40px;");
    }else{
        socket = new QTcpSocket(this);
        socket->connectToHost(ip,port);
        connect(socket,&QTcpSocket::connected,this,&Home::stuad_connected_slot);
        connect(socket,&QTcpSocket::readyRead,this,&Home::stuad_readyRead_slot);
    }

}


void Home::startad_connected_slot()
{
    struct data user;
    user.type=STARTAD;
    user.username=username;
    user.value=generateRandomString(6);
    ui->hintlable_3->setText("签到码:"+user.value);
    ui->startattendancebtn->setStyleSheet("background-color:#ffffff;border-radius:7px;border:1px solid #000000;color:#000000;");
    socket->write(user.toByteArray());
}

void Home::startad_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    outputlog(receivedInfo.data);
    socket->disconnectFromHost();
}

void Home::endad_connected_slot()
{
    struct data user;
    user.type=ENDAD;
    user.username=username;
    user.value="123";
    ui->hintlable_3->setText("");
    ui->startattendancebtn->setStyleSheet("background-color:#000000;border-radius:7px;color:#ffffff;");
    socket->write(user.toByteArray());
}

void Home::endad_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    outputlog(receivedInfo.data);
    socket->disconnectFromHost();
}

void Home::addname_connected_slot()
{
    struct data user;
    user.type=NAMEADD;
    user.username=username;
    user.value=ui->funcinput->text();
    socket->write(user.toByteArray());
}

void Home::addname_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    outputlog("【"+ui->funcinput->text()+"】"+receivedInfo.data);
    ui->funcinput->clear();
    socket->disconnectFromHost();
}

void Home::getAllName_connected_slot()
{
    struct data user;
    user.type=NAMEGETALL;
    user.username=username;
    user.value="123";
    socket->write(user.toByteArray());
}

void Home::getAllName_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    QString receivedData(receivedInfo.data);
    QStringList records = receivedData.split("|", Qt::SkipEmptyParts);

    // 遍历每个记录

    ui->settable->setRowCount(records.size());
    for (int row = 0; row < records.size(); ++row) {
        // 以 '\n' 分割记录中的行
        QStringList lines = records[row].split("\n", Qt::SkipEmptyParts);

        // 遍历每行并添加到QTableWidget中
        for (int col = 0; col < lines.size(); ++col) {
            // 以 ':' 分割键和值
            QStringList keyValue = lines[col].split("&", Qt::SkipEmptyParts);

            QString key = keyValue[0].trimmed();
            QString value = keyValue[1].trimmed();
            QTableWidgetItem *item = new QTableWidgetItem(value);
            ui->settable->setItem(row, col, item);
            item->setTextAlignment(Qt::AlignCenter);
        }
    }
    ui->settable->setColumnWidth(0, 50);
    ui->settable->setColumnWidth(1, 100);
    ui->settable->setColumnWidth(2, 180);
    socket->disconnectFromHost();
}

void Home::deletename_connected_slot()
{


    QList<QTableWidgetItem *> selectedItems = ui->settable->selectedItems();
    if (!selectedItems.isEmpty()) {
        int selectedRow = selectedItems.first()->row();
        QTableWidgetItem *item1 = ui->settable->item(selectedRow, 0);
        QString content1 = item1->text();
        struct data user;
        user.type=NAMEDELETE;
        user.username=username;
        user.value=content1;
        socket->write(user.toByteArray());
    } else {
        qDebug() << "No row selected.";
    }
}

void Home::deletename_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    outputlog(receivedInfo.data);
    socket->disconnectFromHost();
}

void Home::searchname_connected_slot()
{
    struct data user;
    user.type=NAMESEARCH;
    user.username=username;
    user.value=ui->funcinput->text();
    socket->write(user.toByteArray());
}

void Home::searchname_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    QString receivedData(receivedInfo.data);
    QStringList records = receivedData.split("|", Qt::SkipEmptyParts);

    // 遍历每个记录

    ui->settable->setRowCount(records.size());
    for (int row = 0; row < records.size(); ++row) {
        // 以 '\n' 分割记录中的行
        QStringList lines = records[row].split("\n", Qt::SkipEmptyParts);

        // 遍历每行并添加到QTableWidget中
        for (int col = 0; col < lines.size(); ++col) {
            // 以 ':' 分割键和值
            QStringList keyValue = lines[col].split("&", Qt::SkipEmptyParts);

            QString key = keyValue[0].trimmed();
            QString value = keyValue[1].trimmed();
            QTableWidgetItem *item = new QTableWidgetItem(value);
            ui->settable->setItem(row, col, item);
            item->setTextAlignment(Qt::AlignCenter);
        }
    }
    ui->settable->setColumnWidth(0, 50);
    ui->settable->setColumnWidth(1, 100);
    ui->settable->setColumnWidth(2, 180);
    socket->disconnectFromHost();
}

void Home::getadList_connected_slot()
{
    struct data user;
    user.type=NAMEHISTORY;
    user.username=username;
    user.value="123";
    socket->write(user.toByteArray());
}

void Home::getadList_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    QString receivedData(receivedInfo.data);
    QStringList records = receivedData.split("|", Qt::SkipEmptyParts);

    // 遍历每个记录

    ui->settable->setRowCount(records.size());
    for (int row = 0; row < records.size(); ++row) {
        // 以 '\n' 分割记录中的行
        QStringList lines = records[row].split("\n", Qt::SkipEmptyParts);

        // 遍历每行并添加到QTableWidget中
        for (int col = 0; col < lines.size(); ++col) {
            // 以 ':' 分割键和值
            QStringList keyValue = lines[col].split("&", Qt::SkipEmptyParts);

            QString key = keyValue[0].trimmed();
            QString value = keyValue[1].trimmed();
            QTableWidgetItem *item = new QTableWidgetItem(value);
            ui->settable->setItem(row, col, item);
            item->setTextAlignment(Qt::AlignCenter);
        }
    }
    ui->settable->setColumnWidth(0, 50);
    ui->settable->setColumnWidth(1, 80);
    ui->settable->setColumnWidth(2, 80);
    ui->settable->setColumnWidth(3, 160);
    socket->disconnectFromHost();
}

void Home::stuad_connected_slot()
{
    struct data user;
    user.type=STUAD;
    user.username=username;
    user.value=teachEdit->text();
    user.value1=adkeyEdit->text();
    socket->write(user.toByteArray());
}

void Home::callname_connected_slot()
{
    struct data user;
    user.type=NAMECALL;
    user.username=username;
    user.value="123";
    user.value1="123";
    socket->write(user.toByteArray());
}

void Home::callname_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    outputlog(receivedInfo.data);
    socket->disconnectFromHost();
}

void Home::stuad_readyRead_slot()
{
    QByteArray rawData = socket->readAll();
    const char* rawCharArray = rawData.constData();
    socketinfo receivedInfo;
    memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));
    stuHintLable->setText(receivedInfo.data);
    stuHintLable->setStyleSheet("color:rgb(26, 136, 35);height:40px;");
    socket->disconnectFromHost();
}

void Home::on_startattendancebtn_clicked()
{
    if(ui->startattendancebtn->text()=="发起签到")
    {
        socket = new QTcpSocket(this);
        socket->connectToHost(ip,port);
        connect(socket,&QTcpSocket::connected,this,&Home::startad_connected_slot);
        connect(socket,&QTcpSocket::readyRead,this,&Home::startad_readyRead_slot);
        ui->startattendancebtn->setText("关闭签到");
    }else{
        socket = new QTcpSocket(this);
        socket->connectToHost(ip,port);
        connect(socket,&QTcpSocket::connected,this,&Home::endad_connected_slot);
        connect(socket,&QTcpSocket::readyRead,this,&Home::endad_readyRead_slot);
        ui->startattendancebtn->setText("发起签到");
        ui->startattendancebtn->setStyleSheet("background-color:#000000;border-radius:7px;color:#ffffff;");
    }

}

void Home::outputlog(QString msg)
{
    ui->adEdit->setText("【"+getCurretTime()+"】"+msg+"\n"+ui->adEdit->toPlainText());
}

QString Home::getCurretTime()
{
    QTime systime = QTime::currentTime();
    QString s = systime.toString("hh:mm:ss");
    return s;
}

QString Home::generateRandomString(int length) {
    const QString charset = "123456789abcdefghijklmnpqrstuvwxyz";
    QString randomString;

    for (int i = 0; i < length; ++i) {
        int randomIndex = QRandomGenerator::global()->bounded(charset.length());
        randomString.append(charset.at(randomIndex));
    }

    return randomString;
}

void Home::closeAllpage()
{
    ui->hintframe->hide();
    ui->hintframe_2->hide();
    ui->startattendancebtn->hide();
    ui->setmainframe->hide();
    ui->callnamebtn->hide();
}

void Home::openAdPage()
{
    ui->hintframe->show();
    ui->hintframe_2->show();
    ui->startattendancebtn->show();
    ui->callnamebtn->show();
    this->setWindowTitle("签到管理系统v1.0-签到");
}

void Home::openListPage()
{
    this->setWindowTitle("签到管理系统v1.0-记录");
    ui->setmainframe->show();
    ui->funcinput->hide();
    ui->addbtn->hide();
    ui->searchbtn->hide();
    ui->deletebtn->hide();
    ui->backbtn->hide();
    QStringList headerLabels;
    headerLabels << "ID" << "姓名" << "签到码" << "签到时间";
    ui->settable->setHorizontalHeaderLabels(headerLabels);
    ui->settable->verticalHeader()->setVisible(false);
    ui->settable->setColumnCount(4);
    ui->settable->setStyleSheet("QScrollBar:vertical {"
                                "    background: #ffffff;"
                                "    width: 10px;"
                                "    margin: 16px 0px 16px 0px;"
                                "}"
                                "QScrollBar::handle:vertical {"
                                "    background: rgb(78, 79, 79);"
                                "    min-height: 10px;"
                                "}"
                                "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
                                "    border: none;"
                                "    background: none;"
                                "}"
                                "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
                                "    background: none;"
                                "}"
                                "#settable{background-color:#ffffff;color:#000000;}"
                                "QTableWidget::item:selected {"
                                "    background-color: #eeeeee;"
                                "color:#000000;"
                                "}");
    socket = new QTcpSocket(this);
    socket->connectToHost(ip,port);
    connect(socket,&QTcpSocket::connected,this,&Home::getadList_connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Home::getadList_readyRead_slot);

    for (int row = 0; row < ui->settable->rowCount(); ++row) {
        for (int col = 0; col < ui->settable->columnCount(); ++col) {
            QTableWidgetItem *item = new QTableWidgetItem(QString("Row %1, Col %2").arg(row + 1).arg(col + 1));

            // 设置文本居中
            item->setTextAlignment(Qt::AlignCenter);
        }
    }
}

void Home::openSetPage()
{
    this->setWindowTitle("签到管理系统v1.0-设置");
    ui->setmainframe->show();
    ui->funcinput->show();
    ui->addbtn->show();
    ui->searchbtn->show();
    ui->deletebtn->show();
    ui->backbtn->show();
    QStringList headerLabels;
    headerLabels << "ID" << "姓名" << "添加时间";
    ui->settable->setHorizontalHeaderLabels(headerLabels);
    ui->settable->verticalHeader()->setVisible(false);
    ui->settable->setColumnCount(3);
    ui->settable->setColumnWidth(2, 500);
    ui->settable->setStyleSheet("QScrollBar:vertical {"
                              "    background: #ffffff;"
                              "    width: 10px;"
                              "    margin: 16px 0px 16px 0px;"
                              "}"
                              "QScrollBar::handle:vertical {"
                              "    background: rgb(78, 79, 79);"
                              "    min-height: 10px;"
                              "}"
                              "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
                              "    border: none;"
                              "    background: none;"
                              "}"
                              "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
                              "    background: none;"
                              "}"
                                "#settable{background-color:#ffffff;color:#000000;}"
                               "QTableWidget::item:selected {"
                                "    background-color: #eeeeee;"
                                "color:#000000;"
                                "}");
    socket = new QTcpSocket(this);
    socket->connectToHost(ip,port);
    connect(socket,&QTcpSocket::connected,this,&Home::getAllName_connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Home::getAllName_readyRead_slot);

    for (int row = 0; row < ui->settable->rowCount(); ++row) {
        for (int col = 0; col < ui->settable->columnCount(); ++col) {
            QTableWidgetItem *item = new QTableWidgetItem(QString("Row %1, Col %2").arg(row + 1).arg(col + 1));

            // 设置文本居中
            item->setTextAlignment(Qt::AlignCenter);
        }
    }

}



void Home::on_callbtn_2_clicked()
{
    closeAllpage();
    openListPage();
}


void Home::on_callbtn_clicked()
{
    closeAllpage();
    openAdPage();
}


void Home::on_callbtn_3_clicked()
{
    closeAllpage();
    openSetPage();
}


void Home::on_callnamebtn_clicked()
{
    socket = new QTcpSocket(this);
    socket->connectToHost(ip,port);
    connect(socket,&QTcpSocket::connected,this,&Home::callname_connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Home::callname_readyRead_slot);
}


void Home::on_addbtn_clicked()
{
    socket = new QTcpSocket(this);
    socket->connectToHost(ip,port);
    connect(socket,&QTcpSocket::connected,this,&Home::addname_connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Home::addname_readyRead_slot);
}


void Home::on_deletebtn_clicked()
{
    socket = new QTcpSocket(this);
    socket->connectToHost(ip,port);
    connect(socket,&QTcpSocket::connected,this,&Home::deletename_connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Home::deletename_readyRead_slot);


}


void Home::on_backbtn_clicked()
{
    // 创建 Widget 窗口
    Widget *w = new Widget();
    // 显示 Widget 窗口
    w->show();
    // 关闭当前 Home 窗口
    this->close();
}


void Home::on_searchbtn_clicked()
{
    socket = new QTcpSocket(this);
    socket->connectToHost(ip,port);
    connect(socket,&QTcpSocket::connected,this,&Home::searchname_connected_slot);
    connect(socket,&QTcpSocket::readyRead,this,&Home::searchname_readyRead_slot);
}



http://www.kler.cn/a/232281.html

相关文章:

  • 【解决】Layout 下创建槽位后,执行 Image 同步槽位位置后表现错误的问题。
  • ES6更新的内容中什么是proxy
  • Scala的Array
  • 【Docker】Mac安装Docker Desktop导致磁盘剩余空间较少问题如何解决?
  • 【安全科普】NUMA防火墙诞生记
  • github和Visual Studio
  • 文件上传-Webshell
  • 消息中间件:Puslar、Kafka、RabbigMQ、ActiveMQ
  • Canvas的js库:Konva.js-像操作DOM一样,操作canvas
  • 【机器学习】全网最全模型评价指标(性能指标、YOLOv5训练结果分析、轻量化指标、混淆矩阵详解)【基础收藏】
  • 基于SSM的网络在线考试系统(有报告)。Javaee项目。ssm项目。
  • C++服务器端开发(10):性能优化
  • MySQL基础查询篇(10)-正则表达式的基础使用
  • 关于数字图像处理考试
  • 【Java八股面试系列】JVM-内存区域
  • WebSocket+Http实现功能加成
  • 前端学习之路(6) npm详解
  • 计算机网络总结
  • Linux 36.2@Jetson Orin Nano基础环境构建
  • 前端开发中不同语言【react-i18next】
  • 交通 | 共乘出行(下):基于图结构的动态多时空供需网络的均衡度量方法
  • todolist的五种写法(原生、vue2、vue3、react类组件,react函数组件)
  • tee漏洞学习-翻译-2:探索 Qualcomm TrustZone的实现
  • python将Excel文档转成.db数据库文件
  • Vue2中v-for 与 v-if 的优先级
  • flask+pyinstaller实现mock接口,并打包到exe运行使用postman验证