Results 1 to 5 of 5

Thread: Qt View adds empty rows

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt View adds empty rows

    Show us some code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Qt View adds empty rows

    I am having a similar issue.

    Qt Code:
    1. --- mainwindow.cpp ----
    2.  
    3. this->drive_ = new Drive(driveName, this);
    4. this->ui->tableView->setModel(this->drive_);
    5. this->ui->treeView->setModel(this->drive_->tagTree_);
    6.  
    7. QObject::connect(this->ui->treeView,SIGNAL(clicked(const QModelIndex&)),
    8. this,SLOT(recalculate()));
    9. QObject::connect(this->ui->treeView,SIGNAL(viewReleased()),
    10. this->ui->tableView,SLOT(reset()));
    11. QObject::connect(this->ui->treeView,SIGNAL(viewReleased()),
    12. this->ui->tableView,SLOT(scrollToTop()));
    13.  
    14. ... unrelated code ...
    15.  
    16. void MainWindow::recalculate() {
    17. QModelIndexList selection = this->ui->treeView->selectionModel()->selectedIndexes();
    18. this->drive_->recalculate(selection);
    19. }
    20.  
    21.  
    22.  
    23. --- drive.cpp ----
    24.  
    25. int Drive::rowCount(const QModelIndex &parent) const {
    26. return results_.size();
    27. }
    To copy to clipboard, switch view to plain text mode 

    recalculate() is called whenever a new item in the treeView is clicked. It changes the underlying model for the tableView at which point its reset() and scrollToTop() slots are called.

    Say, for example, there are 25 items in the view before recalculate is called. If the result of the recalculation has 5 items, the tableView will show the information for the 5 items in the model and 20 empty rows. Apparently the reset doesn't remove those rows. Is there any way for these rows to be removed?

    rowCount() is defined as the size of the set of results returned by a recalculation.

    Thanks!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt View adds empty rows

    If your custom model does not emit the relevant signals when adjusting the row count then the view has no way to know the row count has changed. Since we cannot see your custom model code we are hard pressed to see the errors.

    You are resetting the view not the model, so only the selections, current item and visual position of the view are changed. This does not cause the view to assume the row or column count has changed.

  5. The following user says thank you to ChrisW67 for this useful post:

    Hardtobenormal (24th February 2013)

  6. #5
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Qt View adds empty rows

    Thanks for the help! My implementation of recalculate() in drive never called beginInsertRows(), endInsertRows(), beginRemoveRows() or endRemoveRows() so, like you had said, the view never knew that there was a change in the number of rows.

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.