Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSqlTableModel : SetQuery issue
Antweb last edited by Antweb
Hello there, I have initialized my QSqlTableModel like this:
QSqlTableModel *model = new QSqlTableModel(this, db);
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)
I get the following error:
error: 'void QSqlTableModel::setQuery(const QSqlQuery&)' is protected
void setQuery(const QSqlQuery &query);
error: within this context
You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.
QSqlQueryModelor if you just want to filter the rows the use setFilter and specify the where clause there.
I did this but my program keeps crashing.
QString value_entered = ui->lineEdit->text();
QString query_text = "Route = '" + value_entered + "%'";
Can you tell why?
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
%? 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.
@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.
Done. Thank you.
Antweb last edited by p3c0
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)));
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?
QSortFIlterProxyModelis not needed. Connect the signal currentIndexChanged which sends a
QStringto the slot where you can update
filterRegExpwith 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!
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!
@Antweb Unfortunately I too do not know how to filter a already filtered list. May be someone else can answer this question. You can start a new topic explain this requirement.