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. Accessing database across multiple forms
Forum Updated to NodeBB v4.3 + New Features

Accessing database across multiple forms

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 310 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have two forms: Form1 and Form2. On Form1, I have:

    Form1.h
    
    private:
        QSqlDatabase myDB;
    
    Form1.cpp
    
    myDB = QSqlDatabase::addDatabase("QSQLITE");
    

    I can do anything I want with the database on Form1. Zero problems. But for adding data, deleting, editing, I created another Dialog Form to work with the data. Thing is, I can't figure out how to access myDB on Form2. I've added Form1.h to Form2.cpp, but i get an error that myDB is a private member of Form1.

    The only thing I can think of at this point is to remove the database in the function call that initiates Form2 and reopen myDB on Form2, having Form2.h and Form2.cpp setup the same way I have Form1.h and Form1.cpp. Although I haven't tried that yet, I know it'll work. But I'd like to know if there's a better way to do this. Any insight would be appreciated. Thanks.

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

      Hi,

      Please re-read the QSqlDatabase documentation. You should not store a local member variable of that type. QSqlDatabase already provides all the plumbing to retrieve databases connection when needed.

      Also, if you only connect to one database and use the default connection then you don't need to retrieve it on every use.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

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