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. Searching word found after several clicks!
Forum Updated to NodeBB v4.3 + New Features

Searching word found after several clicks!

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatortableviewbuttonsearch
4 Posts 3 Posters 1.7k Views 1 Watching
  • 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
    #1

    In my Qt c++ application I have a search function where a word given in a line edit is searched from a table view when the search button is clicked! But the problem is the word which is searched appears on the table view only after the search button is clicked several times!

    following is my code!

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

    }
    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()+ "");
    searchProxy->setSourceModel(modal);
    ui->tableView->setModel(searchProxy);
    }
    }

    How do I correct my code such that the searched word appears from the 1st button click?

    jsulmJ 1 Reply Last reply
    0
    • L Lasith

      In my Qt c++ application I have a search function where a word given in a line edit is searched from a table view when the search button is clicked! But the problem is the word which is searched appears on the table view only after the search button is clicked several times!

      following is my code!

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

      }
      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()+ "");
      searchProxy->setSourceModel(modal);
      ui->tableView->setModel(searchProxy);
      }
      }

      How do I correct my code such that the searched word appears from the 1st button click?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Lasith Why do you close database connection in void Dialog::on_pushButton_clicked()?
      Where do you actually open it?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Lasith Why do you close database connection in void Dialog::on_pushButton_clicked()?
        Where do you actually open it?

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

        @jsulm Sorry It has been commented! :D By the way the specific word I searched is in Row number 1140 in my database table ! To search that I had to click the search button 4 times but when I searched another word which is in in Row number 850 I had to click the search button three times! Is there any relation between the row number and searching? :O

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          More likely that not all your database data are loaded in memory. You would have to call fetchMore however beware that if you have big tables it's going to cost you more in RAM.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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