connecting to database when a form loads using signals and slots



  • I have 2 forms in my app namely MainWindow and Display! The MainWindow has the database connection method named getConnection (works successfully). When I fill database details and click a button in the MainWindow form the Display form is loaded! I want the Display form to have the getconnection method when the form loads! For that I use Signal and slot concept!

    following is my getconnection method(works)

    void MainWindow::getValues(){
    mydb.setDatabaseName(ui->sname->text());
    mydb.setUserName(ui->uname->text());
    mydb.setPassword(ui->pword->text());
    mydb.setPort(ui->port->text().toInt());
    mydb.setHostName(ui->ip->text());

    }

    The following code is from my Display.cpp

    Display::Display(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Display)
    {
    ui->setupUi(this);
    connect(ui->Display,SIGNAL(accepted()),MainWindow,SLOT(getConnection));

    }

    But the connection does not seem to exist! How can I adjust this to get the database connection?


  • Lifetime Qt Champion

    Hi,

    Besides the fact that the connect statement is wrong (you can't connect a class), it should be in your MainWindow object. There's no reason for "Display" to have any knowledge of the MainWindow class.



  • @SGaist said in connecting to database when a form loads using signals and slots:

    Hi,

    Besides the fact that the connect statement is wrong (you can't connect a class), it should be in your MainWindow object. There's no reason for "Display" to have any knowledge of the MainWindow class.

    I think that this post is duplicate

    https://forum.qt.io/topic/83307/qsqlquery-prepare-database-not-open

    ?


  • Qt Champions 2016

    @mostefa said in connecting to database when a form loads using signals and slots:

    I think that this post is duplicate

    Strictly speaking it isn't, although it's quite similar.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.