Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to access QSqlRelationalModel from function?
Forum Updated to NodeBB v4.3 + New Features

How to access QSqlRelationalModel from function?

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 2 Posters 8.3k 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 Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi

    void refresh_devices_search_tbl(){
    Ui::RepairDevices *ui; <<<< what is this ? looks nasty, Did you even allocate it ? Looks bad and wrong to me :)
    }

    why is refresh_devices_search_tbl not member of RepairDevices ?

    If it was then repairsmodel is available to it.

    RepairDevices::refresh_devices_search_tbl

    1 Reply Last reply
    1
    • P Offline
      P Offline
      Panoss
      wrote on last edited by Panoss
      #3

      The 'Ui::RepairDevices *ui' I put it for someone to norice it and tell me if it's correct.
      I made the function a private member, so now I have access to ui and repairsmodel.
      But when the execution goes to repairsmodel->setFilter("id>76") (this is in the function), the application breaks down.
      "The program has unexpectedly finished."

      I tried in debugging, when code execution goes to repairsmodel->setFilter("id>76"), I get this error:
      alt text

      mrjjM 1 Reply Last reply
      0
      • P Panoss

        The 'Ui::RepairDevices *ui' I put it for someone to norice it and tell me if it's correct.
        I made the function a private member, so now I have access to ui and repairsmodel.
        But when the execution goes to repairsmodel->setFilter("id>76") (this is in the function), the application breaks down.
        "The program has unexpectedly finished."

        I tried in debugging, when code execution goes to repairsmodel->setFilter("id>76"), I get this error:
        alt text

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #4

        @Panoss

        im not sure what you are doing but just make
        refresh_devices_search_tbl a function in
        RepairDevices

        and you dont need anything else.

        RepairDevices::RepairDevices()
        {
        repairsmodel = new QSqlRelationalTableModel(this); // make it
        }

        void RepairDevices::refresh_devices_search_tbl(){
        Ui::RepairDevices *ui; // DONT DO THIS. its evil :)

        repairsmodel->setFilter("id>76"); // use it
        

        }

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Panoss
          wrote on last edited by
          #5

          This error appeared after I did exactly what you 're suggesting.

          mrjjM 1 Reply Last reply
          0
          • P Panoss

            This error appeared after I did exactly what you 're suggesting.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #6

            @Panoss
            if u still have Ui::RepairDevices *ui
            then no wonder :)

            Ui::RepairDevices *ui;
            QString sn = ui->sn_search_txt->toPlainText(); ///// BIG FAT CRASH HERE

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panoss
              wrote on last edited by
              #7

              I don't have it, I 've deleted it.

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

                ok,
                then debug it. find where you crash.

                This also looks a bit odd to me

                RepairDevices::RepairDevices()
                {
                repairsmodel = new QSqlRelationalTableModel(this);
                }

                its that the constructor ?
                What about the stuff for QMainWindow ?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Panoss
                  wrote on last edited by
                  #9

                  I told you.I posted the image of the error above.
                  Isn't it visible?

                  mrjjM 1 Reply Last reply
                  0
                  • P Panoss

                    I told you.I posted the image of the error above.
                    Isn't it visible?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #10

                    @Panoss
                    Yes but it jsut says. Programs crashed.
                    So find your error using the debugger :)

                    If i should guess

                    i think
                    RepairDevices::RepairDevices()
                    {
                    repairsmodel = new QSqlRelationalTableModel(this);
                    }

                    is never called and you crash at this line
                    repairsmodel->setFilter("id>76"); // use it
                    since repairsmodel is just a dangling pointer.

                    P 1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Panoss
                      wrote on last edited by
                      #11

                      mrjj forgive my ignorance, I'm a total newbee.
                      I suppose this is the error:
                      ...repairdevices.cpp:115: error: Exception at 0x4029fb, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)

                      mrjjM 1 Reply Last reply
                      0
                      • P Panoss

                        mrjj forgive my ignorance, I'm a total newbee.
                        I suppose this is the error:
                        ...repairdevices.cpp:115: error: Exception at 0x4029fb, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #12

                        @Panoss
                        Ok fair enough.

                        The errors says that something in the program was fiddling around with some memeory it should not.

                        so try this and tell me if crash goes away

                        
                        RepairDevices::RepairDevices()
                        {
                         //////   OUT	repairsmodel = new QSqlRelationalTableModel(this);
                        }
                        	
                        void refresh_devices_search_tbl(){
                         // make it here jsut to test
                        	repairsmodel = new QSqlRelationalTableModel(this); 
                        
                        	QString sn = ui->sn_search_txt->toPlainText();
                        	
                        	repairsmodel->setFilter("id>76");
                        
                            qDebug() << "in refresh_devices_search_tbl";
                        }
                        

                        also please use
                        qDebug()
                        like in sample. it helps alot to see what is called.

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Panoss
                          Yes but it jsut says. Programs crashed.
                          So find your error using the debugger :)

                          If i should guess

                          i think
                          RepairDevices::RepairDevices()
                          {
                          repairsmodel = new QSqlRelationalTableModel(this);
                          }

                          is never called and you crash at this line
                          repairsmodel->setFilter("id>76"); // use it
                          since repairsmodel is just a dangling pointer.

                          P Offline
                          P Offline
                          Panoss
                          wrote on last edited by
                          #13

                          @mrjj said in How to access QSqlRelationalModel from function?:

                          @Panoss
                          RepairDevices::RepairDevices()
                          {
                          repairsmodel = new QSqlRelationalTableModel(this);
                          }

                          is never called and you crash at this line

                          I think you 're right, somehow refresh_devices_search_tbl() is called before repairsmodel = new QSqlRelationalTableModel(this);!!!
                          I don't understand how is this possible...

                          mrjjM 1 Reply Last reply
                          0
                          • P Panoss

                            @mrjj said in How to access QSqlRelationalModel from function?:

                            @Panoss
                            RepairDevices::RepairDevices()
                            {
                            repairsmodel = new QSqlRelationalTableModel(this);
                            }

                            is never called and you crash at this line

                            I think you 're right, somehow refresh_devices_search_tbl() is called before repairsmodel = new QSqlRelationalTableModel(this);!!!
                            I don't understand how is this possible...

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #14

                            @Panoss said in How to access QSqlRelationalModel from function?:

                            RepairDevices::RepairDevices()
                            {
                            repairsmodel = new QSqlRelationalTableModel(this);
                            }

                            This looks like the constructor but most likely you are using the one you get from
                            QMainWindow that takes a parent.
                            and hence yours is never called.

                            show how you create the RepairDevices instance ?

                            ( i mean where you do like )
                            RepairDevices *mine= new RepairDevices ( ? )

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              Panoss
                              wrote on last edited by
                              #15

                              In main.cpp, this has this code:

                              #include "repairdevices.h"
                              #include <QtWidgets>
                              
                              
                              
                              int main(int argc, char * argv[])
                              {
                                  Q_INIT_RESOURCE(books);
                              
                                  QApplication app(argc, argv);    
                              
                                  RepairDevices win;
                                  win.show();
                              
                                  return app.exec();
                              }
                              
                              mrjjM 1 Reply Last reply
                              0
                              • P Panoss

                                In main.cpp, this has this code:

                                #include "repairdevices.h"
                                #include <QtWidgets>
                                
                                
                                
                                int main(int argc, char * argv[])
                                {
                                    Q_INIT_RESOURCE(books);
                                
                                    QApplication app(argc, argv);    
                                
                                    RepairDevices win;
                                    win.show();
                                
                                    return app.exec();
                                }
                                
                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #16

                                @Panoss said in How to access QSqlRelationalModel from function?:

                                Hmm i would have expected it to call
                                your constructor but it seems not. Mainwindow must have one that qualify.

                                Well it seems to use the one u get from your
                                base class and hence yours is never called.

                                so that is why.

                                Using a debugger this would be really clear :)

                                a normal mainwindow constructor looks like this

                                explicit MainWindow(QWidget *parent = 0);
                                
                                MainWindow::MainWindow(QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::MainWindow)
                                {
                                }
                                
                                P 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @Panoss said in How to access QSqlRelationalModel from function?:

                                  Hmm i would have expected it to call
                                  your constructor but it seems not. Mainwindow must have one that qualify.

                                  Well it seems to use the one u get from your
                                  base class and hence yours is never called.

                                  so that is why.

                                  Using a debugger this would be really clear :)

                                  a normal mainwindow constructor looks like this

                                  explicit MainWindow(QWidget *parent = 0);
                                  
                                  MainWindow::MainWindow(QWidget *parent) :
                                      QMainWindow(parent),
                                      ui(new Ui::MainWindow)
                                  {
                                  }
                                  
                                  P Offline
                                  P Offline
                                  Panoss
                                  wrote on last edited by Panoss
                                  #17

                                  @mrjj said in How to access QSqlRelationalModel from function?:

                                  @Panoss said in How to access QSqlRelationalModel from function?:

                                  Hmm i would have expected it to call
                                  your constructor but it seems not.
                                  I thought 'RepairDevices win' calls the constructor.

                                  @mrjj said in How to access QSqlRelationalModel from function?:

                                  @Panoss said in How to access QSqlRelationalModel from function?:
                                  Using a debugger this would be really clear :)
                                  I did use the debugger but it was not clear at all to me :).

                                  It still isn't, what must I do?
                                  I ''l do mainwindow constructor as you suggested and post the result.

                                  I did the test you suggested me above, I had to change a dot to -> (ui->sn_search_txt to ui.sn_search_txt)

                                          repairsmodel = new QSqlRelationalTableModel(this);
                                          QString sn = ui.sn_search_txt->toPlainText();
                                          repairsmodel ->setFilter("id>76");
                                          qDebug() << "in refresh_devices_search_tbl";
                                  

                                  and got no error.
                                  The etext is not printed, so the refresh_devices_search_tbl is not called now.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • P Panoss

                                    @mrjj said in How to access QSqlRelationalModel from function?:

                                    @Panoss said in How to access QSqlRelationalModel from function?:

                                    Hmm i would have expected it to call
                                    your constructor but it seems not.
                                    I thought 'RepairDevices win' calls the constructor.

                                    @mrjj said in How to access QSqlRelationalModel from function?:

                                    @Panoss said in How to access QSqlRelationalModel from function?:
                                    Using a debugger this would be really clear :)
                                    I did use the debugger but it was not clear at all to me :).

                                    It still isn't, what must I do?
                                    I ''l do mainwindow constructor as you suggested and post the result.

                                    I did the test you suggested me above, I had to change a dot to -> (ui->sn_search_txt to ui.sn_search_txt)

                                            repairsmodel = new QSqlRelationalTableModel(this);
                                            QString sn = ui.sn_search_txt->toPlainText();
                                            repairsmodel ->setFilter("id>76");
                                            qDebug() << "in refresh_devices_search_tbl";
                                    

                                    and got no error.
                                    The etext is not printed, so the refresh_devices_search_tbl is not called now.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Panoss said in How to access QSqlRelationalModel from function?:

                                    refresh_devices_search_tbl

                                    who calls it ?
                                    Is it a slot for a button or how should it be called?

                                    P 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @Panoss said in How to access QSqlRelationalModel from function?:

                                      refresh_devices_search_tbl

                                      who calls it ?
                                      Is it a slot for a button or how should it be called?

                                      P Offline
                                      P Offline
                                      Panoss
                                      wrote on last edited by Panoss
                                      #19

                                      It 'a a slot for two combo boxes and two textboxes.
                                      It 's called on textChanged and currentIndexChanged.
                                      (omg text and index of combos I think do get changed during form's load, e.g when they are mapped to the data mapper.)

                                      mrjjM 1 Reply Last reply
                                      0
                                      • P Panoss

                                        It 'a a slot for two combo boxes and two textboxes.
                                        It 's called on textChanged and currentIndexChanged.
                                        (omg text and index of combos I think do get changed during form's load, e.g when they are mapped to the data mapper.)

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #20

                                        @Panoss
                                        well just check the
                                        connects and its should be clear why its not called.
                                        (connect return true / false)

                                        At some point you will also need to learn to use the debugger and set break points.
                                        Its THE BEST tool to find out what is happening and where program goes. :)

                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          Panoss
                                          wrote on last edited by Panoss
                                          #21

                                          I found the guilty!!With the debugger.
                                          It's a combo box which calls the function at oncurrentIndexChanged event!
                                          This means that when a combo gets data from a model, the oncurrentIndexChanged event is called!!
                                          How can I fix this?

                                          mrjjM 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 Extensions
                                          • Unsolved