Results 1 to 5 of 5

Thread: Clicking QTableView's Corner Button does not select all rows

  1. #1
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Clicking QTableView's Corner Button does not select all rows

    I have a QTableView wich its corner button is enabled. Problem is when I click the button, only a portion of rows gets selected -in my case the first 256 rows, which are the currently loaded rows.

    Here is an example:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName("D:\\tempDB\\mydb");
    10.  
    11. if (db.open()) {
    12. QSqlQuery query;
    13. QString str = "title";
    14.  
    15. query.exec("CREATE TABLE " + str + "(\
    16. id INTEGER PRIMARY KEY AUTOINCREMENT,\
    17. title1 VARCHAR(255),\
    18. title2 VARCHAR(255))");
    19.  
    20. model.setTable("title");
    21. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    22.  
    23. QSqlRecord rec1 = db.record("title");
    24. QSqlRecord rec4, rec3, rec2;
    25.  
    26. rec4 = rec3 = rec2 = rec1;
    27.  
    28. rec1.setValue("title1", "test1");
    29. rec1.setValue("title2", "test11");
    30. rec2.setValue("title1", "test2");
    31. rec2.setValue("title2", "test22");
    32. rec3.setValue("title1", "test3");
    33. rec3.setValue("title2", "test33");
    34. rec4.setValue("title1", "test4");
    35. rec4.setValue("title2", "test44");
    36.  
    37. db.transaction();
    38. model.insertRows(0, 1024);
    39.  
    40. for (int i = 0; i < 1024;)
    41. {
    42. model.setRecord(i++, rec1);
    43. model.setRecord(i++, rec2);
    44. model.setRecord(i++, rec3);
    45. model.setRecord(i++, rec4);
    46. }
    47.  
    48. model.submitAll();
    49.  
    50. db.commit();
    51.  
    52. model.select();
    53.  
    54. QTableView view;
    55. view.setModel(&model);
    56. view.resize(640, 480);
    57. view.setCornerButtonEnabled(true);
    58. view.show();
    59.  
    60. return app.exec();
    61. }
    62. else
    63. {
    64. return 1;
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

    Running this example, clicking the corner button before scrolling down the view to the bottom, then scrolling the view down, will reproduce the problem.


    Added after 18 minutes:


    I know I can force the model to fetch all the data by putting
    Qt Code:
    1. while (model.canFetchMore())
    2. model.fetchMore();
    To copy to clipboard, switch view to plain text mode 
    in the code, but shouldn't it load all when I click the corner button?
    Last edited by Ashkan_s; 19th September 2012 at 08:56. Reason: reformatted to look better

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Clicking QTableView's Corner Button does not select all rows

    I found a solution here in the forum. I have re-implemented the QTableView::selectAll() in subclass, here is the code

    Qt Code:
    1. void TableView::selectAll()
    2. {
    3. QSqlTableModel *model = (QSqlTableModel *)(this->model());
    4. while(model->canFetchMore())
    5. {
    6. model->fetchMore();
    7. }
    8.  
    9. QTableView::selectAll();
    10. }
    To copy to clipboard, switch view to plain text mode 

    But my question still stands:
    shouldn't it load all when I click the corner button?

  3. #3
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Clicking QTableView's Corner Button does not select all rows

    It seems everything is working based on the number of currently loaded rows. Scroll to bottom does not scrolls to bottom because the model loads new rows, or QSqlTableModel::rowCount() returns the number of loaded rows.

  4. #4
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Re: Clicking QTableView's Corner Button does not select all rows

    I have changed the example that I had sent in the first post. Now there is a QSortFilterProxyModel between QSqlTableModel and QTableView. There's also a QLineEdit to filter the table rows.

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName(":memory:");
    10.  
    11. if (db.open()) {
    12. QSqlQuery query;
    13. QString str = "title";
    14.  
    15. query.exec("CREATE TABLE " + str + "(\
    16. id INTEGER PRIMARY KEY AUTOINCREMENT,\
    17. title1 VARCHAR(255),\
    18. title2 VARCHAR(255))");
    19.  
    20. model.setTable("title");
    21. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    22.  
    23. proxy.setSourceModel(&model);
    24. proxy.setDynamicSortFilter(true);
    25. proxy.setFilterKeyColumn(2);
    26.  
    27. QSqlRecord rec1 = db.record("title");
    28. QSqlRecord rec5, rec4, rec3, rec2;
    29.  
    30. rec5 = rec4 = rec3 = rec2 = rec1;
    31.  
    32. rec1.setValue("title1", "test1");
    33. rec1.setValue("title2", "test11");
    34. rec2.setValue("title1", "test2");
    35. rec2.setValue("title2", "test22");
    36. rec3.setValue("title1", "test3");
    37. rec3.setValue("title2", "test33");
    38. rec4.setValue("title1", "test4");
    39. rec4.setValue("title2", "test44");
    40. rec5.setValue("title1", "table5");
    41. rec5.setValue("title2", "table55");
    42.  
    43. db.transaction();
    44. model.insertRows(0, 10240);
    45.  
    46. for (int i = 0; i < 10240;)
    47. {
    48. if(i % 512 != 0)
    49. model.setRecord(i++, rec1);
    50. else
    51. model.setRecord(i++, rec5);
    52.  
    53. model.setRecord(i++, rec2);
    54. model.setRecord(i++, rec3);
    55. model.setRecord(i++, rec4);
    56. }
    57.  
    58. model.submitAll();
    59.  
    60. db.commit();
    61.  
    62. model.select();
    63.  
    64. QTableView view;
    65. view.setModel(&proxy);
    66. view.resize(640, 480);
    67. view.show();
    68.  
    69. le.show();
    70.  
    71. QObject::connect(&le, SIGNAL(textChanged(QString)), &proxy, SLOT(setFilterFixedString(QString)));
    72.  
    73. return app.exec();
    74. }
    75. else
    76. {
    77. return 1;
    78. }
    79. }
    To copy to clipboard, switch view to plain text mode 

    After running this, typing "ta" in the lineEdit to search for "table55", updates the view and one row appears. Now rotating the wheel button of mouse will cause other rows to be appear one by one.
    Forcing the model to fetch all rows solves this problem.

    My question is: To avoid fetching all rows, do I have to write my own SQL queries for filtering? Is there any other way?

  5. #5
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Post Re: Clicking QTableView's Corner Button does not select all rows

    OK, there is another way: Calling fetchMore() while the view has enough space to show more rows without scrolling.

Similar Threads

  1. Select item on double clicking in QComboBox
    By tommnaj in forum Newbie
    Replies: 1
    Last Post: 16th February 2012, 10:55
  2. Replies: 2
    Last Post: 1st February 2011, 19:35
  3. QTableView: Select multiple rows?
    By realdarkman71 in forum Newbie
    Replies: 2
    Last Post: 9th December 2010, 21:36
  4. Accessing the Table Corner Button
    By jmc in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2010, 20:40
  5. Replies: 1
    Last Post: 7th July 2007, 09:03

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.