Search code examples
databaseqtserverclient

How to connect Server application and Client Application , Qt


I have a server application connected to a database like this:

Database::Database(QObject *parent) : QObject(parent)
{
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("main");
    db.setUserName("postgres");
    db.setPort(5432);
    db.setPassword("zalfon19");
    if(!db.open()){
        auto problemDialog = new InfoDialog(InfoDialog::Warning, "Baza danych", "Aplikacja napotkała na problem z połączeniem z bazą danych", nullptr, true);
        problemDialog->show();
        qDebug()<<"There is a problem with database.";
        return;
    }
    qDebug()<<"There is no problem with database.";

    QSqlQuery query;
    query.exec("truncate active_errors");
}

The client application without necessary changes to explain my situation:

Database::Database(QObject *parent) : QObject(parent)
{
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName(settings.dbpath.get());
    db.setDatabaseName("main");
    db.setUserName("postgres");
    db.setPort(5432);
    db.setPassword("zalfon19");
    if(!db.open()){
        auto problemDialog = new InfoDialog(InfoDialog::Warning, "Baza danych", "Aplikacja napotkała na problem z połączeniem z bazą danych", nullptr, true);
        problemDialog->show();
        qDebug()<<"There is a problem with database.";
    }
    qDebug()<<"There is no problem with database.";

}

Solution

  • My root problem was not the same input data between Pgadmin and Qt.

    db = QSqlDatabase::addDatabase("QPSQL");
        db.setHostName("localhost");
        db.setDatabaseName("main");
        db.setUserName("postgres");
        db.setPort(5432);
        db.setPassword("password");
    

    all this should be the same.