SQL query not loading back
Solved
General and Desktop
-
- The view doesn't own the model so, since you are giving no parent to
QSqlQueryModel *modal = new QSqlQueryModel();
you are leaking memory - Same leak in
QSqlQuery* qry=new QSqlQuery
but this time the solution is just to leave it on the stack and not create it withnew
- You do not need to create a new model every time. declare
QSqlQueryModel *modal
as a private member ofDialog
, create it in the constructor and just callsetQuery
in the slot - You are doing the filtering wrong. Use
QSortFilterProxyModel
betweenmodal
andui->tableView
and callsetFilterWildcard("*" + searchLine + "*");
and then callsetFilterFixedString(QString())
to return to showing everything
- The view doesn't own the model so, since you are giving no parent to
-
@VRonin
Dialog.hifndef 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()+ "*"); }