Results 1 to 7 of 7

Thread: QTableView and QSQLTableModel

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTableView and QSQLTableModel

    Hello everybody

    QT:4.1.1

    I am able with this code to insert a Row in my table:
    Qt Code:
    1. QString table = ui.tabelle_le->text();
    2. model->setTable(table);
    3. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    4. model->select();
    5.  
    6. QSqlRecord rec = model->record();
    7. ui.tableView->setModel(model);
    8. ui.tableView->show();
    9.  
    10.  
    11. rec.setValue("status", "test");
    12. model->insertRecord(-1, rec);
    To copy to clipboard, switch view to plain text mode 

    My problem is, that i have some tables, so i am searching a solution how to implement my function so that i have not to write every columns like:
    table1
    Qt Code:
    1. rec.setValue("status", "test");
    2. rec.setValue("date", "test");
    3. rec.setValue("price", "test");
    4. .
    5. .
    6. .
    To copy to clipboard, switch view to plain text mode 
    table2
    Qt Code:
    1. rec.setValue("address", "test");
    2. rec.setValue("tel", "test");
    3. rec.setValue("mobil", "test");
    4. .
    5. .
    6. .
    To copy to clipboard, switch view to plain text mode 
    I would like to insert a empty row: modal->insertRecord(-1, rec); fill cells and save it
    Can somebody see the problem? Have somebody a idea how to implement this dynamic?
    Last edited by raphaelf; 4th March 2006 at 15:08.
    Think DigitalGasoline

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

    Default Re: QTableView and QSQLTableModel

    Maybe something like this:

    Qt Code:
    1. QList< QPair< QString, QVariant > >list;
    2. list << QPair<QString, QVariant>("fld1", 10);
    3. list << QPair<QString, QVariant>("fld2", "xxx");
    4. list << QPair<QString, QVariant>("fld3", "2005-01-01");
    5. //...
    6.  
    7. QSqlRecord rec = model->record();
    8. foreach(QPair<QString, QVariant> item, list){
    9. rec.setValue(item.first, item.second);
    10. }
    11. // ...
    To copy to clipboard, switch view to plain text mode 

    Of course you can construct the list in any way you want. You could use a QMap instead and have something like this:

    Qt Code:
    1. QMap<QString, QVariant> map;
    2. map["fld1"] = 10;
    3. map["fld2"] = "xxx";
    4. map["fld3"] = "2005-01-01";
    5. //...
    6. foreach(QString key, map.keys()){
    7. record.setValue(key, map[key]);
    8. }
    9. //...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView and QSQLTableModel

    Hi Wysota,
    I think i have not explain exactly again my problem
    or i cant see what you mean

    ok,

    I have a combobox with all my tables names from database x.
    I have a tableview where i can show my table by clicking a button.

    I want to click a button and insert a empty row in my tableView.
    The User can fill the cells (Maybe 2, or 18 colums) and save Database
    I am searching a solution how to insert this row in the table without to implement fix in my code :
    Qt Code:
    1. //status is a column name, i dont want to implement all columns from my database
    2. rec.setValue("status", "test");
    To copy to clipboard, switch view to plain text mode 

    Its not possible to implement something dynamic like this:
    1. Show table that i want to insert a new row
    2. insert a empty row in my QTableView
    3. User can fill the fileds
    4. getColumnnames from current Table ind insert Values(cell1, cell2)

    If you mean something like this, i have to understand your code
    Can you see my problem?If not i have to explain again

    Maybe this function is allready implemented by qt but i dont know which one
    Last edited by raphaelf; 4th March 2006 at 15:54.
    Think DigitalGasoline

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

    Default Re: QTableView and QSQLTableModel

    You have to explain again I don't have the slightest idea what your problem is

    Can't you just use an editable model and let Qt handle everything?

    What is exactly your problem? You don't know the field names or what?

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView and QSQLTableModel

    Its not easy to bring this on text

    I try again:

    I am testing QSqlTableView class for update, delete and insert a new dataset (record) in a Table.

    My situation now:
    i am able to update a Table like this (really easy, i had just to change the EditStrategy ):
    Qt Code:
    1. QString table = ui.tabelle_le->text();
    2. model->setTable(table);
    3. //So kann man jeder Feld update: OnFiledChange
    4. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    5. model->select();
    6. ui.tableView->setModel(model);
    7. ui.tableView->show();
    To copy to clipboard, switch view to plain text mode 
    i am able too, to delete a dataset like this(very simple):
    Qt Code:
    1. .
    2. .
    3. model->removeRows(x,1);
    To copy to clipboard, switch view to plain text mode 
    What i need now, is to find a simple way, how to insert a new dataset over my tableView
    I now that i can use this:
    Qt Code:
    1. rec.setValue("status", "test");
    2. model->insertRecord(-1, rec);
    To copy to clipboard, switch view to plain text mode 
    So my problem is:
    1. I have 18 tables
    2. every table has between 2 and 23 columns.
    3. How to insert a new dataset over my TableView in a easy Way independent of which table is set as Modal?

    I hope i could explain better, if not i shoul lern more english
    Think DigitalGasoline

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

    Default Re: QTableView and QSQLTableModel

    Just insert an empty row into your model using insertRow() or insertRecord() with an empty record and order the view to go into edit mode on the newly inserted row.

  7. #7
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView and QSQLTableModel

    Hi Wysota!
    finally i am able now to input a new rec
    I hope you mean like that:
    Qt Code:
    1. void MainWindow::insertNewRow()
    2. {
    3. QString table = ui.tabellen_cb->currentText();
    4. model->setTable(table);
    5. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    6. model->select();
    7.  
    8. ui.tableView->setModel(model);
    9. ui.tableView->show();
    10.  
    11. //Just insert an empty row into your model using insertRow() or insertRecord()
    12. //with an empty record
    13. QSqlRecord rec = model->record();
    14. model->insertRecord(-1, rec);
    15.  
    16. //and order the view to go into edit mode on the newly inserted row.
    17. model->setEditStrategy(QSqlTableModel::OnRowChange);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

Similar Threads

  1. Replies: 2
    Last Post: 6th January 2009, 21:55
  2. QSqlTableModel and QTableView selection problems
    By innerhippy in forum Qt Programming
    Replies: 5
    Last Post: 19th December 2008, 06:48
  3. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 12:50
  4. QTableView and QSqlTableModel problem
    By Misenko in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2008, 22:58
  5. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 14:22

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.