Results 1 to 4 of 4

Thread: Inserting Record

  1. #1
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Inserting Record

    Hi there guys!

    I'm having a small problem here, whenever i try to insert a QSqlRecord into a QSqlTableModel, I know that a new row is inserted (QTableView shows that) but data simply isn't displayed in the view (the fields remain blank). Here is the code:

    in mainw.cpp
    Qt Code:
    1. //this makes the adddialog SLOT to instantiate an "Add" dialog
    2. void mainwindow::adddialogslot() {
    3. adddialog *add = new adddialog;
    4. add->show();
    5. if (add->Accepted) {
    6. model->insertRecord(-1,add->record);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    in adddialog.cpp
    Qt Code:
    1. void adddialog::allowinput() {
    2.  
    3. if (lineEdit_name->text().isEmpty() || lineEdit_surname->text().isEmpty()
    4. || lineEdit_phone->text().isEmpty() || lineEdit_phone1->text().isEmpty()
    5. || lineEdit_phone2->text().isEmpty() || lineEdit_phone3->text().isEmpty()
    6. || lineEdit_address->text().isEmpty() || lineEdit_year->text().isEmpty()
    7. || lineEdit_insurance->text().isEmpty() || lineEdit_HS->text().isEmpty()
    8. || lineEdit_diseases->text().isEmpty() || lineEdit_treat->text().isEmpty()
    9. || lineEdit_comp->text().isEmpty() || comboBox_day->currentText().isEmpty()
    10. || comboBox_month->currentText().isEmpty() || !(radioButton_M->isChecked() || radioButton_F->isChecked()) ) {
    11. QMessageBox::information(this, tr("Add Patient"), tr("Please fill in all of the required fields"));
    12. }
    13. else {
    14. record.setValue("name", lineEdit_name->text());
    15. record.setValue("surname", lineEdit_surname->text());
    16. if (radioButton_M->isChecked()) {
    17. record.setValue("gender", "M");
    18. }
    19. else {
    20. record.setValue("gender", "F");
    21. }
    22. record.setValue("birth date", comboBox_day->currentText() + "/" + comboBox_month->currentText() +"/"+ lineEdit_year->text());
    23. record.setValue("address", lineEdit_address->text());
    24. record.setValue("phone number", lineEdit_phone->text() + lineEdit_phone1->text() + lineEdit_phone2->text() + lineEdit_phone3->text());
    25. record.setValue("insurance number", lineEdit_insurance->text());
    26. record.setValue("health system", lineEdit_HS->text());
    27. record.setValue("diseases", lineEdit_diseases->text());
    28. record.setValue("treatments", lineEdit_treat->text());
    29. record.setValue("complications", lineEdit_comp->text());
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? Any code samples would be greatly appreciated.

    Thanks in advance,

    Nefastious

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Inserting Record

    Are you sure, your record is valid? A field name with a white space like "insurance number" seems odd to me and maybe you need to set the integer index and not only the name? "Debug" a record which was created by the model and check it with the one you create.

  3. #3
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Question Re: Inserting Record

    Well using the following:

    Qt Code:
    1. if (record.isEmpty()) {
    2. qDebug() << "It is empty";
    3. }
    To copy to clipboard, switch view to plain text mode 

    I am informed that the record is indeed empty.
    What exactly am I doing wrong? Instead of name should I have specified the index instead?

    Thanks in advance

  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: Inserting Record

    Qt Code:
    1. //this makes the adddialog SLOT to instantiate an "Add" dialog
    2. void mainwindow::adddialogslot() {
    3. adddialog *add = new adddialog;
    4. add->show();
    5. if (add->Accepted) {
    6. model->insertRecord(-1,add->record);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Line 4 opens a modeless dialog and immediately returns control. The user doesn't a have chance to do anything in the dialog before you grab the (empty) record and insert it. You probably wanted:
    Qt Code:
    1. add->exec();
    To copy to clipboard, switch view to plain text mode 
    to open a modal dialog to wait for user data entry and confirmation.
    Last edited by ChrisW67; 20th October 2009 at 04:29. Reason: Corrected fat-handed typing

Similar Threads

  1. Replies: 2
    Last Post: 25th June 2012, 10:54
  2. Problem while inserting the data into database
    By sudheer168 in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2008, 12:22
  3. problem: inserting a BLOB into sqlite3 with QT4.3
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2007, 12:38
  4. no record returns from stored procedure
    By mandal in forum Qt Programming
    Replies: 0
    Last Post: 26th April 2007, 14:45
  5. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50

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.