salut, donc mon problème c'est que l'ajout marche bien mais la modification et la suppression génère une erreur QSqlQuery::value: not positioned on a valid record.
voici mon code de connexion:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "connexiondb.h"
 
ConnexionDB::ConnexionDB()
{
}
 
bool ConnexionDB::setupDB(QString dbname)
{
    //using sqlite driver
    ConnexionDB::db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbname);
 
    if(db.open())
    {
        // If it is not available create it.
        bool found = false;
        if(db.tables().size()==9)
            {
                found = true;
            }
        if(!found)
        {
            QSqlQuery query(db);
            query.exec("CREATE TABLE classe (Id integer NOT NULL PRIMARY KEY,NombreEleve integer NOT NULL,Nom varchar NOT NULL);");
            query.exec("CREATE TABLE matiere (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT, nom VARCHAR(32) NOT NULL);");
            query.exec("CREATE TABLE encadreur (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,Nom varchar NOT NULL,Prenom varchar NOT NULL);");
            query.exec("CREATE TABLE groupe (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,IdEncadreur integer REFERENCES encadreur (Id));");
            query.exec("CREATE TABLE etudiant (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,Nom varchar NOT NULL,Prenom varchar NOT NULL,NumPiece varchar,NumTel varchar,DateN date,IdGroupe integer REFERENCES groupe (Id),IdClasse integer REFERENCES classe (Id));");
            query.exec("CREATE TABLE projet (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,Nom varchar NOT NULL,DureeReal timestamp NOT NULL,DateDebut date NOT NULL,DateFin date NOT NULL,IdMatiere integer REFERENCES matiere (Id));");
            query.exec("CREATE TABLE avancement (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,Note double NOT NULL,DateDebut date NOT NULL,DateFin date NOT NULL,DateEnc date NOT NULL,Description text,IdProjet integer REFERENCES projet (Id));");
            query.exec("CREATE TABLE avancementetud (Presence boolean NOT NULL,Note double NOT NULL,Description text,IdEtudiant integer REFERENCES etudiant (Id),IdAvancement integer REFERENCES avancement (Id));");
            query.exec("CREATE TABLE document (Id integer NOT NULL PRIMARY KEY AUTOINCREMENT,Nom text NOT NULL,Taille double NOT NULL,IdAvancement integer REFERENCES avancement (Id));");
        }
    }
    else
        return false;
 
    return true;
}
 
 
mon code d'ajout modification et suppression :
 
 
#include "preference.h"
#include "ui_preference.h"
#include "connexiondb.h"
#include "newmatiere.h"
#include <QMessageBox>
 
Preference::Preference(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Preference)
{
    ui->setupUi(this);
    QSqlDatabase db = QSqlDatabase::database();
 
//***************************************Matiére*******************************************//
 
    modelMatiere = new QSqlTableModel(this, db);
    modelMatiere->setTable("matiere");
    modelMatiere->setEditStrategy(QSqlTableModel::OnFieldChange);
    modelMatiere->select();
    modelMatiere->removeColumn(0);
    ui->listMatiere->setModel(modelMatiere);
    connect(ui->bAjouterMatiere, SIGNAL(clicked()), this, SLOT(ajouterMatiere()));
    connect(ui->bModifierMatiere, SIGNAL(clicked()), this, SLOT(modifierMatiere()));
    connect(ui->bSupprimerMatiere, SIGNAL(clicked()), this, SLOT(supprimerMatiere()));
}
 
Preference::~Preference()
{
    delete ui;
}
 
 
//******************************************slot matiere*****************************************//
 
void Preference::ajouterMatiere(){
    NewMatiere dlg(this);
    dlg.resetNom();
   if(dlg.exec() == QDialog::Accepted && dlg.getNom().length() > 0 )
   {
       // create a record from the current model
       QSqlRecord rec = modelMatiere->record();
       rec.setValue("Nom",dlg.getNom());
       // insert a new record (-1)
      modelMatiere->insertRecord(-1,rec);
   }
}
void Preference::modifierMatiere(){
    // get the current index, if any
        QModelIndex sample = ui->listMatiere->currentIndex();
        if( sample.row() >= 0 )
        {
            // copy the current record
            QSqlRecord rec = modelMatiere->record(sample.row());
            NewMatiere dlg(this);
            dlg.setNom(rec.value("nom").toString());
            if(dlg.exec() == QDialog::Accepted)
            {
                rec.setValue("nom",dlg.getNom());
                // save modified data
                modelMatiere->setRecord(sample.row(),rec);
            }
        }
}
void Preference::supprimerMatiere(){
    // get the current index, if any
    modelMatiere->submitAll();
      QModelIndex sample = ui->listMatiere->currentIndex();
      if( sample.row() >= 0 )
      {
          QMessageBox::StandardButton dlg;
          dlg = QMessageBox::question(this, tr("Suppression"),
                                      QString(tr("Supprimer Matière ?")),
                                      QMessageBox::Yes | QMessageBox::No);
 
          if(dlg == QMessageBox::Yes)
          {
              // remove the current index
              modelMatiere->removeRow(sample.row());
              modelMatiere->submitAll();
          }
      }
}
 
et pour la création de la base :
 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString filename = QDir::homePath() + QDir::separator() + "miniprojet.db";
    ConnexionDB *c = new ConnexionDB();
    bool ok = c->setupDB(filename);
......................
}
je suis débutant et c'est ma première alors si quelqu’un peut m'aider ou me donner un lien vers un exemple qui utilise une classe à part pour la connexion sur la base en utilisant les modelés et merci d'avance.