Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

show table



  • hello , when i execute my table is not shown unless i click on "trier" (sort) or any other button, what's the problem ? how do i fix this please?[link text]![alt text]4cd0c257-e825-4b15-bc1c-e078465d5e94-image.png
    98680b8b-0dfa-4413-901c-447c3394a15b-image.png
    here's my code that's supposed to show the data, the table is called 'tabpromotion'
    e9699d29-5b83-4d5d-8cfa-cce46d0bc5a5-image.png
    ddf83b3c-338a-4293-b5b2-c5e7a8c106d0-image.png
    please help!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please past your code as text and not screens of computer. This makes it hard to read and people can't even test it without completely rewriting it.

    As for issue, you don't show any code that actually makes use of the models you are using.

    But if you take a look at the output of your application you'll see that at some point you are trying to access your database before opening a connection to it.



  • mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "promotion.h"
    #include "joursferies.h"
    #include <QMessageBox>
    #include <QApplication>
    #include <QtWidgets/QMainWindow>
     #include <QtCharts/QChartView>
    #include <QtCharts/QBarSeries>
    #include <QtCharts/QBarSet>
    #include <QtCharts/QLegend>
    #include <QtCharts/QBarCategoryAxis>
    #include <QtCharts/QHorizontalStackedBarSeries>
    #include <QtCharts/QLineSeries>
    #include <QtCharts/QCategoryAxis>
    #include <QtCharts/QPieSeries>
    #include <QtCharts/QPieSlice>
    #include "qcustomplot.h"
    #include <QPdfWriter>
    #include <QPainter>
    #include <QMessageBox>
    #include <QDesktopServices>
    #include <QUrl>
    #include "secdialog.h"
    
    #include <QRegExpValidator>
    #define CARACTERES_ETRANGERS "~{}[]()|-`'^ç@_]\"°01234567890+=£$*µ/§!?,.&#;><"
    #include<QIntValidator>
    
    /*#include "widget.h"*/
    
    #include "tableprinter.h"
    #include <QPrinter>
    #include <QPrintPreviewDialog>
    #include <QPainter>
    #include <QDebug>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QSqlError>
    #include <QSqlTableModel>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->comboBox_id->setModel(tmppromotion.afficher_id());
    
        ui->tabpromotion->setModel(tmppromotion.afficher());
        ui->tabjoursferies->setModel(tmpjoursferies.afficher2());
      QSqlTableModel *model= new QSqlTableModel(ui->tabpromotion);
    
    
    
    ui->combobox->addItem("5%");
    ui->combobox->addItem("10%");
    ui->combobox->addItem("15%");
    ui->combobox->addItem("20%");
    ui->combobox->addItem("25%");
    ui->combobox->addItem("30%");
    ui->combobox->addItem("35%");
    ui->combobox->addItem("40%");
    
        model = new QSqlTableModel;
        model->select();
    
    //controle de saisie
            QIntValidator *roll=new QIntValidator(1,999999);
             QIntValidator *roll2=new QIntValidator(1,31);
             QIntValidator *roll3=new QIntValidator(1,12);
            ui->lineEdit_jour->setValidator(roll2);
            ui->lineEdit_mois->setValidator(roll3);
            ui->lineEdit_nombre_colis->setValidator(roll);
    
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;delete model;
    }
    
    
    void MainWindow::on_pb_supprimer_clicked()
    {
    int id = ui->comboBox_id->currentText().toInt();
    bool test=tmppromotion.supprimer(id);
    if(test)
    {ui->tabpromotion->setModel(tmppromotion.afficher());//refresh
        QMessageBox::information(nullptr, QObject::tr("Supprimer une promotion"),
                    QObject::tr("promotion supprimé.\n"
                                "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
    else
        QMessageBox::critical(nullptr, QObject::tr("Supprimer promotion"),
                    QObject::tr("Erreur !.\n"
                                "Click Cancel to exit."), QMessageBox::Cancel);
    
    
    }
    
    void MainWindow::on_pb_supprimer2_clicked()
    {
    int jour = ui->lineEdit_jour->text().toInt();
    bool test=tmpjoursferies.supprimer2(jour);
    if(test)
    {ui->tabjoursferies->setModel(tmpjoursferies.afficher2());//refresh
        QMessageBox::information(nullptr, QObject::tr("Supprimer un jour ferie"),
                    QObject::tr("jour ferie supprimé.\n"
                                "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
    else
        QMessageBox::critical(nullptr, QObject::tr("Supprimer jour ferie"),
                    QObject::tr("Erreur !.\n"
                                "Click Cancel to exit."), QMessageBox::Cancel);
    
    
    }
    void MainWindow::on_pb_modifier_clicked()
    {
        int id =  ui->comboBox_id->currentText().toInt();
          QString nom_client= ui->lineEdit_nom_client->text();
          QString pourcentage= ui->combobox->currentText();
          int nombre_colis = ui->lineEdit_nombre_colis->text().toInt();
         promotion p;
         bool test=p.modifier(id,nom_client,pourcentage,nombre_colis);
       if(test)
         {
            ui->tabpromotion->setModel(tmppromotion.afficher());//refresh
            QMessageBox::information(nullptr, QObject::tr("Modifier une promotion !"),
                              QObject::tr(" promotion modifiée ! \n"
                                          "Click Cancel to exit."), QMessageBox::Cancel);
         }
    
         else {
    
             QMessageBox::critical(nullptr, QObject::tr("Modifier une promotion"),
                         QObject::tr("Erreur !.\n"
                                     "Click Cancel to exit."), QMessageBox::Cancel);
         }
    }
    
    void MainWindow::on_pb_modifier2_clicked()
    {
          QString nom_jour= ui->lineEdit_nom_jour->text();
          int jour = ui->lineEdit_jour->text().toInt();
          QString mois= ui->lineEdit_mois->text();
    
         joursferies j;
         bool test=j.modifier2(nom_jour,jour,mois);
       if(test)
         {
            ui->tabjoursferies->setModel(tmpjoursferies.afficher2());//refresh
            QMessageBox::information(nullptr, QObject::tr("Modifier un jour ferie !"),
                              QObject::tr(" jour ferie modifiée ! \n"
                                          "Click Cancel to exit."), QMessageBox::Cancel);
         }
    
         else {
    
             QMessageBox::critical(nullptr, QObject::tr("Modifier jour ferie"),
                         QObject::tr("Erreur !.\n"
                                     "Click Cancel to exit."), QMessageBox::Cancel);
         }
    }
    
    void MainWindow::on_pb_ajouter_clicked()
    {
        int id =  ui->comboBox_id->currentText().toInt();
        QString nom_client= ui->lineEdit_nom_client->text();
        QString pourcentage= ui->combobox->currentText();
        int nombre_colis = ui->lineEdit_nombre_colis->text().toInt();
      promotion p(id,nom_client,pourcentage,nombre_colis);
      bool test=p.ajouter();
      if(test)
    {
    
          ui->tabpromotion->setModel(tmppromotion.afficher());//refresh
    QMessageBox::information(nullptr, QObject::tr("Ajouter une promotion"),
                      QObject::tr("promotion ajouté.\n"
                                  "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
      else
          QMessageBox::critical(nullptr, QObject::tr("Ajouter une promotion"),
                      QObject::tr("Erreur !.\n"
                                  "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
    
    void MainWindow::on_pb_ajouter2_clicked()
    {
        QString nom_jour= ui->lineEdit_nom_jour->text();
        int jour = ui->lineEdit_jour->text().toInt();
        QString mois= ui->lineEdit_mois->text();
    
      joursferies j(nom_jour,jour,mois);
      bool test=j.ajouter2();
      if(test)
    {
    
          ui->tabjoursferies->setModel(tmpjoursferies.afficher2());//refresh
    QMessageBox::information(nullptr, QObject::tr("Ajouter un jour ferie"),
                      QObject::tr("jour ferie ajouté.\n"
                                  "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
      else
          QMessageBox::critical(nullptr, QObject::tr("Ajouter un jour ferie"),
                      QObject::tr("Erreur !.\n"
                                  "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
    
    void MainWindow::on_pb_rechercher_clicked()
    {
        {
            int id =  ui->comboBox_id->currentText().toInt();
           ui->tabpromotion->setModel(tmppromotion.rechercher(id));
    
        }
    }
    
    void MainWindow::on_pb_rechercher2_clicked()
    {
        {
            int jour = ui->lineEdit_jour->text().toInt();
           ui->tabjoursferies->setModel(tmpjoursferies.rechercher2(jour));
    
        }
    }
    
    void MainWindow::on_pb_trier_clicked()
    {
        bool test=true;
                if(test){
    
                       { ui->tabpromotion->setModel(tmppromotion.trier());
                        QMessageBox::information(nullptr, QObject::tr("trier promotion"),
                                    QObject::tr("promotion trier.\n"
                                                "Voulez-vous enregistrer les modifications ?"),
                                           QMessageBox::Save
                                           | QMessageBox::Cancel,
                                          QMessageBox::Save);
        }
    
                    }
                    else
                        QMessageBox::critical(nullptr, QObject::tr("trier promotion"),
                                    QObject::tr("Erreur !.\n"
                                                "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
    
    void MainWindow::on_pb_trier2_clicked()
    {
        bool test=true;
                if(test){
    
                       { ui->tabjoursferies->setModel(tmpjoursferies.trier2());
                        QMessageBox::information(nullptr, QObject::tr("trier jour ferie"),
                                    QObject::tr("jours feries trier.\n"
                                                "Voulez-vous enregistrer les modifications ?"),
                                           QMessageBox::Save
                                           | QMessageBox::Cancel,
                                          QMessageBox::Save);
        }
    
                    }
                    else
                        QMessageBox::critical(nullptr, QObject::tr("trier jours feries"),
                                    QObject::tr("Erreur !.\n"
                                                "Click Cancel to exit."), QMessageBox::Cancel);
    
    }
    
    /* void MainWindow::on_pb_statistique_clicked() */
    
    
    
    
    //controle de saisie
    void MainWindow::on_lineEdit_nom_client_textEdited(const QString &arg1)
    {
            QString texte=arg1;
        QString caracteresEtrangers(CARACTERES_ETRANGERS);
        for (int i=0; i<texte.size(); i++)
        foreach (const QChar &y, caracteresEtrangers)
            if(texte.at(i)==y)
                texte[i]=' ';
    
        ui->lineEdit_nom_client->setText(texte);
    }
    
    void MainWindow::on_lineEdit_nom_jour_textEdited(const QString &arg1)
    {
            QString texte=arg1;
        QString caracteresEtrangers(CARACTERES_ETRANGERS);
        for (int i=0; i<texte.size(); i++)
        foreach (const QChar &y, caracteresEtrangers)
            if(texte.at(i)==y)
                texte[i]=' ';
        ui->lineEdit_nom_jour->setText(texte);
    }
    
    
    
    // ----------------- example of inharitance from PagePrepare ---------------------
    
    class PrintBorder : public PagePrepare
    {
    public:
        virtual void preparePage(QPainter *painter);
        static int pageNumber;
    };
    
    int PrintBorder::pageNumber = 0;
    
    void PrintBorder::preparePage(QPainter *painter)
    { // print a border on each page
        QRect rec = painter->viewport();
        painter->setPen(QPen(QColor(0, 0, 0), 1));
        painter->drawRect(rec);
        painter->translate(10, painter->viewport().height() - 10);
        painter->drawText(0, 0, QString("Page %1").arg(pageNumber));
        pageNumber += 1;
    }
    
    // --------------------------------------------------------------------------------
    
    
    
    
    void MainWindow::print(QPrinter *printer) {
    
        // ------------------ simplest example --------------------------
    
        QPainter painter;
        if(!painter.begin(printer)) {
            qWarning() << "can't start printer";
            return;
        }
        // print table
        TablePrinter tablePrinter(&painter, printer);
        QVector<int> columnStretch = QVector<int>() << 2 << 5 << 10 << 15;
        if(!tablePrinter.printTable(ui->tabpromotion->model(), columnStretch)) {
            qDebug() << tablePrinter.lastError();
        }
        painter.end();
    }
    
    void MainWindow::print2(QPrinter *printer) {
    
        // ------------------ simplest example --------------------------
    
        QPainter painter;
        if(!painter.begin(printer)) {
            qWarning() << "can't start printer";
            return;
        }
        // print table
        TablePrinter tablePrinter(&painter, printer);
        QVector<int> columnStretch = QVector<int>() << 2 << 5 << 10;
        if(!tablePrinter.printTable(ui->tabjoursferies->model(), columnStretch)) {
            qDebug() << tablePrinter.lastError();
        }
        painter.end();
    }
    
    void MainWindow::print_two_tables(QPrinter *printer) {
    
        // ------------------ two tables example --------------------------
    
        QPainter painter;
        if(!painter.begin(printer)) {
            qWarning() << "can't start printer";
            return;
        }
        // print table
        TablePrinter tablePrinter(&painter, printer);
        QVector<int> columnStretch = QVector<int>() << 2 << 5 << 10 << 15;
        if(!tablePrinter.printTable(ui->tabpromotion->model(), columnStretch)) {
            qDebug() << tablePrinter.lastError();
        }
        // print second table
        painter.translate(0, 100);
        if(!tablePrinter.printTable(ui->tabpromotion->model(), columnStretch)) {
            qDebug() << tablePrinter.lastError();
        }
        painter.end();
    }
    
    void MainWindow::print_two_tables2(QPrinter *printer) {
    
        // ------------------ two tables example --------------------------
    
        QPainter painter;
        if(!painter.begin(printer)) {
            qWarning() << "can't start printer";
            return;
        }
        // print table
        TablePrinter tablePrinter(&painter, printer);
        QVector<int> columnStretch = QVector<int>() << 2 << 5 << 10;
        if(!tablePrinter.printTable(ui->tabjoursferies->model(), columnStretch)) {
            qDebug() << tablePrinter.lastError();
        }
        // print second table
        painter.translate(0, 100);
        if(!tablePrinter.printTable(ui->tabjoursferies->model(), columnStretch)) {
            qDebug() << tablePrinter.lastError();
        }
        painter.end();
    }
    
    
    void MainWindow::uglyPrint(QPrinter *printer) {
    
        // ---------------- death-to-designers example ------------------
    
        QPainter uglyPainter;
        if(!uglyPainter.begin(printer)) {
            qWarning() << "can't start printer";
            return;
        }
        TablePrinter uglyTablePrinter(&uglyPainter, printer);
        QVector<int> colStretch = QVector<int>() << 5 << 5 << 0 << 10;
        uglyTablePrinter.setPen(QPen(QColor(0, 100, 255), 3, Qt::DotLine)); // pen for borders
        uglyTablePrinter.setHeaderColor(Qt::red);
        uglyTablePrinter.setContentColor(Qt::green);
        QFont font1; // font for headers
        font1.setBold(true);
        QFont font2; // font for content
        font2.setItalic(true);
        uglyTablePrinter.setHeadersFont(font1);
        uglyTablePrinter.setContentFont(font2);
        PrintBorder *printB = new PrintBorder;
        printB->pageNumber = 1;
        uglyTablePrinter.setPagePrepare(printB);
        QVector<QString> headers = QVector<QString>() << "ID" << "NOM_CLIENT" << "POURCENTAGE" << "NOMBRE_COLIS";
        uglyPainter.setPen(QPen(Qt::yellow));
        uglyPainter.drawText(uglyPainter.viewport().width()/2 - 40, 40, "TABLE PROMOTION");
        uglyPainter.translate(0, 60); // start print point
        uglyTablePrinter.setCellMargin(10, 5, 5, 5);
        uglyTablePrinter.setPageMargin(100, 40, 40, 40);
        if(!uglyTablePrinter.printTable(ui->tabpromotion->model(), colStretch, headers)) {
            qDebug() << uglyTablePrinter.lastError();
        }
        uglyPainter.end();
        delete printB;
    }
    
    void MainWindow::uglyPrint2(QPrinter *printer) {
    
        // ---------------- death-to-designers example ------------------
    
        QPainter uglyPainter;
        if(!uglyPainter.begin(printer)) {
            qWarning() << "can't start printer";
            return;
        }
        TablePrinter uglyTablePrinter(&uglyPainter, printer);
        QVector<int> colStretch = QVector<int>() << 5 << 5 << 0 ;
        uglyTablePrinter.setPen(QPen(QColor(0, 100, 255), 3, Qt::DotLine)); // pen for borders
        uglyTablePrinter.setHeaderColor(Qt::red);
        uglyTablePrinter.setContentColor(Qt::green);
        QFont font1; // font for headers
        font1.setBold(true);
        QFont font2; // font for content
        font2.setItalic(true);
        uglyTablePrinter.setHeadersFont(font1);
        uglyTablePrinter.setContentFont(font2);
        PrintBorder *printB = new PrintBorder;
        printB->pageNumber = 1;
        uglyTablePrinter.setPagePrepare(printB);
        QVector<QString> headers = QVector<QString>() << "NOM_JOUR" << "JOUR" << "MOIS";
        uglyPainter.setPen(QPen(Qt::yellow));
        uglyPainter.drawText(uglyPainter.viewport().width()/2 - 40, 40, "TABLE JOURS FERIES");
        uglyPainter.translate(0, 60); // start print point
        uglyTablePrinter.setCellMargin(10, 5, 5, 5);
        uglyTablePrinter.setPageMargin(100, 40, 40, 40);
        if(!uglyTablePrinter.printTable(ui->tabjoursferies->model(), colStretch, headers)) {
            qDebug() << uglyTablePrinter.lastError();
        }
        uglyPainter.end();
        delete printB;
    }
    
    void MainWindow::on_pushButton_5_clicked() {
        QPrintPreviewDialog dialog;
        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(uglyPrint(QPrinter*)));
        dialog.exec();
    }
    
    void MainWindow::on_pushButton_7_clicked() {
        QPrintPreviewDialog dialog;
        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(uglyPrint2(QPrinter*)));
        dialog.exec();
    }
    
    void MainWindow::on_pushButton_4_clicked() {
        QPrintPreviewDialog dialog;
        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
        dialog.exec();
    }
    
    void MainWindow::on_pushButton_8_clicked() {
        QPrintPreviewDialog dialog;
        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print2(QPrinter*)));
        dialog.exec();
    }
    
    void MainWindow::on_pushButton_6_clicked() {
        QPrintPreviewDialog dialog;
        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print_two_tables(QPrinter*)));
        dialog.exec();
    }
    
    void MainWindow::on_pushButton_9_clicked() {
        QPrintPreviewDialog dialog;
        connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print_two_tables2(QPrinter*)));
        dialog.exec();
    }
    
    void MainWindow::on_pushButton_clicked()
    {
      //  SecDialog secDeialog;
       // secDeialog.setModal(true);
      //  secDeialog.exec();
    
        secDialog = new SecDialog(this);
        secDialog ->show();
    
    }
    

    mainwindow.h:

    #include "joursferies.h"
    #include <QMainWindow>
    #include <QWidget>
    #include "secdialog.h"
    
    class QPrinter;
    class QSqlTableModel;
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pb_ajouter_clicked();
        void on_pb_supprimer_clicked();
        void on_pb_modifier_clicked();
        void on_pb_rechercher_clicked();
        void on_pb_trier_clicked();
        void on_lineEdit_nom_client_textEdited(const QString &arg1);
        void print(QPrinter *printer);
        void print_two_tables(QPrinter *printer);
        void uglyPrint(QPrinter *printer);
        void on_pushButton_5_clicked();
        void on_pushButton_4_clicked();
        void on_pushButton_6_clicked();
    
    
        void on_pb_ajouter2_clicked();
        void on_pb_supprimer2_clicked();
        void on_pb_modifier2_clicked();
        void on_pb_rechercher2_clicked();
        void on_pb_trier2_clicked();
        void on_lineEdit_nom_jour_textEdited(const QString &arg1);
        void print2(QPrinter *printer);
        void print_two_tables2(QPrinter *printer);
        void uglyPrint2(QPrinter *printer);
        void on_pushButton_7_clicked();
        void on_pushButton_8_clicked();
        void on_pushButton_9_clicked();
    
        void on_pushButton_clicked();
    
    
    private:
        Ui::MainWindow *ui;
        SecDialog *secDialog;
        promotion tmppromotion;
        joursferies tmpjoursferies;
        QSqlTableModel *model;
    };
    #endif // MAINWINDOW_H
    

    promotion.cpp:

    #include "promotion.h"
    #include <QString>
    #include <QSqlQuery>
    #include<QSqlQueryModel>
    
    
    promotion::promotion()
    {
        id=0;
        nom_client="";
        pourcentage="";
        nombre_colis=0;
    }
    
    promotion::promotion(int id, QString nom_client, QString pourcentage,int nombre_colis)
    {
        this ->id=id;
        this->nom_client=nom_client;
        this->pourcentage=pourcentage;
        this ->nombre_colis=nombre_colis;
    }
    
    QString promotion::get_nom_client() {return nom_client;}
    QString promotion::get_pourcentage() {return pourcentage;}
    int promotion::get_id() {return id;}
    int promotion::get_nombre_colis() {return nombre_colis;}
    
    bool promotion::ajouter()
    {
        QSqlQuery query;
        QString res=QString::number(id);
        query.prepare("INSERT INTO promotion (id,nom_client,pourcentage,nombre_colis)"
                      "VALUES (:id, :nom_client, :pourcentage, :nombre_colis)");
        query.bindValue(":id",res);
        query.bindValue(":nom_client",nom_client);
        query.bindValue(":pourcentage",pourcentage);
        query.bindValue(":nombre_colis", nombre_colis);
        return query.exec();
    }
    
    QSqlQueryModel * promotion::afficher()
    {QSqlQueryModel * model= new QSqlQueryModel();
    model->setQuery("select * from promotion");
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("nom_client"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("pourcentage"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("nombre_colis"));
        return model;
    }
    
    bool promotion::supprimer(int idd)
    {
    QSqlQuery query;
    QString res= QString::number(idd);
    query.prepare("Delete from promotion where ID = :id ");
    query.bindValue(":id", res);
    return    query.exec();
    }
    
    bool promotion::modifier(int idd,QString nom_client,QString pourcentage,int nombre_colis)
    { QSqlQuery query;
        QString res= QString::number(idd);
        query.prepare("UPDATE promotion SET NOM_CLIENT=:nom_client,POURCENTAGE=:pourcentage ,NOMBRE_COLIS=:nombre_colis where ID=:id");
        query.bindValue(":id",res);
        query.bindValue(":nom_client",nom_client);
        query.bindValue(":pourcentage",pourcentage);
         query.bindValue(":nombre_colis",nombre_colis);
        return query.exec();
    
    }
    
    QSqlQueryModel *  promotion::rechercher(int id)
    {
        QSqlQueryModel * model=new QSqlQueryModel();
        QString res=QString::number(id);
        model->setQuery("select * from promotion where (id LIKE '"+res+"%' ) ");
    
    
                return  model;
    
    
    }
    QSqlQueryModel * promotion::trier()
    {
        QSqlQueryModel * model= new QSqlQueryModel();
        model->setQuery("select * from promotion ORDER BY nom_client");
        model->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
        model->setHeaderData(1, Qt::Horizontal, QObject::tr("nom_client "));
        model->setHeaderData(2, Qt::Horizontal, QObject::tr("pourcentage"));
            return model;
    }
    
    QSqlQueryModel * promotion::afficher_id()
    {
        QSqlQueryModel * model= new QSqlQueryModel();
    model->setQuery("select id from promotion");
    return model;
    }
    

    promotion.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include "promotion.h"
    #include "joursferies.h"
    #include <QMainWindow>
    #include <QWidget>
    #include "secdialog.h"
    
    class QPrinter;
    class QSqlTableModel;
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pb_ajouter_clicked();
        void on_pb_supprimer_clicked();
        void on_pb_modifier_clicked();
        void on_pb_rechercher_clicked();
        void on_pb_trier_clicked();
        void on_lineEdit_nom_client_textEdited(const QString &arg1);
        void print(QPrinter *printer);
        void print_two_tables(QPrinter *printer);
        void uglyPrint(QPrinter *printer);
        void on_pushButton_5_clicked();
        void on_pushButton_4_clicked();
        void on_pushButton_6_clicked();
    
    
        void on_pb_ajouter2_clicked();
        void on_pb_supprimer2_clicked();
        void on_pb_modifier2_clicked();
        void on_pb_rechercher2_clicked();
        void on_pb_trier2_clicked();
        void on_lineEdit_nom_jour_textEdited(const QString &arg1);
        void print2(QPrinter *printer);
        void print_two_tables2(QPrinter *printer);
        void uglyPrint2(QPrinter *printer);
        void on_pushButton_7_clicked();
        void on_pushButton_8_clicked();
        void on_pushButton_9_clicked();
    
        void on_pushButton_clicked();
    
    
    private:
        Ui::MainWindow *ui;
        SecDialog *secDialog;
        promotion tmppromotion;
        joursferies tmpjoursferies;
        QSqlTableModel *model;
    };
    #endif // MAINWINDOW_H
    

    connection.cpp:

    #include "connection.h"
    
    Connection::Connection()
    {
    
    }
    
    bool Connection::ouvrirConnection()
    {bool test=false;
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("projet_2a");
    db.setUserName("kenza");//inserer nom de l'utilisateur
    db.setPassword("181JFT0998");//inserer mot de passe de cet utilisateur
    
    if (db.open())
    test=true;
        return  test;
    }
    
    void Connection::fermerConnection()
    {db.close();}
    

    connection.h:

    #ifndef CONNECTION_H
    #define CONNECTION_H
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>
    
    class Connection
    {
    private:
        QSqlDatabase db;
    public:
        Connection();
        bool ouvrirConnection();
        void fermerConnection();
    };
    #endif // CONNECTION_H```
    

  • Lifetime Qt Champion

    Hi
    make sure that
    Connection::ouvrirConnection()is called before you try to

    ui->comboBox_id->setModel(tmppromotion.afficher_id());

    while this member is created first
    promotion tmppromotion;

    it does not look to me that it calls
    Connection::ouvrirConnection()
    in its constructor.

    So from the description, i think you call setQuery before database is open in the constructor
    of mainwindow.

    This is easy to test - put a break point in Connection::ouvrirConnection() and check its called before any other functions that needs an open db.


Log in to reply