Results 1 to 3 of 3

Thread: updating QTableView with new qsl columns

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default updating QTableView with new qsl columns

    I'm writing an application using QtSql and the columns of my tables can't be determined when the table is created. When I add columns to the database, I'm not able to add the column to the table view and I may also get extra empty rows added to my table view. I wrote a small example which demonstrates the problem. DOes anyone have any suggestions?
    Qt Code:
    1. class MyTable : public QTableView
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyTable() : QTableView() {
    7. }
    8.  
    9. };
    10.  
    11. int main(int argv, char *args[])
    12. {
    13. QApplication app(argv, args);
    14.  
    15. // Create an im memory database
    16. QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    17. db.setDatabaseName( ":memory:" );
    18. if ( !db.open() )
    19. {
    20. qCritical( "ERROR in opening the internal database" );
    21. }
    22.  
    23. // Create a table in the database
    24. QSqlQuery query = db.exec(QString("create table test (id INTEGER PRIMARY KEY AUTOINCREMENT)"));
    25.  
    26. // Create a tavle model for the table view
    27. model->setTable("test");
    28. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    29. model->select();
    30.  
    31. // Cteate a table view and display it
    32. QTableView table;
    33. table.setModel(model);
    34. table.show();
    35.  
    36.  
    37. // Add a column in the table using a query
    38. query.exec("ALTER TABLE test ADD col2 VARCHAR(50);");
    39. if (query.isActive())
    40. qDebug() << "query active after column insertion" ;
    41. // Add a row to the column
    42. query.exec("INSERT INTO test ('col2') VALUES ('something');");
    43. if (query.isActive())
    44. qDebug() << "query active after row insertion" ;
    45.  
    46.  
    47. // reload the data? Not sure if this is needed or valid
    48. model->select();
    49.  
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 

    Below is what my table view looks like with the empty row and missing column:
    Last edited by sylvainb; 28th January 2009 at 16:58.

Similar Threads

  1. Don't resize columns - QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2009, 16:59
  2. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 08:30
  3. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 09:17
  4. autoexpanding QTableView columns
    By ucntcme in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2008, 09:28
  5. Ensure columns in qtableview uses all available space
    By oysteinpettersen in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 11:28

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.