-1

i tried to entre data from a form " means from QlineEdit as integer "

the programm is compiled successfully but when i open the form and entre data , the programm crach

her's the erreur

Object::connect: No such slot FenArticle::chercheParFamille() in ..\stockmanagement\fenarticle.cpp:113
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

hers my code *

    #include "fenajoutearticle.h"
#include <QIntValidator>
#include <QValidator>

FenAjouteArticle::FenAjouteArticle(QWidget *parent) :
    QWidget(parent)
{
    resize(500,450);
    setWindowModality(Qt::ApplicationModal);

    // init member widgets

     //*****************************Signals SLots ******************************//
     signalSlots();

     //***********************************Base de donnée Traitement***********************************//

     db = new QSqlDatabase;
     *db = QSqlDatabase::addDatabase("QSQLITE") ;
     db->setHostName("localhost");
     db->setDatabaseName("gestionstockDB.db");
     db->setPassword("");
     db->setUserName("");

     db->open();

     if(!db->open())
         QMessageBox::critical(this,"Erreur","Erreur connexion base de données") ;
     else{
         QSqlQuery *chercheParFamilleQuery = new QSqlQuery("select Famille from Produit");
         SelectFamille->clear();
         while(chercheParFamilleQuery->next())
             SelectFamille->addItem(chercheParFamilleQuery->value(0).toString());

         QSqlQuery *chercheParUniteQuery = new QSqlQuery("select Unite from Produit");
         selectUnit->clear();
         while(chercheParUniteQuery->next())
             selectUnit->addItem(chercheParUniteQuery->value(0).toString());
     }
     // basic layouting 
}

void FenAjouteArticle::signalSlots()
{
    connect(buttonOkUtilisateur,SIGNAL(clicked()),this,SLOT(ajoutDonne())) ;
    connect(buttonAnnulerUtilisateur,SIGNAL(clicked()),this,SLOT(close())) ;
}

//********************* Signal Slots coeurs ***********************************//

void FenAjouteArticle::ajoutDonne()
{
    if(db->open())
    {


    QSqlQuery *ajouterDonneeQuery = new QSqlQuery;

        ajouterDonneeQuery->prepare("insert into Produit (Reference,Designation,localisation,Famille,Qte_min,Qte_max,Qte_stock,Unite,Prix_achat,Prix_vente)   VALUES(:ref,:design,:local,:fam,:qtmin,:qtmax,:qtstock ,:unite,:pachat,:pvente)");
        //QString Stock = champStockInit->text();


        ajouterDonneeQuery->bindValue(":ref",champRef->text());  // QlineEdit
        ajouterDonneeQuery->bindValue(":design",champDesignation->text()); // QlineEdit
        ajouterDonneeQuery->bindValue(":local",champLocalisation->text()); // QlineEdit
        ajouterDonneeQuery->bindValue(":fam","fam"); // QcomboBox
        ajouterDonneeQuery->bindValue(":qtmin",200); // QlineEdit
        ajouterDonneeQuery->bindValue(":qtmax",20); // QlineEdit
        ajouterDonneeQuery->bindValue(":qtstock",20); // QlineEdit
        ajouterDonneeQuery->bindValue(":unite","unite"); // QlineEdit
        ajouterDonneeQuery->bindValue(":pachat",20);  // QlineEdit
        ajouterDonneeQuery->bindValue(":pvente",champPrixVente->text());  // QlineEdit

        ajouterDonneeQuery->exec();

}

}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126

1 Answers1

0

the DB message with the duplication is caused by (i guess) multiple instances of that class.

set up your database connection at a "global" spot, like main() or inside your main window class. (except you want and need multiple connections, but i dont think this is good when working with sqlite) when using multiple connections, you need to add them to the QSqlQuery constructor, otherwise it will use the default connection, mostly the first created one.

no slot existing named FenArticle::chercheParFamille() should be message enough. maybe some characters are incorrect or the parameters given differs from declaration.

to the value binding, sqlite should be cool with a string as value for an integer and will use it correct, IF the value is a valid integer. you could alternatively use:

QString intText = lineEdit->text();
bool test = false;
intText.toInt(&test);
if (test == true)
   // it is a valid integer, use it
else
   // message or whatever
Zaiborg
  • 2,492
  • 19
  • 28