Qt driver assembly for working with PostgreSQL (Windows)

  • Tutorial
A small lyrical digression: To
write this note, I was encouraged by the presence on the Internet of a huge number of instructions that simply quote the documentation, but really do not work. Because Since I work with Qt from time to time, I don’t know how things are with this problem in Qt5.


Problem


The driver for working with Postgres is not included in the default installation (at least for version 4.8.4).
The official documentation suggests collecting it in a very simple way http://qt-project.org/doc/qt-4.8/sql-driver.html#qpsql , but if you downloaded the already built Qt, you will be pretty puzzled that the assembly goes without errors, but nothing works.

Qt and SQL


Here you should go a little deeper into the features of working with sql drivers in Qt. There are two possible options for their implementation:
  • embedding a driver in the QtSql library
  • compiling the driver as a plugin

When building Qt ( http://doc.qt.digia.com/4.7/configure-options.html ), the following options are responsible for this
  • -qt-sql- Enable a SQL  in the Qt Library.
  • -plugin-sql- Enable SQL  as a plugin to be linked to at run time.

You can see which drivers in your installation are assembled as plugins along this path:
%QTDIR%\plugins\sqldrivers

Decision


And now we turn to the essence of the problem and its simple solution.
As far as I understand, by default, SQL drivers are built statically, i.e. for linking with QtSql. Therefore, as a result of the assembly according to the documentation, we get only * .a files that you can’t attach to the existing QtSql4.dll in any way. The output that is often found on the network is to rebuild Qt entirely by including the necessary drivers during configuration. In principle, the option is normal, but only very long.

Naturally, we can manually fix the makefiles and assemble the necessary driver as a plugin, but this is an ungrateful task - let the computer do it for us.

It is further assumed that PostgreSQL with the necessary libraries and header files is installed in C: \ psql (I usually put it in Program Files, and C: \ psql is junction).

Well, then everything is quick and easy:
  1. Run the Qt command line (Start-> Programs-> Qt by Digia ...-> Qt xxx Command Promt)
  2. We configure the sources and enable the necessary driver as a plugin
    configure -I "c:\psql\include" -L "c:\psql\lib" -plugin-sql-psql
  3. cd src\plugins\sqldrivers\psql
  4. qmake psql.pro
  5. make debug
  6. make release

Checking% QTDIR% \ plugins \ sqldrivers. Bingo!

Also popular now: