StudentMainWindow
::StudentMainWindow(QString userID,
QWidget *parent
) : ui(new Ui::StudentMainWindow)
{
this->userID = userID;
ui->setupUi(this);
ui->userNameLabel->setText(userID);
connectToDatabase();
updateComboBox();
ui->databaseView->setModel(databaseModel);
updateView(ui->subjectsComboBox->currentText());
connect(ui
->subjectsComboBox,
SIGNAL(currentIndexChanged
(QString)),
this,
SLOT(updateView
(QString)));
}
void StudentMainWindow::connectToDatabase(){
database.setHostName("127.0.0.1");
database.setDatabaseName("studentmanagement");
database.setUserName("root");
database.setPassword("");
}
void StudentMainWindow::updateComboBox(){
if (!database.open()){
connectToDatabase();
}
if (query.exec("SELECT * FROM subject")){
while(query.next()){
subjects.append(query.value(1).toString());
}
ui->subjectsComboBox->addItems(subjects);
}else {
QMessageBox::critical(this,
"SQL Error",
"Could not retrieve subjects: </br>" + database.
lastError().
text(),
QMessageBox::Ok);
}
query.clear();
}
void StudentMainWindow
::updateView(QString subject
){ if (database.open()){
databaseModel->setTable("marks");
databaseModel->setFilter("marks.studentID = '" + userID + "' AND marks.subjectID = (SELECT subject.subjectID FROM subject WHERE subject.subjectName = '" + subject + "')");
databaseModel->select();
qDebug() << databaseModel->rowCount();
}else {
QMessageBox::critical(this,
"Error Connecting",
"Error Connecting to Database: </br>" + database.
lastError().
text(),
QMessageBox::Ok);
}
}
StudentMainWindow::StudentMainWindow(QString userID, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::StudentMainWindow)
{
this->userID = userID;
ui->setupUi(this);
ui->userNameLabel->setText(userID);
connectToDatabase();
updateComboBox();
databaseModel = new QSqlRelationalTableModel(this);
ui->databaseView->setModel(databaseModel);
updateView(ui->subjectsComboBox->currentText());
connect(ui->subjectsComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateView(QString)));
}
void StudentMainWindow::connectToDatabase(){
database = QSqlDatabase::addDatabase("QMYSQL");
database.setHostName("127.0.0.1");
database.setDatabaseName("studentmanagement");
database.setUserName("root");
database.setPassword("");
}
void StudentMainWindow::updateComboBox(){
if (!database.open()){
connectToDatabase();
}
QSqlQuery query;
QStringList subjects;
if (query.exec("SELECT * FROM subject")){
while(query.next()){
subjects.append(query.value(1).toString());
}
ui->subjectsComboBox->addItems(subjects);
}else {
QMessageBox::critical(this, "SQL Error", "Could not retrieve subjects: </br>" + database.lastError().text(), QMessageBox::Ok);
}
query.clear();
}
void StudentMainWindow::updateView(QString subject){
if (database.open()){
databaseModel->setTable("marks");
databaseModel->setFilter("marks.studentID = '" + userID + "' AND marks.subjectID = (SELECT subject.subjectID FROM subject WHERE subject.subjectName = '" + subject + "')");
databaseModel->select();
qDebug() << databaseModel->rowCount();
}else {
QMessageBox::critical(this, "Error Connecting", "Error Connecting to Database: </br>" + database.lastError().text(), QMessageBox::Ok);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks