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 can a child form use parent 's form model
Forum Updated to NodeBB v4.3 + New Features

How can a child form use parent 's form model

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 913 Views
  • 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #1

    I have a form named 'ArticlesWindow' and I open another form named 'addArticle' this way:

    void ArticlesWindow::on_newArticle_btn_clicked()
    {   
        class addArticle *m = new class addArticle ();
        m->setModal(true);
        m->setAttribute(Qt::WA_DeleteOnClose);
        m->show();    
    }
    

    With form 'addArticle' I want to add a new Article, insert a new one.
    So I will have to use the model (a QSqlRelationalTableModel) of the parent form ('ArticlesWindow') from within the child form.

    How can I do this?

    JonBJ 1 Reply Last reply
    0
    • P Panoss

      I have a form named 'ArticlesWindow' and I open another form named 'addArticle' this way:

      void ArticlesWindow::on_newArticle_btn_clicked()
      {   
          class addArticle *m = new class addArticle ();
          m->setModal(true);
          m->setAttribute(Qt::WA_DeleteOnClose);
          m->show();    
      }
      

      With form 'addArticle' I want to add a new Article, insert a new one.
      So I will have to use the model (a QSqlRelationalTableModel) of the parent form ('ArticlesWindow') from within the child form.

      How can I do this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Panoss
      Pass a pointer or a reference to the QSqlRelationalTableModel from the parent form into the child form. You could do that by adding a parameter to child's constructor, or have an explicit setModel() method exposed after construction.

      Alternatively, it isn't always a good idea to pass models around and have UI windows/dialogs update it. It may be better to have your sub-forms gather information from the user, put it into a struct/class or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates. Can help to keep things "tidy" like that, then the UI forms don't have their own logic for updating the data, when over time you find you wish to re-use code or manage it all in one place.

      P 2 Replies Last reply
      3
      • JonBJ JonB

        @Panoss
        Pass a pointer or a reference to the QSqlRelationalTableModel from the parent form into the child form. You could do that by adding a parameter to child's constructor, or have an explicit setModel() method exposed after construction.

        Alternatively, it isn't always a good idea to pass models around and have UI windows/dialogs update it. It may be better to have your sub-forms gather information from the user, put it into a struct/class or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates. Can help to keep things "tidy" like that, then the UI forms don't have their own logic for updating the data, when over time you find you wish to re-use code or manage it all in one place.

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

        @JonB said in How can a child form use parent 's form model:

        @Panoss
        It may be better to have your sub-forms gather information from the user, put it into a struct/class or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates.

        But if something goes wrong with the new record's insertion, then the parent form will have to emit a signal back to the child...
        I 'll try this but sounds quite complicated.

        JonBJ 1 Reply Last reply
        0
        • P Panoss

          @JonB said in How can a child form use parent 's form model:

          @Panoss
          It may be better to have your sub-forms gather information from the user, put it into a struct/class or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates.

          But if something goes wrong with the new record's insertion, then the parent form will have to emit a signal back to the child...
          I 'll try this but sounds quite complicated.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Panoss said in How can a child form use parent 's form model:

          I 'll try this but sounds quite complicated.

          Then at least start out with the first way, which is the way you're currently wanting to do it...

          1 Reply Last reply
          0
          • JonBJ JonB

            @Panoss
            Pass a pointer or a reference to the QSqlRelationalTableModel from the parent form into the child form. You could do that by adding a parameter to child's constructor, or have an explicit setModel() method exposed after construction.

            Alternatively, it isn't always a good idea to pass models around and have UI windows/dialogs update it. It may be better to have your sub-forms gather information from the user, put it into a struct/class or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates. Can help to keep things "tidy" like that, then the UI forms don't have their own logic for updating the data, when over time you find you wish to re-use code or manage it all in one place.

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

            @JonB said in How can a child form use parent 's form model:

            @Panoss
            Pass a pointer or a reference to the QSqlRelationalTableModel from the parent form into the child form. You could do that by adding a parameter to child's constructor

            I tried this but obviously I 'm not doing ig correctly:

            #define ADDARTICLE_H
            
            #include <QDialog>
            #include <QtSql>
            
            namespace Ui {
            class addArticle;
            }
            
            class addArticle : public QDialog
            {
                Q_OBJECT
            
            public:
                explicit addArticle(QWidget *parent = nullptr, QSqlRelationalTableModel *model);
                ~addArticle();
            
            private:
                Ui::addArticle *ui;
            };
            
            #endif // ADDARTICLE_H
            

            (the code I added is: ', QSqlRelationalTableModel *model' and I get error "addarticle.h:16:78: error: missing default argument on parameter 'model'")

            JonBJ 1 Reply Last reply
            0
            • P Panoss

              @JonB said in How can a child form use parent 's form model:

              @Panoss
              Pass a pointer or a reference to the QSqlRelationalTableModel from the parent form into the child form. You could do that by adding a parameter to child's constructor

              I tried this but obviously I 'm not doing ig correctly:

              #define ADDARTICLE_H
              
              #include <QDialog>
              #include <QtSql>
              
              namespace Ui {
              class addArticle;
              }
              
              class addArticle : public QDialog
              {
                  Q_OBJECT
              
              public:
                  explicit addArticle(QWidget *parent = nullptr, QSqlRelationalTableModel *model);
                  ~addArticle();
              
              private:
                  Ui::addArticle *ui;
              };
              
              #endif // ADDARTICLE_H
              

              (the code I added is: ', QSqlRelationalTableModel *model' and I get error "addarticle.h:16:78: error: missing default argument on parameter 'model'")

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Panoss said in How can a child form use parent 's form model:

              explicit addArticle(QWidget *parent = nullptr, QSqlRelationalTableModel *model);

              C++: You cannot have a parameter with a default (an "optional" parameter) before a compulsory parameter! For QWidget classes, keep parent parameter as the last one:

              addArticle(QSqlRelationalTableModel *model, QWidget *parent = nullptr);
              
              1 Reply Last reply
              3
              • P Offline
                P Offline
                Panoss
                wrote on last edited by Panoss
                #7

                This is how i did , but it crashes on the last line:

                addArticle::addArticle(QSqlRelationalTableModel *model, QWidget *parent) :
                    QDialog(parent),
                    ui(new Ui::addArticle){
                    ui->setupUi(this);
                    
                    //this->model=model;
                
                    // Table: articles, Fields: id, name, position
                
                    // Remember the indexes of the columns:
                    positionIdx = model->fieldIndex("position");
                
                    // bound field for combo is field 'position'
                    ui->positionEdit->setModel(model->relationModel(positionIdx));
                
                    // display field for combo is field 'name'
                    ui->positionEdit->setModelColumn(model->relationModel(positionIdx)->fieldIndex("name"));
                }
                

                (ui->positionEdit is a combo box on the child form)

                JonBJ 1 Reply Last reply
                0
                • P Panoss

                  This is how i did , but it crashes on the last line:

                  addArticle::addArticle(QSqlRelationalTableModel *model, QWidget *parent) :
                      QDialog(parent),
                      ui(new Ui::addArticle){
                      ui->setupUi(this);
                      
                      //this->model=model;
                  
                      // Table: articles, Fields: id, name, position
                  
                      // Remember the indexes of the columns:
                      positionIdx = model->fieldIndex("position");
                  
                      // bound field for combo is field 'position'
                      ui->positionEdit->setModel(model->relationModel(positionIdx));
                  
                      // display field for combo is field 'name'
                      ui->positionEdit->setModelColumn(model->relationModel(positionIdx)->fieldIndex("name"));
                  }
                  

                  (ui->positionEdit is a combo box on the child form)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Panoss said in How can a child form use parent 's form model:

                  but it crashes on the last line

                  That's what a debugger is for! Run it in debugger, allow to crash, look at stack trace window.

                  If a line "crashes", break it into separate code to test intermediate results. For all I know there may not even be a fieldIndex("name")....

                  1 Reply Last reply
                  1

                  • Login

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