RaymondMcEvilly
25th June 2012, 09:48
Hi,
I am quite new to Qt programming. So please bare with me. I have a simple application that connects to a database using a QSQLRelationalTableModel. A QTableView is used to display the data in the model. Running the application works perfectly. The amount of rows in the table view is exactly the same as the amount of records in the model.
Now, when a new filter is applied on the model (using setFilter()) and calling select() again, the table is updated as it should, i.e. the new data is displayed in the table. The problem is, extra empty rows are added to the bottom of the table. The amount of rows that are "added" is exactly the same amount of rows that were previously in the table, i.e. before a new filter was applied. Thus it seems as if the previous data is removed out of the cells, the empty rows are kept, and the new rows are added on top of the old rows.
How can I get rid of these empty rows quick and simple? I have been looking everywhere for a function that will sort of reset the view to only display the data in the model (and to remove any old rows).
Some code:
#include "studentmainwindow.h"
#include "ui_studentmainwindow.h"
#include "QDebug"
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);
}
}
I am quite new to Qt programming. So please bare with me. I have a simple application that connects to a database using a QSQLRelationalTableModel. A QTableView is used to display the data in the model. Running the application works perfectly. The amount of rows in the table view is exactly the same as the amount of records in the model.
Now, when a new filter is applied on the model (using setFilter()) and calling select() again, the table is updated as it should, i.e. the new data is displayed in the table. The problem is, extra empty rows are added to the bottom of the table. The amount of rows that are "added" is exactly the same amount of rows that were previously in the table, i.e. before a new filter was applied. Thus it seems as if the previous data is removed out of the cells, the empty rows are kept, and the new rows are added on top of the old rows.
How can I get rid of these empty rows quick and simple? I have been looking everywhere for a function that will sort of reset the view to only display the data in the model (and to remove any old rows).
Some code:
#include "studentmainwindow.h"
#include "ui_studentmainwindow.h"
#include "QDebug"
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);
}
}