Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. QSqlRelationalTableModel submitAll() ma dati non aggiornati nel DB
Forum Update on Monday, May 27th 2025

QSqlRelationalTableModel submitAll() ma dati non aggiornati nel DB

Scheduled Pinned Locked Moved Unsolved Italian
3 Posts 2 Posters 347 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.
  • A Offline
    A Offline
    addebito
    wrote on last edited by addebito
    #1

    Ciao a tutti,

    database: PostgreeSQL
    os: Windows 10
    Qt: 5.14
    sono partito dall'esempio "relationaltablemodel" modificando la connessione per puntare al mio db che contiene solamente 2 tabelle

    tramite un modello QSqlRelationalTableModel riesco a visualizzare i dati in una tableView ma quando cerco di modificare un campo quest'ultimo non viene aggiornato anche se la submitAll() mi ritorna ok.

    Dialog::Dialog(QWidget *parent)
        : QDialog(parent)
        , ui(new Ui::Dialog)
    {
        ui->setupUi(this);  
        
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
        db.setHostName("127.0.0.1");
        db.setPort(5432);
        db.setDatabaseName("postgres");
        db.setUserName("postgres");
        db.setPassword("ciao");
        bool ok = db.open();
        if (!ok)
        {
            QMessageBox msgBox;
            msgBox.setText("Cannot open database. " + db.lastError().text());
            msgBox.exec();
        }
    
        qDebug() << db.databaseName();
        qDebug() << db.tables();
    
        model = new QSqlRelationalTableModel;
        initializeModel(model);
        ui->tableView->setModel(model);
        ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
        ui->tableView->resizeColumnsToContents();
    }
    
    void Dialog::initializeModel(QSqlRelationalTableModel *model)
    {
        model->setTable("tabella_1");
    
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->setRelation(1, QSqlRelation("tabella_2", "id", "nome"));
    
        bool ok_sel = model->select();
        if (!ok_sel)
        {
            QMessageBox msgBox;
            msgBox.setText("Cannot query database. " + m_model->lastError().text());
            msgBox.exec();
        }
        qDebug() << "total " << model->rowCount();
    }
    
    void Dialog::on_pushButton_clicked()
    {
        bool ok = false;
    
        ok = model->database().transaction();
        if (!ok)
        {
            QMessageBox msgBox;
            msgBox.setText("Transaction database. " + model->database().lastError().text());
            msgBox.exec();
        }
    
        ok = model->submitAll();
        if (ok)
        {
            model->database().commit();
        }
        else
        {
            QMessageBox msgBox;
            msgBox.setText("Model submitAll. " + model->lastError().text());
            msgBox.exec();
        }
    }
    

    Sembra funzionare tutto, ma modifico dei valori presenti in tabella, poi premo il bottone per aggiornare... mi riappaiono i vecchi dati senza alcun errore... ?!?!
    Cosa diavolo sbaglio?
    Avete qualche idea?

    Grazie mille!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      model->database().commit(); ritorna un booleano. poi controllare se quello e' true?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      A 1 Reply Last reply
      0
      • VRoninV VRonin

        model->database().commit(); ritorna un booleano. poi controllare se quello e' true?

        A Offline
        A Offline
        addebito
        wrote on last edited by
        #3

        Grazie per il suggerimento @VRonin, tuttavia ritorna true anche il commit().

        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