Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved connecting to database when a form loads using signals and slots

    General and Desktop
    qtcreator forms signal & slot database
    4
    4
    1279
    Loading More Posts
    • 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.
    • L
      Lasith last edited by Lasith

      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?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

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

        M 1 Reply Last reply Reply Quote 0
        • M
          mostefa @SGaist last edited by

          @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

          ?

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @mostefa last edited by

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

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply Reply Quote 2
            • First post
              Last post