Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQL query not loading back
QtWS25 Last Chance

SQL query not loading back

Scheduled Pinned Locked Moved Solved General and Desktop
qtableviewbuttonsearchresults
11 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by VRonin
    #1

    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 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      L 1 Reply Last reply
      2
      • VRoninV VRonin
        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
        L Offline
        L Offline
        Lasith
        wrote on last edited by Lasith
        #3

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

        VRoninV 1 Reply Last reply
        0
        • L Lasith

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

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Lasith Dialog::on_searchMessages_clicked()

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          L 1 Reply Last reply
          0
          • VRoninV VRonin

            @Lasith Dialog::on_searchMessages_clicked()

            L Offline
            L Offline
            Lasith
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              can you post dialog.h and dialog.cpp?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              L 1 Reply Last reply
              0
              • VRoninV VRonin

                can you post dialog.h and dialog.cpp?

                L Offline
                L Offline
                Lasith
                wrote on last edited by VRonin
                #7

                @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);
                        }
                
                }
                
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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()+ "*");
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  L 3 Replies Last reply
                  3
                  • VRoninV VRonin

                    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()+ "*");
                    }
                    
                    L Offline
                    L Offline
                    Lasith
                    wrote on last edited by
                    #9

                    @VRonin Thanx alot mate

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      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()+ "*");
                      }
                      
                      L Offline
                      L Offline
                      Lasith
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • VRoninV VRonin

                        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()+ "*");
                        }
                        
                        L Offline
                        L Offline
                        Lasith
                        wrote on last edited by
                        #11

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

                        should be put inside the search button method!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved