Results 1 to 6 of 6

Thread: Editing QSqlRelationalTableModel - but not through the usual way

  1. #1
    Join Date
    Jul 2006
    Posts
    8
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Post Editing QSqlRelationalTableModel - but not through the usual way

    Hi,

    in my app. i use a qtableview with a qsqlrelationaltablemodel. User can not edit data directly in the view but via a "form" below the view. For example the "foreign" fields can be set using a ComboBox (just as if i would use the view's delegate).
    The model is set to OnManualSumbit mode, so on update first i call setRecord and than submitAll(). And submitAll drops an error - incorrect integer value: "theselectedforeignname" for column groupid.
    Here's two pieces of code:

    [Init]
    Qt Code:
    1. mView = new QTableView;
    2. mRelationalModel = new SqlRelationalTableModel(this,QSqlDatabase::database());
    3. mRelationalModel->setTable("table");
    4. mRelationalModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. mRelationalModel->setRelation(5, QSqlRelation("othertable","id","name"));
    6. if ( ! mRelationalModel->select() ) {
    7. QMessageBox::information(this,"Error",mRelationalModel->lastError().text());
    8. return false;
    9. }
    10. mView->setModel(mRelationalModel);
    11. mView->setItemDelegate(new QSqlRelationalDelegate(mView));
    12. mView->setSelectionBehavior(QAbstractItemView::SelectRows);
    13. mView->setSelectionMode(QAbstractItemView::SingleSelection);
    14. mView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    15. mView->setSortingEnabled(true);
    16. mView->sortByColumn(0,Qt::AscendingOrder);
    17. mView->verticalHeader()->hide();
    18. mView->show();
    To copy to clipboard, switch view to plain text mode 


    [The update code]

    Qt Code:
    1. if ( ! mView->currentIndex().isValid() ) return;
    2. int row = mView->currentIndex().row();
    3. QSqlRecord record = mRelationalModel->record(row);
    4. record.setValue("field1",field1Editor->text());
    5. record.setValue("field2",field2Editor->text());
    6. //more fields....
    7. //and this is the foreign field
    8. record.setValue("name",foreignfieldCombo->currentText());
    9.  
    10. if ( mRelationalModel->setRecord(row, record) ) {
    11. //on second try this two lines were added and record.setValue("name"...was commented out
    12. //QModelIndex idx = mRelationalModel->index(row,5);
    13. //mRelationalModel->setData(idx, foreignfieldCombo->currentText(), Qt::EditRole);
    14. if ( ! mRelationalModel->submitAll() ) {
    15. QMessageBox::critical(this, "Error", mRelationalModel->lastError().text());
    16. mRelationalModel->revertRow(row);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 


    As you can see i tried to set foreign data with record.setValue and also with this two lines of code:
    Qt Code:
    1. QModelIndex idx = mRelationalModel->index(row,5);
    2. mRelationalModel->setData(idx, foreignfieldCombo->currentText(), Qt::EditRole);
    To copy to clipboard, switch view to plain text mode 

    The first method did nothing and submitAll returned false
    The second method can succesfully update the foreign field data in the view, but submitAll still couldn't write record back to db.

    So, how should i do this correctly?
    Should i "translate" the selected name into the corresponding id value myself?

    Thanks:
    vr

  2. #2
    Join Date
    May 2006
    Posts
    788
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 48 Times in 46 Posts

    Default Re: Editing QSqlRelationalTableModel - but not through the usual way

    How you connect to db... ? driver ?


    Qt Code:
    1. if ( ! mRelationalModel->submitAll() ) {
    2. QMessageBox::critical(this, "Error", mRelationalModel->lastError().text());
    3. mRelationalModel->revertRow(row);
    4. }
    To copy to clipboard, switch view to plain text mode 
    error here waht is mRelationalModel->lastError().text() on save ....

    if You use mysql check the model from http://www.qtcentre.org/forum/f-qt-s...dmin-6807.html

    and set the now allow to edit field... run on all table wo having index...

  3. #3
    Join Date
    Jul 2006
    Posts
    8
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: Editing QSqlRelationalTableModel - but not through the usual way

    Quote Originally Posted by patrik08 View Post
    How you connect to db... ? driver ?



    error here waht is mRelationalModel->lastError().text() on save ....

    if You use mysql check the model from http://www.qtcentre.org/forum/f-qt-s...dmin-6807.html

    and set the now allow to edit field... run on all table wo having index...
    This is a mysql5 db, and submitAll drops the following error:

    drivertext = unable to execute statement
    databasetext = Incorrect integer value: 'theselectedname' for column 'groupid' at row 1
    error number = 1366 which is ER_TRUNCATED_WRONG_VALUE_FOR_FIELD


    db connection? the simple way
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2. db.setDatabaseName("dbname");
    3. db.setUserName("username");
    4. db.setPassword("pswd");
    5. db.setPort(dbPort);
    6. if ( db.open() )
    To copy to clipboard, switch view to plain text mode 
    ...etc, etc...

    If i enable direct editing via the qsqlrelationaldelegate - everything works fine.

  4. #4
    Join Date
    May 2006
    Posts
    788
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 48 Times in 46 Posts

    Default Re: Editing QSqlRelationalTableModel - but not through the usual way

    Quote Originally Posted by vr View Post
    This is a mysql5 db, and submitAll drops the following error:
    If i enable direct editing via the qsqlrelationaldelegate - everything works fine.
    O yes model the model is correct if a field is int open and save as int..... string ecc...

    if you assign other format to column 'groupid' ..... sql error emit error ...


    debug here ...


    Qt Code:
    1. QModelIndex idx = mRelationalModel->index(row,5);
    2. mRelationalModel->setData(idx, foreignfieldCombo->currentText(), Qt::EditRole);
    To copy to clipboard, switch view to plain text mode 
    idx->isValid() ?

    you can use

    qDebug() << "### idx->isValid() -> " << idx->isValid();

    to see live the result ..

    append
    CONFIG += console
    on pro file...

    post the result of

    SHOW COLUMNS FROM tablename

    to know column 'groupid' format....



    try this model ...

    http://ppk.ciz.ch/qt_c++/mysql_model...YSQL_Model.zip

    unzip cd dir
    qmake && make...

  5. #5
    Join Date
    Jul 2006
    Posts
    8
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: Editing QSqlRelationalTableModel - but not through the usual way

    hi patrik08,

    i'm afraid you misunderstood me. I know this is wrong this way, but don't understand why.

    If i edit the model directly via the view's relationaldelegate, everything is OK. If i set the data myself, it is not...

    Simple example:
    main table: int id, varchar name, int foreignid
    foreign table: int id, varchar name
    QSqlRelationalModel displays data in the following way (after you set a QSqlRelation(2,"id","name"))
    View shows the following: id, name, foreignname.
    If you edit foreignname delegate creates a combobox filled with names from foreigntable.
    After you choose one of them, on save the correct foreignid will be written back to the main table - it is correct, isn't it.
    Of course if you check the model's record the fileds are the following:
    id, name, foreignname.

    So my question is: what more does the delegate or the model than me when set the foreign data back to the model.

    If i call record.setValue("foreignname","choosenname") is not enough.
    If i call the two lines
    Qt Code:
    1. //yes, the index is valid!
    2. QModelIndex idx = mRelationalModel->index(row,5);
    3. mRelationalModel->setData(idx, "choosenname", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    the view is updated correctly, but data cannot be written back to db - that means foreignname is not mapped back to the correct id value.

    Of course i can write a simple update statement instead, but i'm sure it must work using only the model's public functions.

    vr
    Last edited by vr; 24th May 2007 at 09:45.

  6. #6
    Join Date
    Jul 2006
    Posts
    8
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Unhappy Re: Editing QSqlRelationalTableModel - but not through the usual way

    OK, i solved the problem but it is not a clear solution.

    I found this in qt doc:

    bool QSqlRelationalTableModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) [virtual]
    Sets the data for the role in the item with the specified index to the value given. Depending on the edit strategy, the value might be applied to the database at once, or it may be cached in the model.
    Returns true if the value could be set, or false on error (for example, if index is out of bounds).
    For relational columns, value must be the index, not the display value.

    So i search for the id value of the selected foreign name and call setData with this id value instead of the combo's current text and it works.

    But normally neither the model, the view nor the delegate allow me to get the selected foreign columns mapped id value.
    Obviously the delegate finds it itself internally and calls setData with the found index value.
    I thought i don't have to do it myself
    Last edited by vr; 24th May 2007 at 13:12.

Similar Threads

  1. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 19:46
  2. Editing properties of groups of widgets
    By Dusdan in forum Qt Tools
    Replies: 2
    Last Post: 18th August 2006, 13:51
  3. Replies: 5
    Last Post: 18th July 2006, 23:31
  4. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 12:03
  5. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 10:56

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
  •  
Qt is a trademark of The Qt Company.