0

I am creating a simple qt application to provide details and login to the application and retrieve details from the database.It has mainly 2 forms(MainWindow and Dialog) A DBconncetion class has been written to obtain a database connection! I used the DBconnection class to log into application by giving details via the MainWindow form! but I don't know how to keep the connection that I opened in the MainWindow form and use it to retreive the data to the tableview in the Dialog form.

mycode is as follows

DBconnection.h (working successfully)

 public:
        QSqlDatabase mydb;
        bool connOpen(QString uname,QString pword,QString ip,int port,QString dbname){
            mydb=QSqlDatabase::addDatabase("QOCI","MyDB");
            mydb.setUserName(uname);
            mydb.setPassword(pword);
            mydb.setHostName(ip);
            mydb.setPort(port);
            mydb.setDatabaseName(dbname);
            mydb.open();
            return true;

        }

MainWindow.cpp (working successfully)

void MainWindow::on_pushButton_clicked()
    {
        DBconnection con;
        if(con.connOpen(ui->lineEdit->text(),ui->lineEdit_2->text(),ui->lineEdit_3->text(),ui->lineEdit_4->text().toInt(),ui->lineEdit_5->text())){
            Dialog dialog1;

            dialog1.setModal(true);
            dialog1.exec();

        }
   }

Dialog.cpp (not working)

void Dialog::on_pushButton_clicked()
{
    QSqlQueryModel *modal = new QSqlQueryModel();
    con.connOpen();

    QSqlQuery* qry=new QSqlQuery(con.mydb);


    qry->prepare("select NAME FROM TEST1");
       qry->exec();
       modal->setQuery(*qry);
       ui->tableView->setModel(modal);

}

How can I adjust my code so that I can retrieve data to the tablewidget in Dialog form from the connection I made from the MainWindow form?

Kushan Peiris
  • 167
  • 3
  • 15
  • I think you're looking for this https://docstore.mik.ua/orelly/linux/sql/ch13_02.htm You want to use the same object so use the C++ OOP and not C Style Programming. – CocoCrisp Sep 18 '17 at 07:06

1 Answers1

1

You could either pass a reference to the connection to your dialog, or make the connection static/global.

e.g.1

class Dialog()
{
  DBconnection &con;
  Dialog(DBconnection &con) : con(con) {};
};

Instead of a reference, you might also want to use a std::shared_ptr.

A nice way of making the connection global/static would be through the Service locator pattern. This pattern uses a central registry known as the "service locator", which on request returns the information necessary to perform a certain task.

You might also want to have a look into things related to "Dependency injection"

Lanting
  • 3,060
  • 12
  • 28
  • Is it in the Dialog.h or Dialog.cpp I need to place the above code? – Kushan Peiris Sep 18 '17 at 07:17
  • You should add `DBconnection &con;` to your dialog.h header, and you should modify the constructor of your dialog to include the connection argument and add the connection to the constructors class member initializer list. – Lanting Sep 18 '17 at 07:20
  • `shared_ptr` is a good concept here @Lanting , also remember to destroy the object if you're not using `shared_ptr` – CocoCrisp Sep 18 '17 at 08:02
  • @MilindW can you put a link for shared_ptr? – Kushan Peiris Sep 18 '17 at 09:42
  • @KushanPeiris Go through this to learn more about `shared_ptr` https://msdn.microsoft.com/en-us/library/hh279669.aspx – CocoCrisp Sep 18 '17 at 10:34