-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();

}

}
4

1 回答 1

0

带有重复的 DB 消息是由(我猜)该类的多个实例引起的。

main()在主窗口类之类的“全局”位置设置数据库连接。(除非您想要并且需要多个连接,但我认为在使用 sqlite 时这不是很好)当使用多个连接时,您需要将它们添加到QSqlQuery构造函数中,否则它将使用默认连接,主要是第一个创建的连接。

没有命名的插槽FenArticle::chercheParFamille()应该是足够的消息。可能某些字符不正确或给定的参数与声明不同。

对于值绑定,sqlite 应该很酷,将字符串作为整数的值,并且会正确使用它,如果值是有效整数。您也可以使用:

QString intText = lineEdit->text();
bool test = false;
intText.toInt(&test);
if (test == true)
   // it is a valid integer, use it
else
   // message or whatever
于 2013-08-15T09:52:25.270 回答