Important: Please read the Qt Code of Conduct -

Passing object to included modules

  • 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.

  • Lifetime Qt Champion

    Once you have open a database, QSqlQuery will use this if you dont specify a
    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"); 
    // ...
    if(!this->  // 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);


  • 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.

  • Lifetime Qt Champion

    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.

  • @jsulm

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

Log in to reply