SQL query not loading back



  • In my qt c++ application when I click button named "Load" an sql query is executed and the result is displayed in a QTableview. There is a Q line edit to enter a word and when search button is clicked the word in the tableview is clicked and displayed in the table view itself. But when I click the load button again the previous results do not appear again and only the searched result still appears in the table view!.(Qtableview is blank!)

    Following is the code for the load button

    void Dialog::on_backMessages_clicked()
    {
        {
            QSqlQueryModel *modal = new QSqlQueryModel();
            if(QSqlDatabase::contains("MyConnection")){
    
                QSqlQuery* qry=new QSqlQuery(QSqlDatabase::database("MyConnection"));
           
                qry->prepare("Select UserName from User");
              
    
                qry->exec();
                modal->setQuery(*qry);
              ui->tableView->setModel(modal);
              ui->tableView->resizeColumnsToContents();
            }
              QSqlDatabase::database("myconnection").close();
        }
    }
    

    following is the code search button!

    void Dialog::on_searchMessages_clicked()
    {
        int row  = ui->tableView->model()->rowCount();
        int col =  ui->tableView->model()->columnCount();
        QAbstractItemModel *model = ui->tableView->model();
        QString searchLine=ui->messageName->text();
    
    
                for( int i = 0; i < row; ++i )
                {
                    bool match = false;
                    for( int j = 0; j < col; ++j )
                    {QModelIndex index = model->index(i, 0);
    
    
                        if(index.data().toString().contains(searchLine)){
                        searchMessages<<index.data().toString();
                        match=true;
                        break;
    
                        }
    
    
                }
                     ui->tableView->setRowHidden(i,!match);
            }
    
    }
    


    1. The view doesn't own the model so, since you are giving no parent to QSqlQueryModel *modal = new QSqlQueryModel(); you are leaking memory
    2. Same leak in QSqlQuery* qry=new QSqlQuery but this time the solution is just to leave it on the stack and not create it with new
    3. You do not need to create a new model every time. declare QSqlQueryModel *modal as a private member of Dialog, create it in the constructor and just call setQuery in the slot
    4. You are doing the filtering wrong. Use QSortFilterProxyModel between modal and ui->tableView and call setFilterWildcard("*" + searchLine + "*"); and then call setFilterFixedString(QString()) to return to showing everything


  • @VRonin Thanx but what do u mean by calling on the slot?



  • @Lasith Dialog::on_searchMessages_clicked()



  • @VRonin Thanx but it is difficult for me to figure it out! Can You show it using my code? :(



  • can you post dialog.h and dialog.cpp?



  • @VRonin
    Dialog.h

    ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include "QtSql"
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
    
        ~Dialog();
    
    private slots: 
        void on_searchMessages_clicked();
        void on_pushButton_clicked();  
    
    private:
        Ui::Dialog *ui;
    };
    
    #endif // DIALOG_H
    

    Dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include "QtSql"
    #include "mainwindow.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
        
    {
        ui->setupUi(this);
    }
    Dialog::~Dialog()
    {
        delete ui;
    }
    void Dialog::on_pushButton_clicked()
    {
        {
            QSqlQueryModel *modal = new QSqlQueryModel();
            if(QSqlDatabase::contains("MyConnection")){
    
                QSqlQuery* qry=new QSqlQuery(QSqlDatabase::database("MyConnection"));
           
                qry->prepare("Select UserName from User");
              
    
                qry->exec();
                modal->setQuery(*qry);
              ui->tableView->setModel(modal);
              ui->tableView->resizeColumnsToContents();
            }
              QSqlDatabase::database("myconnection").close();
        }
    }
    void Dialog::on_searchMessages_clicked()
    {
        int row  = ui->tableView->model()->rowCount();
        int col =  ui->tableView->model()->columnCount();
        QAbstractItemModel *model = ui->tableView->model();
        QString searchLine=ui->messageName->text();
    
    
                for( int i = 0; i < row; ++i )
                {
                    bool match = false;
                    for( int j = 0; j < col; ++j )
                    {QModelIndex index = model->index(i, 0);
    
    
                        if(index.data().toString().contains(searchLine)){
                        searchMessages<<index.data().toString();
                        match=true;
                        break;
    
                        }
    
    
                }
                     ui->tableView->setRowHidden(i,!match);
            }
    
    }
    


  • Dialog.h

    ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    class QSqlQueryModel;
    class QSortFilterProxyModel;
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
    
        ~Dialog();
    
    private slots: 
        void on_searchMessages_clicked();
        void on_pushButton_clicked();  
    
    private:
        Ui::Dialog *ui;
    QSqlQueryModel *modal;
    QSortFilterProxyModel* searchProxy;
    };
    
    #endif // DIALOG_H
    

    Dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QSqlQuery>
    #include <QSortFilterProxyModel>
    #include <QSqlQueryModel>
    #include "mainwindow.h"
    #include <QSqlDatabase>
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    ,modal(new QSqlQueryModel(this))
    ,searchProxy(new QSortFilterProxyModel(this))
        
    {
        ui->setupUi(this);
    searchProxy->setSourceModel(modal);
    ui->tableView->setModel(searchProxy);
    }
    Dialog::~Dialog()
    {
        delete ui;
    }
    void Dialog::on_pushButton_clicked()
    {
        {
            if(QSqlDatabase::contains("MyConnection")){
    
                QSqlQuery qry(QSqlDatabase::database("MyConnection"));
           
                qry.prepare("Select UserName from User");
              
    
                if(!qry.exec()) return;
                modal->setQuery(qry);
              ui->tableView->resizeColumnsToContents();
            }
              QSqlDatabase::database("myconnection").close();
        }
    }
    void Dialog::on_searchMessages_clicked()
    {
    searchProxy->setFilterWildcard("*" + ui->messageName->text()+ "*");
    }
    


  • @VRonin Thanx alot mate



  • @VRonin Hi, though I entered a word and searched the previous list remained unchanged without giving the search result



  • @VRonin I got it corrected!
    searchProxy->setSourceModel(modal);
    ui->tableView->setModel(searchProxy);

    should be put inside the search button method!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.