Unsolved TabWidget tabs, each with a model TableView. Crashing switching between tabs.
-
Something I forgot to mention, I tried and experiment where I introduced a
slot void ProfileSearch::on_tabWidget_currentChanged(int index) { if(index==0){ //This is the search tab //here i placed all the code for creating a model and called the DB for the current search the user has entered int othe cmbFilter and lineBy box. this works. } if(index==1){ //here i placed all the code for creating the previoussearchmodel and query the db for that data and displayed the data to the PreviousSearch_View. this worked, but isn't a great idea because the code will keep hitting the database for the same data as many times as the user clicks on the previous search tab. } }
This code worked. The program no longer would crash when switching between tabs. This reconfirms my suspicions that the PreviousSearchModel data is lost after the user searches for something. I just don't understand why.
-
Hi
In
void ProfileSearch::on_tabWidget_currentChanged(int index)
{
if(index==0){ //This is the search tab
Did you use NEW here to create the models ?
SomeModel * model = new SomeModel;
so they survive after the function ends? -
Also worth of note in this test I didn't use the proxysortmodel.
I used:
QString GetPreviousSearchsQuery = "crazy long query string"; this->PreviousSearchModel = new QSqlQueryModel(); PreviousSearchModel->setQuery(GetPreviousSearchsQuery); ui->PreviousSearchView->setModel(PreviousSearchModel);
I don't know for certain if they survive. I would think they do not, but I don't know.
-
Hi
Since you have the syntax
PreviousSearchModel->xxx
( and not PreviousSearchModel.xxx)
It seems like you use pointers and
hence the models should survive fine.at what line does it crash ?
-
I'm trying to reply with a long post, but my post keeps getting flagged as spam.
I'm going to try to break my reply up into smaller chuncks. Maybe Akismet.com will like that better.
-
@mrjj
Reply Part 1
For the purpose of bettering my understanding of the language could you explain the differences in the syntax you just referred to? PreviousSearchModel->xx vs PreviousSearchModel. xxI can't answer your question truthfully. I was feeling overwhelmed by how unmanageable my code had become. The reason for that is bad coding practices and inexperience. I saved everything and created a new project and built just the bare minimum to experiment with this issue i'm having. This is the reason for the delay in my response to your question.
One major difference in my code is a created a sqlconnect.h file that contains a class sqlconnect that makes my life easier gathering data from a sql db from anywhere in my code. Before I had redundant code that was a pain to manage. Here is the sqlconnect.h contents:
#ifndef SQLCONNECT_H #define SQLCONNECT_H #include <QString> #include <QtSql> class sqlconnect { public: QSqlDatabase db; QSqlQueryModel *QueryModel; QSqlQueryModel* QueryDB(QString Query) { db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("AnimalsDB"); db.setUserName("Spiderman"); db.setPassword("12345"); this->QueryModel = new QSqlQueryModel(); if(db.open()) { QueryModel->setQuery(Query); } else { qDebug() << "Didn't work yo"; } return QueryModel; } };
Now in my animalsearch.h
#ifndef ANIMALSEARCH_H #define ANIMALSEARCH_H #include <QDialog> #include <QtSql> namespace Ui { class AnimalSearch; } class AnimalSearch : public QDialog { Q_OBJECT public: explicit AnimalSearch(QWidget *parent =0); ~AnimalSearch(); QSqlQueryModel *previousSearchModel; QSqlQueryModel *SearchModel; private slots: void on_OkButton_clicked(); void on_Searchbutton_clicked(); private: Ui::AnimalSearch *ui; }; #endif //
-
Yeah
Akismet.com can be a little flower sometimes but i hope it catches some real spam from time to time.
The syntax for accessing members depending on the allocation method.
http://net-informations.com/faq/net/stack-heap.htm
When you do
ClassX a; // stack. its a local variable. meaning it dies with the scope its in. function/ class
To access its member, you use var.xxx ( dot )
ClassX *a = new ClassX(); // heap. variable now points to memory and lives until delete is called on it
To access its member, you use var->xxx ( -> )When you have first type allocation, one should always be vary if passing to another class.
Especially if a & a needed ( take address )
but new (heap) allocation is often need for objects that we need to live longer than where they are allocated.
Hope this clears it up a bit. -
This post is deleted! -
This post is deleted! -
@mrjj
Reply Part 2
animalsearch.cppincludes section //akismet didn't like my includes...ugh AnimalSearch:: AnimalSearch (QWidget *parent) : QDialog(parent), ui(new Ui::AnimalSearch) { ui->setupUi(this); sqlconnect PreviousSearch; QString uname = qgetenv ("USER"); if (uname.isEmpty()) uname = qgetenv("USERNAME"); QString GetComplete = "long query to get previous searches using the currently logged on user"; this->PreviousSearchModel = new QSqlQueryModel(); Previous Search Model = PreviousSearch. QueryDB (GetComplete); ui->Previous Search Table View->set Model(Previous Search Model); } AnimalSearch:~AnimalSearch() { delete ui; } void AnimalSearch::on_OkButton_clicked() { close(); } void AnimalSearch::onSearchButton_clicked() { sqlconnect CurrentSearch; QString SearchQuery = "Hard coded query to search for box turtles"; this->SearchModel = new QSqlQueryModel(); SearchModel = CurrentSearch.QueryDB(SearchQuery); ui->SearchTableView->setModel(SearchModel); }
-
Reply Part 3
mainwindow.cpp
include mainwindow.h include ui_mainwindow.h include animalsearch.h //trust me the syntax is correct in the program. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { AnimalSearch animalsearch; animalsearch.setModal(true); animalsearch.exec(); //This is line 21. According to debugger this is where the crash occurs. }
Now, when this code is ran the previous search tabs QTableView is populated with previous searches.
I can click between the current search tab and the previous search tab without fail as many times as I'd like.
If i click the search button, which is hard coded to search for a box turtle the search tab QTableView populates with box turtle results. Cool good stuff.
But, when i click back on previous search tab i have a crash. I am shown the same error message as before. Exception at 0x7ffa38f1c1ea, code: 0xc0000005: read access violation at: 0x0, flgs=0x0 (first chance).
The program stops at line 21 of the mainwindow in the pushbutton slot that launches the animalsearch ui.I hope I did not complicate things moving away from the originally posted code.
Again thanks for your time.
-
Hi
I think you going to need to use the debugger and put break point in button, and single step into creation and showing animalsearch. until you find the line that makes it crash.I cant see/guess guess from the source, maybe others can guess the reason for the crash.
It really sounds like a dangling pointer :)
-
Ok. I will do just that. After reading your link from one of your previous posts about stack and heap, but still not completely understanding, i watched this guys videos, ReelLearning. I think I have a good grasp of what I should be looking for now. I will spend the day using debugging to try and solve the problem.
This video was very informative.
Pointers and Dynamic Memory in C++ (Memory Management)Good video as well.
Introduction to Pointers in C++, Tutorial on Pointers, C++ PointersInteresting observation. The more I learn about c++/Qt the more I realize I don't know much:)
Thanks for the guidance.
-
Hi
if you stil have doubts about stack vs heap, please ask/post
description of that still needs more info.
As its a critical part of C++ so one has to know when to use
heap in the cases where it will be ultra important.- Interesting observation. The more I learn about c++/Qt the more I realize I don't know much:)
Heh yep. I get that feeling from models and views in Qt :)
- Interesting observation. The more I learn about c++/Qt the more I realize I don't know much:)
-
Hello,
Isn't it just because you re-declare your proxy model in the constructor body of AnimalSearch ?
QSortFilterProxyModel *SearchHistorySortProxyModel = new QSortFilterProxyModel(this);
I think it shadows your member declared in the header as you have used the same name.
This should work better
this->SearchHistorySortProxyModel = new QSortFilterProxyModel(this);
-
@Gojir4
I will try that out when i switch back to the master branch. Currently I'm working on a new branch where i cut all the fat of the program. now it is simple and easy to understand.Also i noticed something while using the dbugger today. The error happens in qsql_odbc.cpp Unfortunately I don't see the file code i see the assembly code when the error pops up. So it looks Greek to me. Any suggestions?
-
@Core2 said in TabWidget tabs, each with a model TableView. Crashing switching between tabs.:
The error happens in qsql_odbc.cpp
Just check which line of your code was executed just before qsql_odbc.cpp
And did you fix the issue @Gojir4 pointed out? -
No I have not implemented the changes suggested by Gojir4 because I moved away from that code. At this time I'm not using a proxy model to simplify things.