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. Passing object to included modules
Forum Updated to NodeBB v4.3 + New Features

Passing object to included modules

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.3k Views 2 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.
  • RustR Offline
    RustR Offline
    Rust
    wrote on last edited by Rust
    #1

    I have a connection to an SQL database whose object is created in main.cpp, then there's a main window module (.cpp + .h) that is called in main.cpp, and from that window module several other windows are also called.

    I need to execute some queries on those windows through the SQL object that connects to the database.

    I've tried using an header file with a pointer to the database, this pointer is set in main.cpp and would be used to access the SQL object on other modules but the compiler reports multiple inclusions of that pointer throughout the program.

    How can i pass this object to trigger functions inside those windows e.g. button clicked, text changed, etc? Such functions do not take any input arguments and are somehow similar to interrupts.

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

      hi
      Once you have open a database, QSqlQuery will use this if you dont specify a
      db.
      also, you can name a connection and use function to look it up again.

      // In an object that has the same lifetime as your application
      // (or as a global variable, since it has almost the same goal here)
      QSqlDatabase db;
      
      // In the constructor or initialization function of that object       
      db = QSqlDatabase::addDatabase("QSQLDRIVER", "connection-name"); 
      db.setHostname(...);
      // ...
      if(!this->db.open())  // open it and keep it opened
      {
          // Error handling...
      }
      
      // --------
      // Anywhere you need it, you can use the "global" db object 
      // or get the database connection from the connection name        
      QSqlDatabase db = QSqlDatabase::database("connection-name"); 
      QSqlQuery query(db);
      

      from
      http://stackoverflow.com/questions/7669987/what-is-the-correct-way-of-qsqldatabase-qsqlquery

      1 Reply Last reply
      1
      • RustR Offline
        RustR Offline
        Rust
        wrote on last edited by Rust
        #3

        Before anything else, i would like to thank you, mrjj, for your input/help on the matter.

        I've managed to fix my problem, and the problem was that i may be a bit too used to working with pointers to allocated resources in memory.

        Since i can't use new in this case or else it'll create an invalid QSqlDatabase object, i created the sql object then returned it as a whole and passed it to a public function in the module mainwindow that copies it to an internal private QSqlDatabase variable/object. This way, the connection is still open between module calls and i can do sql queries inside those window modules in which the database connection object was not created from.

        Although this is not the most efficient type of programming i do not see any way around the problem other than this.

        I can keep copying the object to every included window module that gets added the root mainwindow mudole.

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The way around this problem is already presented by @mrjj
          There is no need to pass the database pointer around, just call

          QSqlDatabase db = QSqlDatabase::database("connection-name");
          

          to get the database connection you need.

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

          RustR 1 Reply Last reply
          1
          • jsulmJ jsulm

            The way around this problem is already presented by @mrjj
            There is no need to pass the database pointer around, just call

            QSqlDatabase db = QSqlDatabase::database("connection-name");
            

            to get the database connection you need.

            RustR Offline
            RustR Offline
            Rust
            wrote on last edited by Rust
            #5

            @jsulm

            Oh, i get it. My apologies for my misunderstanding @Moderators mrjj.
            Tested and it works. @jsulm

            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