QSqlQueryModel and QTableView closes program.
Hello, I'm new in QT.
I made program like below::
queryString = "SELECT " + queryField + " FROM partiRegi WHERE " + queryField + " LIKE '%" + searchText + "%'";
QSqlQueryModel *searchModel = new QSqlQueryModel;
searchModel->setHeaderData(0, Qt::Horizontal, tr("Result"));
searchResultView = new QTableView; searchResultView->setModel(searchModel); searchResultView->setSelectionBehavior(QAbstractItemView::SelectRows); searchResultView->setSelectionMode(QAbstractItemView::SingleSelection); searchResultView->horizontalHeader()->setStretchLastSection(true); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(searchResultView); searchResultPanel->setLayout(layout);
Everytime "searchText" changed I delete "searchResultPanel" and re-layout it.
I had succeed using this with QSqlRelationalTableModel + QTableView.
But when I click in QSqlQueryModel + QTableView, program closes with error.
"debug\WithoutUI.exe exited with code -1073741819"
Can anybody tell me what is the problem??
Edit: please use @ tags around your code sections; Andre
Are you using QtCreator? If so, run the debugger (Top menu: Debug -> Start debugging -> Start debugging). Then, when it crashes, you debugger can show you what triggered the problem.
Thank you JKSH.
I run the debugger and pop-up says
The inferior stopped because it received a signal from the OS.
Signal Name : SIGSEGV
Signal meaning : Segmentation fault."
What this means??
When I did FindAll and click in the QTableView it does not go wrong.
Is there some error in query or TableView??
You get a "segmentation fault" if your program tries to read or write memory that it is not allowed to access. For example, if you delete searchResultPanel, and you don't construct a new Panel, but you still call searchResultPanel->setLayout(), this error will occur. I can't see anything wrong in the code you posted, so the error begins somewhere else.
Go back to QtCreator, and Start Debugging again. When you get the SIGSEGV, look at your code in QtCreator: There will be a yellow arrow pointing to your error.
I accidently SOLVED!!
I programed delete searchResultPanel and re-new it right away.
delete searchResultPanel; searchResultPanel = new QWidget(groupBox); verticalLayout_2->addWidget(searchResultPanel);
But the problem was SIGNAL/SLOT!!
I programed connect lineEdit's editFinished SIGNAL with refreshing the panel.
Before doing your second advice I changed 'editFinished' SIGNAL to 'textChanged' and it works VERY WELL!!
Anyway, thank you for your advices and have a good day ;)