userwindow.cpp 5.4 KB
Newer Older
Qiu Chuntao's avatar
Qiu Chuntao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include "userwindow.h"
#include "ui_userwindow.h"

UserWindow::UserWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::UserWindow)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose,1);

    table = new ShowTable(this);

    connect(this,SIGNAL(sendata(QSqlDatabase,QString)),table,SLOT(dowork(QSqlDatabase,QString)));
}

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

void UserWindow::getdata(QSqlDatabase db1,QString tablename1) {
    db = db1;
    tablename = tablename1;
}

void UserWindow::on_usrtable_clicked()
{
    emit sendata(db,"user");
    table->show();
}


//根据username查找用户,并将用户放入一张新表单显示到tableview窗口
void UserWindow::on_find_usr_clicked()
{
    QSqlDatabase db2;
    QString datasql;
    QSqlQuery sql_query(db2);
    QString sql = "create table if not exists "
                  "temtable(id integer primary key autoincrement,"
                  "username text not NULL ,password text not NULL);";
    if (!sql_query.exec(sql)) {
        return;
    }

    bool ok;
    QString text = QInputDialog::getText(this, tr("查询"),
                                 tr("查找用户"), QLineEdit::Normal,
Qiu Chuntao's avatar
Qiu Chuntao committed
49
                                 tr(""), &ok);
Qiu Chuntao's avatar
Qiu Chuntao committed
50
    if (ok) {
Qiu Chuntao's avatar
Qiu Chuntao committed
51 52 53 54
        if (text.isEmpty()) {
            QMessageBox::warning(this, "提示", "请输入需要查找的用户!", QMessageBox::Ok);
            return;
        }
Qiu Chuntao's avatar
Qiu Chuntao committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        QString str = QString("SELECT * FROM user WHERE username = '%1'").arg(text);
        QSqlQuery query(QSqlDatabase::database("HB"));
        if (!query.exec(str)) {
            QMessageBox::about(this,"提示","没有找到用户");
            return;
        }
        while (query.next()) {
            datasql = QString("insert into temtable(username,password) "
                          "values('%1','%2');").arg(query.value(1).toString()).arg(query.value(2).toString());
            QSqlQuery sql_query(db2);
            if (!sql_query.exec(datasql)) {
                QMessageBox::about(this,"提示","向临时表插入数据失败");
                return;
            }
        }
        emit sendata(db2,"temtable");
        table->show();

        datasql = QString("DELETE FROM temtable");
        if (!query.exec(datasql)) {
            QMessageBox::about(this,"提示","删除临时表失败!");
            return;
        }
    }
    else
        return;
}

void UserWindow::on_delet_usr_clicked()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("删除用户"),
                                 tr("查找用户"), QLineEdit::Normal,
Qiu Chuntao's avatar
Qiu Chuntao committed
88
                                 tr(""), &ok);
Qiu Chuntao's avatar
Qiu Chuntao committed
89
    if (ok) {
Qiu Chuntao's avatar
Qiu Chuntao committed
90 91 92 93
        if (text.isEmpty()) {
            QMessageBox::warning(this,"提示","输入为空",QMessageBox::Ok);
            return;
        }
Qiu Chuntao's avatar
Qiu Chuntao committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        QString str = QString("DELETE FROM user WHERE username = '%1'").arg(text);
        QSqlQuery sql_query(db);
        if (!sql_query.exec(str)) {
            QMessageBox::about(this,"提示","删除用户失败");
            return;
        }
        QMessageBox::about(this,"提示","删除成功");
    }
    else
        return;

}

void UserWindow::on_goback_clicked()
{
    this->parentWidget()->show();
    this->hide();
}

void UserWindow::on_updata_usr_clicked()
{
    bool ok;
    QString text = QInputDialog::getText(this, tr("修改用户信息"),
                                 tr("查找用户"), QLineEdit::Normal,
Qiu Chuntao's avatar
Qiu Chuntao committed
118
                                 tr(""), &ok);
Qiu Chuntao's avatar
Qiu Chuntao committed
119
    if (ok) {
Qiu Chuntao's avatar
Qiu Chuntao committed
120 121 122 123
        if (text.isEmpty()) {
            QMessageBox::warning(this,"提示","输入为空",QMessageBox::Ok);
            return;
        }
Qiu Chuntao's avatar
Qiu Chuntao committed
124 125 126 127 128 129
        int ret = QMessageBox::question(this, tr("修改选项"),
                                           "修改用户名/密码?(Yes-用户名|No-密码)",
                                           QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
        if (ret == QMessageBox::Yes ) {
            QString newname = QInputDialog::getText(this, tr("提示"),
                                         tr("修改用户名!"), QLineEdit::Normal,
Qiu Chuntao's avatar
Qiu Chuntao committed
130 131 132 133 134
                                         tr(""), &ok);
            if (newname.isEmpty()) {
                QMessageBox::warning(this,"提示","输入为空",QMessageBox::Ok);
                return;
            }
Qiu Chuntao's avatar
Qiu Chuntao committed
135 136 137 138 139 140 141 142 143 144 145 146 147
            QString str = QString("UPDATE user SET username = '%1' WHERE username = '%2'").arg(newname).arg(text);
            QSqlQuery sql_query(db);
            if (!sql_query.exec(str)) {
                QMessageBox::about(this,"提示","修改失败");
                return;
            }
            QMessageBox::about(this,"提示","修改成功");
        }

        if (ret == QMessageBox::No) {
            QString newpassword = QInputDialog::getText(this, tr("提示"),
                                         tr("修改密码"), QLineEdit::Normal,
                                         tr("请输入需要修改的密码"), &ok);
Qiu Chuntao's avatar
Qiu Chuntao committed
148 149 150 151
            if (newpassword.isEmpty()) {
                QMessageBox::warning(this,"提示","输入为空",QMessageBox::Ok);
                return;
            }
Qiu Chuntao's avatar
Qiu Chuntao committed
152 153 154 155 156 157 158 159 160 161 162 163
            QString str = QString("UPDATE user SET password = '%1' WHERE username = '%2'").arg(newpassword).arg(text);
            QSqlQuery sql_query(db);
            if (!sql_query.exec(str)) {
                QMessageBox::about(this,"提示","修改失败");
                return;
            }
           QMessageBox::about(this,"提示","修改成功");
        }
    }
    else
        return;
}