QSqlTableModel : SetQuery issue
-
Hello there, I have initialized my QSqlTableModel like this:
QSqlTableModel *model = new QSqlTableModel(this, db);
model->setTable("path_master_table");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();ui->tableView->setModel(model);
ui->tableView->sortByColumn(0, Qt::AscendingOrder);I can view my table very well.
Now i want the table to change when i run a query on it. So I did this.QSqlQuery query(query_text); (where query text is a sql command)
model->setQuery(query);I get the following error:
error: 'void QSqlTableModel::setQuery(const QSqlQuery&)' is protected
void setQuery(const QSqlQuery &query);
^error: within this context
model->setQuery(query);
^Any Help?
Thanks! -
Hello there, I have initialized my QSqlTableModel like this:
QSqlTableModel *model = new QSqlTableModel(this, db);
model->setTable("path_master_table");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();ui->tableView->setModel(model);
ui->tableView->sortByColumn(0, Qt::AscendingOrder);I can view my table very well.
Now i want the table to change when i run a query on it. So I did this.QSqlQuery query(query_text); (where query text is a sql command)
model->setQuery(query);I get the following error:
error: 'void QSqlTableModel::setQuery(const QSqlQuery&)' is protected
void setQuery(const QSqlQuery &query);
^error: within this context
model->setQuery(query);
^Any Help?
Thanks!Hi @Antweb Since setQuery is protected you cannot use it directly but only in its subclass.
You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.
Either use
QSqlQueryModelor if you just want to filter the rows the use setFilter and specify the where clause there. -
I basically want to filter as I am entering route no in my lineedit. I have connected the lineedit valuechanged signal to a slot where I have written the above code.
If not this way then what is the way to do that?@Antweb Shouldn't you use
likewhen using%? I'm not sure why it is crashing but since you want some realtime filtering its better to use QSortFilterProxyModel here. Checkout the examples Qt installed directory on your system. -
I used like and the command is working if I call it in the constructor. But when I am calling in the slot connected to my valuechanged of lineedit it crashes. And I want to reduce the size of my table with each added character. I don't think that is possible with QSortFIlterProxyModel.
-
I used like and the command is working if I call it in the constructor. But when I am calling in the slot connected to my valuechanged of lineedit it crashes. And I want to reduce the size of my table with each added character. I don't think that is possible with QSortFIlterProxyModel.
@Antweb OfCourse it is possible. You just have to re-implement filterAcceptsRow and put a condition as to what should be displayed. You have to just connect the textchanged signal to a slot where you will update the text using filterRegExp. Check the filtering subtopic for more details.
-
I am able to filter my QSqlTableModel with the help of text in lineedit. If simultaneously i want to filter the same table from selection in a combobox. How can i do that. Will I need a separate QSortFIlterProxyModel? Also i want the text selected from combobox to filter this QSqlTableModel on the basis of another Column.
This is my code right now:
QSqlTableModel *model = new QSqlTableModel(this, db); model->setTable("path_master_table"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(model); ui->tableView->setModel(proxyModel); connect(ui->lineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterRegExp(QString)));Thanks.
-
I am able to filter my QSqlTableModel with the help of text in lineedit. If simultaneously i want to filter the same table from selection in a combobox. How can i do that. Will I need a separate QSortFIlterProxyModel? Also i want the text selected from combobox to filter this QSqlTableModel on the basis of another Column.
This is my code right now:
QSqlTableModel *model = new QSqlTableModel(this, db); model->setTable("path_master_table"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this); proxyModel->setSourceModel(model); ui->tableView->setModel(proxyModel); connect(ui->lineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterRegExp(QString)));Thanks.
If simultaneously i want to filter the same table from selection in a combobox. How can i do that. Will I need a separate QSortFIlterProxyModel?
No. Separate
QSortFIlterProxyModelis not needed. Connect the signal currentIndexChanged which sends aQStringto the slot where you can updatefilterRegExpwith this text as done earlier.Also i want the text selected from combobox to filter this QSqlTableModel on the basis of another Column.
In that case you will need to override filterAcceptsColumn.
-
On what basis should i do the segregation while overriding.
Sorry, I am unable to figure out!@Antweb That will need subclassing. Filtering by column can also be done by filterKeyColumn.
Here is a complete example. It is forQTreeViewbut can also be done forQTableViewis the same way. -
I was able to do it with some simple signals and slots concept by resetting setFilterKeyColumn.
Complicating things further, is there a way to filter rows using the lineedit which have already been filtered once by the combobox?
Because right now it goes back to the original table and then filters! -
I was able to do it with some simple signals and slots concept by resetting setFilterKeyColumn.
Complicating things further, is there a way to filter rows using the lineedit which have already been filtered once by the combobox?
Because right now it goes back to the original table and then filters!