QDataWidgetMapper not saving data in table!



  • I have one form below with all mappings with QDataWidgetMapper, but for some problem it's not saving data on database SQLITE3. The form was working but from nothing stop work. Some can help me understand that problem?! I'm using QT5.8.0. on Ubuntu 16.04.

    #include <QtWidgets>
    #include <QtSql>
    #include "zoneForm.h"
    #include <QDebug>

    ZoneForm::ZoneForm(double mlimit, int reservid, int zoneid, QWidget *parent)
    : QDialog(parent)
    {
    repositoryComboBox = new QComboBox;
    repositoryLabel = new QLabel(tr("Reservatorio:"));
    repositoryLabel->setBuddy(repositoryComboBox);

    minLimitEdit = new QLineEdit;
    minLimitEdit->setValidator(new QIntValidator(0.0, 99999.0, this));
    minLimitEdit->setText(QString::number(mlimit));
    minLimitEdit->setDisabled(true);
    minLimitLabel = new QLabel(tr("Limite Inferior(hm³):"));
    minLimitLabel->setBuddy(minLimitEdit);
    
    maxLimitEdit = new QLineEdit;
    maxLimitEdit->setValidator(new QIntValidator(0.0, 99999.0, this));
    maxLimitLabel = new QLabel(tr("Limite Superior(hm³):"));
    maxLimitLabel->setBuddy(maxLimitEdit);
    
    priorityEdit = new QLineEdit;
    priorityEdit->setValidator(new QIntValidator(0, 99999, this));
    priorityLabel = new QLabel(tr("Prioridade:"));
    priorityLabel->setBuddy(priorityEdit);
    
    addButton = new QPushButton(tr("&Incluir"));
    deleteButton = new QPushButton(tr("&Apagar"));
    closeButton = new QPushButton(tr("&Fechar"));
    
    buttonBox = new QDialogButtonBox;
    buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
    buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
    buttonBox->addButton(closeButton, QDialogButtonBox::AcceptRole);
    
    tableModel = new QSqlRelationalTableModel(this);
    tableModel->setTable("zonas");
    tableModel->setRelation(Zone_ReservId,
                            QSqlRelation("reservatorios", "id", "id"));
    tableModel->setSort(Zone_Priority, Qt::AscendingOrder);
    tableModel->setFilter(QString("reservid = %1").arg(reservid));
    tableModel->select();
    
    QSqlTableModel *relationModel = tableModel->relationModel(Zone_ReservId);
    repositoryComboBox->setModel(relationModel);
    repositoryComboBox->setModelColumn(relationModel->fieldIndex("id"));
    repositoryComboBox->setDisabled(true);
    
    mapper = new QDataWidgetMapper(this);
    mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    mapper->setModel(tableModel);
    mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    mapper->addMapping(minLimitEdit, Zone_MinVol);
    mapper->addMapping(maxLimitEdit, Zone_MaxVol);
    mapper->addMapping(priorityEdit, Zone_Priority);
    mapper->addMapping(repositoryComboBox, Zone_ReservId);
    
    if (zoneid != -1) {
        for (int row = 0; row < tableModel->rowCount(); ++row) {
            QSqlRecord record = tableModel->record(row);
            if (record.value(Zone_Id).toInt() == zoneid) {
                mapper->setCurrentIndex(row);
                break;
            }
        }
    } else {
        mapper->toFirst();
    }
    
    connect(addButton, SIGNAL(clicked()), this, SLOT(addZone()));
    connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteZone()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));
    
    QHBoxLayout *topButtonLayout = new QHBoxLayout;
    topButtonLayout->setContentsMargins(20, 0, 20, 5);
    topButtonLayout->addStretch();
    
    QGridLayout *mainLayout = new QGridLayout;
    
    mainLayout->addWidget(repositoryLabel, 1, 0);
    mainLayout->addWidget(repositoryComboBox, 1, 1, 1, 2);
    mainLayout->addWidget(minLimitLabel, 2, 0);
    mainLayout->addWidget(minLimitEdit, 2, 1, 1, 2);
    mainLayout->addWidget(maxLimitLabel, 3, 0);
    mainLayout->addWidget(maxLimitEdit, 3, 1);
    mainLayout->addWidget(priorityLabel, 4, 0);
    mainLayout->addWidget(priorityEdit, 4, 1, 1, 2);
    mainLayout->addWidget(buttonBox, 7, 0, 1, 3);
    mainLayout->setRowMinimumHeight(6, 10);
    mainLayout->setRowStretch(6, 1);
    mainLayout->setColumnStretch(2, 1);
    setLayout(mainLayout);
    
    maxLimitEdit->setFocus();
    
    setWindowTitle(tr("Incluir/Editar Zonas e Prioridades"));
    

    }

    void ZoneForm::done(int result)
    {
    mapper->submit();
    QDialog::done(result);
    }

    void ZoneForm::addZone()
    {
    QString maxlimit="";
    int row = mapper->currentIndex();
    if (row!=-1) {
    QSqlRecord record = tableModel->record(row);
    maxlimit = record.value(Zone_MaxVol).toString();
    }

    mapper->submit();
    tableModel->insertRow(row);
    mapper->setCurrentIndex(row);
    
    if (!maxlimit.isEmpty())
        minLimitEdit->setText(maxlimit);
    
    maxLimitEdit->clear();
    priorityEdit->clear();
    maxLimitEdit->setFocus();
    

    }

    void ZoneForm::deleteZone()
    {
    int row = mapper->currentIndex();
    tableModel->removeRow(row);
    mapper->submit();
    mapper->setCurrentIndex(qMin(row, tableModel->rowCount() - 1));
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.