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 pass QSqlRelationalTableModel object to a class constructor (from main.cpp)
Forum Updated to NodeBB v4.3 + New Features

How to pass QSqlRelationalTableModel object to a class constructor (from main.cpp)

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 263 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.
  • G Offline
    G Offline
    Gandalf404
    wrote on last edited by
    #1

    For example, partwidget.h:
    explicit PartWidget(QWidget parent = nullptr, QSqlRelationalTableModel model = new QSqlRelationalTableModel());

    partwidget.cpp:
    PartWidget::PartWidget(QWidget parent, QSqlRelationalTableModel model)
    : QWidget(parent)
    , ui(new Ui::PartWidget)
    , model(model)
    {
    ui->setupUi(this);
    }

    Pl45m4P 1 Reply Last reply
    0
    • G Gandalf404

      For example, partwidget.h:
      explicit PartWidget(QWidget parent = nullptr, QSqlRelationalTableModel model = new QSqlRelationalTableModel());

      partwidget.cpp:
      PartWidget::PartWidget(QWidget parent, QSqlRelationalTableModel model)
      : QWidget(parent)
      , ui(new Ui::PartWidget)
      , model(model)
      {
      ui->setupUi(this);
      }

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Gandalf404

      If you really need to pass the model around (maybe there's a better design to achieve what you are trying to do) then the same way like you would pass any other QObject-based class:

      explicit PartWidget(QSqlRelationalTableModel *model = nullptr, QWidget *parent = nullptr);
      
      PartWidget::PartWidget(QSqlRelationalTableModel *model, QWidget *parent)
      : QWidget(parent)
      , ui(new Ui::PartWidget)
      , m_model(model)
      {
         ui->setupUi(this);
      }
      

      This is more a C++ OOP question
      And please use Code Tags the next time you post code snippets.

      Edit:
      As I just read the title again:
      Why create the model in main and then pass it to some widget in the first place?
      Why not create the model right in the widget where you need it?!
      There shouldn't be too much going on in your main.cpp.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      G 1 Reply Last reply
      2
      • Pl45m4P Pl45m4

        @Gandalf404

        If you really need to pass the model around (maybe there's a better design to achieve what you are trying to do) then the same way like you would pass any other QObject-based class:

        explicit PartWidget(QSqlRelationalTableModel *model = nullptr, QWidget *parent = nullptr);
        
        PartWidget::PartWidget(QSqlRelationalTableModel *model, QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::PartWidget)
        , m_model(model)
        {
           ui->setupUi(this);
        }
        

        This is more a C++ OOP question
        And please use Code Tags the next time you post code snippets.

        Edit:
        As I just read the title again:
        Why create the model in main and then pass it to some widget in the first place?
        Why not create the model right in the widget where you need it?!
        There shouldn't be too much going on in your main.cpp.

        G Offline
        G Offline
        Gandalf404
        wrote on last edited by
        #3

        @Pl45m4 Code tags is like code in your answer displayed ? I just trying do not create unnecessary objects of QSqlRelationalTableModel, so i thought better just pass it through widgets. Can you recommend C++ or OOP related book ? Thank you for answer !

        Pl45m4P 1 Reply Last reply
        0
        • G Gandalf404

          @Pl45m4 Code tags is like code in your answer displayed ? I just trying do not create unnecessary objects of QSqlRelationalTableModel, so i thought better just pass it through widgets. Can you recommend C++ or OOP related book ? Thank you for answer !

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Gandalf404 said in How to pass QSqlRelationalTableModel object to a class constructor (from main.cpp):

          I just trying do not create unnecessary objects of QSqlRelationalTableModel, so i thought better just pass it through widgets

          Like I've said, create it in your widget where you need it.
          That would make one instance only.
          Or for what reason you need it in main()?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          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