Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TabWidget tabs, each with a model TableView. Crashing switching between tabs.
Qt 6.12 LTS is out! See what's new in the release blog

TabWidget tabs, each with a model TableView. Crashing switching between tabs.

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 6.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM mrjj

    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 ?

    C Offline
    C Offline
    Core2
    wrote on last edited by
    #7

    @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. xx

    I 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 //
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #8

      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.

      1 Reply Last reply
      1
      • mrjjM mrjj

        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 ?

        C Offline
        C Offline
        Core2
        wrote on last edited by
        #9
        This post is deleted!
        1 Reply Last reply
        0
        • mrjjM mrjj

          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 ?

          C Offline
          C Offline
          Core2
          wrote on last edited by
          #10
          This post is deleted!
          1 Reply Last reply
          0
          • C Core2

            @mrjj

            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.

            C Offline
            C Offline
            Core2
            wrote on last edited by
            #11

            @Core2

            @mrjj
            Reply Part 2
            animalsearch.cpp

            includes 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);
            }
            
            1 Reply Last reply
            0
            • C Core2

              @mrjj

              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.

              C Offline
              C Offline
              Core2
              wrote on last edited by Core2
              #12

              @Core2

              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.

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #13

                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 :)

                C 1 Reply Last reply
                1
                • mrjjM mrjj

                  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 :)

                  C Offline
                  C Offline
                  Core2
                  wrote on last edited by
                  #14

                  @mrjj

                  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++ Pointers

                  Interesting observation. The more I learn about c++/Qt the more I realize I don't know much:)

                  Thanks for the guidance.

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #15

                    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 :)
                    1 Reply Last reply
                    2
                    • Gojir4G Offline
                      Gojir4G Offline
                      Gojir4
                      wrote on last edited by
                      #16

                      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);
                      
                      C 1 Reply Last reply
                      3
                      • Gojir4G Gojir4

                        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);
                        
                        C Offline
                        C Offline
                        Core2
                        wrote on last edited by
                        #17

                        @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?

                        @mrjj

                        jsulmJ 1 Reply Last reply
                        0
                        • C Core2

                          @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?

                          @mrjj

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #18

                          @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?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          C 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @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?

                            C Offline
                            C Offline
                            Core2
                            wrote on last edited by
                            #19

                            @jsulm

                            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.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt
                            • Unsolved