void VideoWindow::loadUsers(){ Database db; QList<Users> users = db.getAllUsers(); QStringList completions; for(int l = 0; l < users.size(); l++){ Users u = users.at(l); QString name = u.getName().toString(); completions.push_back(name); } QCompleter *completer = new QCompleter(completions, this); completer->setCompletionMode(QCompleter::PopupCompletion); this->ui->observer->setCompleter(completer); }
int Database::insertUser(Users u) { QVariant name = u.getName().toString().toLower(); if(!this->userExist(name)){ this->query.prepare("INSERT INTO Users (name) VALUES (:name);"); this->query.bindValue(":name",name); if(!this->query.exec()){ this->showError(this->query.lastError()); return -1; } return this->query.lastInsertId().toInt(); } this->showError(QMessageBox::tr("Esse usuário já existe!")); return 0; }
int Database::editUsers(Users u) { QVariant name = u.getName().toString().toLower(); if(this->userExist(name) > 0){ this->showError(QMessageBox::tr("Esse usuário já existe")); return 0; } if(this->userExist(name) >= 0){ this->query.prepare("UPDATE Users SET name=? WHERE Users.id=?;"); this->query.addBindValue(name); this->query.addBindValue(u.getId()); if(!this->query.exec()){ this->showError(this->query.lastError()); return -1; } return 1; } QMessageBox::information(0, QMessageBox::tr("Erro"),QMessageBox::tr("Usuário não encontrado!")); return 0; }