Results 1 to 5 of 5

Thread: Adding new record to database from QDataWidgetMapper

  1. #1
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Adding new record to database from QDataWidgetMapper

    Hello, i am trying to make a small database redactor with QDataWidgetMapper as edit field for data and qtableview. Pressing button "add new parameter" must add a new record to model with current data from QDataWidgetMapper. Currentrly a managed to add an empty record in the end af model. How must i do it and what is the best way?
    Adding some source code with model and mapper:

    Qt Code:
    1. filterModel = new QSortFilterProxyModel;
    2. filterModel->setSourceModel(modelt);
    3. filterModel->setFilterKeyColumn(2);
    4. mapper = new QDataWidgetMapper(this);
    5. mapper->setModel(filterModel);
    6. mapper->addMapping(le1, 2);
    7. mapper->addMapping(le2, 1);
    8. mapper->addMapping(le3, 3);
    9. mapper->addMapping(le4, 5);
    10. mapper->addMapping(le5, 6);
    11. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    12. ......
    13. QPushButton* pcmd3 = new QPushButton("Add new parameter");
    14. pcmd3->setIcon(QIcon(":/icons/16x16/actions/add.png"));
    15. connect(pcmd3, SIGNAL(clicked()), this, SLOT(insertRow()));
    To copy to clipboard, switch view to plain text mode 
    and slot:
    Qt Code:
    1. void MainWindow::insertRow()
    2. {
    3. filterModel->insertRow(filterModel->rowCount());
    4. mapper->toLast();
    5. mapper->submit();
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adding new record to database from QDataWidgetMapper

    The "current data from QDataWidgetMapper" belongs to another row in the table. Your code creates a new 'blank' row in the model and positions the mapper there, causing it to display the new 'blank' row. If you want to duplicate the original row in the model into the new row then you should code it to do so. I don't really see the problem here.

  3. #3
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding new record to database from QDataWidgetMapper

    Mapper is connected with tableview:
    Qt Code:
    1. connect(view->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    I meant that i select some row in view, mapper displays it, i make some changes in lineedits without submitting and press "add new parameter", and it creates a new record in model with new data from mapper.

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

    Default Re: Adding new record to database from QDataWidgetMapper

    Yes. If you want to create a new row in the model containing the current values in the widgets the QDataWidgetMapper is also using then write some code to do that. Your current code does not and QDataWidgetMapper will not magically do it for you.

  5. #5
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding new record to database from QDataWidgetMapper

    Made in such way. But how can i place mapper on it and update tableview to display new record?


    Qt Code:
    1. void MainWindow::insertRow()
    2. {
    3. QSqlDatabase db=QSqlDatabase::database();
    4.  
    5. QString sqlstr=QString("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\");")
    6. .arg(le2->text())
    7. .arg(le1->text())
    8. .arg(le3->text())
    9. .arg(le4->text())
    10. .arg(le5->text());
    11.  
    12.  
    13. QSqlQuery q(db);
    14. if(q.exec(sqlstr)==false)
    15. {
    16. QMessageBox *pmsg = new QMessageBox;
    17. pmsg->setText("Не могу соединиться с базой данных");
    18. pmsg->setInformativeText(q.lastError().text());
    19. pmsg->exec();
    20. return;
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 16th June 2012, 00:16
  2. extract record from database
    By sattu in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2011, 15:23
  3. How to move QDataWidgetMapper to a specific record ?
    By marcvanriet in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2010, 20:51
  4. Replies: 1
    Last Post: 4th October 2010, 00:46
  5. Replies: 3
    Last Post: 4th August 2010, 18:51

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.