Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding a new row with QSqlTableModel and QDataWidgetMapper
QtWS25 Last Chance

Adding a new row with QSqlTableModel and QDataWidgetMapper

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scarleton
    wrote on last edited by
    #1

    I have an order form that, there are two tables used: Order and OrderItem. The Order contains all the standard stuff like username, address, etc. I do have a OrderModel that derives from QSqlTableModel and on the form there is a control for each relevant column. The one and only field I have ahead of time is the phone number. So here is my code to create a new row in the model, set the phone number and then wire up the mapper, but the phone number is never set. What am I missing?

    @_orderModel = new OrderModel(this, _db);
    _orderModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    if(dto.orderId)
    {
    _orderModel->setOrderIdFilter( dto.orderId);
    _orderModel->select();
    }
    else
    {
    _orderModel->insertRow(0);
    }

    _orderModel->setData(_orderModel->index(0, _orderModel->phoneNo()), dto.phonenumber);

    _orderMapper = new QDataWidgetMapper(this);
    _orderMapper->setModel( _orderModel);

    _orderMapper->addMapping(ui.firstNameEdit, _orderModel->fnameNo());
    _orderMapper->addMapping(ui.lastNameEdit, _orderModel->lnameNo());
    _orderMapper->addMapping(ui.address1Edit, _orderModel->address1No());
    _orderMapper->addMapping(ui.address2Edit, _orderModel->address2No());
    _orderMapper->addMapping(ui.cityEdit, _orderModel->cityNo());
    _orderMapper->addMapping(ui.stateEdit, _orderModel->stateNo());
    _orderMapper->addMapping(ui.zipEdit, _orderModel->zipNo());
    _orderMapper->addMapping(ui.emailEdit, _orderModel->emailNo());
    _orderMapper->addMapping(ui.phoneEdit, _orderModel->phoneNo());
    _orderMapper->toFirst();@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      Check whether setData returns true (i.e. the data were successfully set).

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dialingo
        wrote on last edited by
        #3

        You have used
        @setEditStrategy(QSqlTableModel::OnManualSubmit);@
        Is there a corresponding submitAll() in your code?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          scarleton
          wrote on last edited by
          #4

          Dialingo, there is, if I understand thing correctly, the phone number should show up without calling submitAll().

          I will check the return code what I get to my PC (on an iPad right now, cannot wait for it to be a MeeGo)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            scarleton
            wrote on last edited by
            #5

            opps, I never called QSqlTableModel::setTable(). Added an Q_ASSERT to the macro that gets the column id's so should have that problem again. Don't know why so many folks hate macro's, they sure speed up development when used correctly:)

            Ok, next issue. How do I get the primary key from this inserted row?

            In the beforeInsert() slot, the PK column is being set to not be generates so the SQLite's AUTOINCREMENT can do it thing:

            record.setGenerated(_orderModel->orderIdNo(), false);

            Here is the little catch, I know for an absolute FACT, in the very near feature the DB will be converted over to Firebird, which does NOT have a AUTOINCREMENT concept like most DB's.

            So ideally if there is a way to get the PK that will work today for SQLite and tomorrow for Firebird, that would be ideal!

            Sam

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved