1

I'm a newbie in Qt (although I have some experience with C/C++/Java/PHP). I'm trying to migrate my older program that use PostgreSQL database into Qt GUI. I'm using PostgreSQL v2 and have downloaded the recent QT 5.3.0 (for MinGW 32-bit).

I created an example widget application, called Anu. The Anu.pro file looks like this :

QT       += sql
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Anu
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

And the mainwindow.cpp looks like this :

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QtSql>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName("localhost");
    db.setDatabaseName("basicaccount");
    db.setUserName("postgres");
    db.setPassword("root");
    db.setPort(5435);

    bool ok = db.open();
    if(ok != true)
    {
        QMessageBox::information(this,"Connection","Connection Failed!") ;
    }
    else
    {
        QMessageBox::information(this,"Connection","Connection OK!") ;

        QSqlQueryModel model;
        model.setQuery("select * from invoice ");

        //ui->tableView->setModel(&model);
        QMessageBox::information(this,"Information","This Message box is needed in       order to see the rendered tableview!") ;

    }
}

The error in compile shows QSqlDatabase: QPSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7. I tried to build the plugin using the information here : http://qt-project.org/doc/qt-5/sql-driver.html. I opened the command prompt for Qt and put this :

cd E:\Qt\Qt5.3.0\5.3\mingw482_32\plugins\sqldrivers\psql
qmake "INCLUDEPATH+=E:\Program Files\PostgreSQL\9.2\include" "LIBS+=E:\Program Files\PostgreSQL\9.2\lib\libpq.lib" psql.pro
nmake

The problem is, I can't find the src folder or psql on folder sqldrivers. I've only found qsqlpsql.dll on that folder, and everytime I used it it says directory can't be found. Also, what's 'psql.pro' mean?

Thanks a lot for any help.

1 Answer 1

2

The most easy way, you need the following; Depending on your application, 64 or 32 bit If postgresql is 64 or 32 bit it doesn't matter, as the mingw is configured for 32 bit applications. You need the 32 bit dlls, If your version of postgre is 32 bit, then copy following dll's from your postgresql installations bin folder libeay32.dll, libintl.dll, libpq.dll, and ssleay32.dll, to your qt version mingw bin folder, now your program should work

If postgresql is 64 bit, the the included dll's doesn't work out of the box these are 64 bit !

You can still use 64 bit postgresql DB, no problem but you need the 32 bits dll's for your application. There are more sophisticated ways to do this but to kick start your application this is considered the fastest.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.