Results 1 to 5 of 5

Thread: Qt View adds empty rows

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt View adds empty rows

    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"

    Qt Code:
    1. StudentMainWindow::StudentMainWindow(QString userID, QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::StudentMainWindow)
    4. {
    5. this->userID = userID;
    6. ui->setupUi(this);
    7. ui->userNameLabel->setText(userID);
    8.  
    9. connectToDatabase();
    10. updateComboBox();
    11.  
    12. databaseModel = new QSqlRelationalTableModel(this);
    13. ui->databaseView->setModel(databaseModel);
    14.  
    15. updateView(ui->subjectsComboBox->currentText());
    16.  
    17. connect(ui->subjectsComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateView(QString)));
    18. }
    19.  
    20. void StudentMainWindow::connectToDatabase(){
    21. database = QSqlDatabase::addDatabase("QMYSQL");
    22. database.setHostName("127.0.0.1");
    23. database.setDatabaseName("studentmanagement");
    24. database.setUserName("root");
    25. database.setPassword("");
    26. }
    27.  
    28.  
    29. void StudentMainWindow::updateComboBox(){
    30. if (!database.open()){
    31. connectToDatabase();
    32. }
    33.  
    34. QSqlQuery query;
    35. QStringList subjects;
    36. if (query.exec("SELECT * FROM subject")){
    37. while(query.next()){
    38. subjects.append(query.value(1).toString());
    39. }
    40. ui->subjectsComboBox->addItems(subjects);
    41. }else {
    42. QMessageBox::critical(this, "SQL Error", "Could not retrieve subjects: </br>" + database.lastError().text(), QMessageBox::Ok);
    43. }
    44. query.clear();
    45. }
    46.  
    47. void StudentMainWindow::updateView(QString subject){
    48. if (database.open()){
    49. databaseModel->setTable("marks");
    50. databaseModel->setFilter("marks.studentID = '" + userID + "' AND marks.subjectID = (SELECT subject.subjectID FROM subject WHERE subject.subjectName = '" + subject + "')");
    51. databaseModel->select();
    52. qDebug() << databaseModel->rowCount();
    53. }else {
    54. QMessageBox::critical(this, "Error Connecting", "Error Connecting to Database: </br>" + database.lastError().text(), QMessageBox::Ok);
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by RaymondMcEvilly; 25th June 2012 at 10:12. Reason: updated contents

Similar Threads

  1. Replies: 4
    Last Post: 18th June 2012, 08:31
  2. QSortFilterProxyModel::insertRows always adds rows to the end
    By Agnostic Pope in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2012, 23:23
  3. Refreshing one or more rows in a table view
    By William Wilson in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2009, 00:10
  4. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  5. View decoration role empty on Windows
    By sebr in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2007, 10:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.