To install QT:
- Official website Download Qt5.2.1:qt-opensource-linux-x64-5.2.1.run
- Direct command line run:./qt-opensource-linux-x64-5.2.1.run
- Select the installation path: (I chose/HOME/MYNAME/QT) the default is/home/yourpcname/qt5.2.1
- Step-by-step installation, select "Source Components" when selecting a build, otherwise the plugin of the subsequent recompile SQL may need to reload QT
Install MySQL and write a simple test program:
#include <QApplication> #include <QtSql> #include <qdebug>int main (int argc, char *argv[]) { Qapplication A (argc, argv); Qsqldatabase db = Qsqldatabase::adddatabase ("Qmysql", "Conna"); Db.sethostname ("localhost"); Db.setdatabasename ("MySQL"); Db.setusername ("root"); Db.setpassword ("Zymysql"); if (Db.open ()) { qdebug () << "connected!" << Endl; Exit (0); } else { qdebug () << "failed!" << Endl; Exit (0); } return a.exec ();}
Compile run, prompt error:
Qsqldatabase:qmysql driver not loadedqsqldatabase:available drivers:qsqlite qmysql QMYSQL3 qpsql QPSQL7
1. View Sqldriver dependencies:
$CD ~/qt/5.2.1/gcc_64/plugins/sqldrivers$ldd libqsqlmysql.so #得到以下: linux-vdso.so.1 = (0x00007ffe607ac000) libmysqlclient_r.so.16 = Not foundlibqt5sql.so.5 =/home/sharon/qt/5.2.1/gcc_64/plugins/sqldrivers/./. /.. /lib/libqt5sql.so.5 (0x00007f4862374000) libqt5core.so.5 =/home/sharon/qt/5.2.1/gcc_64/plugins/sqldrivers/./. /.. /lib/libqt5core.so.5 (0x00007f4861ca6000) libstdc++.so.6 =/usr/lib/x86_64-linux-gnu/libstdc++.so.6 ( 0x00007f48619a2000) libc.so.6 =/lib/x86_64-linux-gnu/libc.so.6 (0x00007f48615dd000) libpthread.so.0 =/lib/ x86_64-linux-gnu/libpthread.so.0 (0x00007f48613bf000) libicui18n.so.51 =/home/sharon/qt/5.2.1/gcc_64/plugins/ Sqldrivers/./. /.. /lib/libicui18n.so.51 (0x00007f4860fa6000) libicuuc.so.51 =/home/sharon/qt/5.2.1/gcc_64/plugins/sqldrivers/./ .. /.. /lib/libicuuc.so.51 (0x00007f4860c20000) libdl.so.2 =/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4860a1c000) libgthread-2.0.so.0 =/usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f486081a000) librt.so.1 =/lib/x86_64-linux-gnu/librt.so.1 (0x00007f4860612000) libglib-2.0.so.0 =/lib/x86_64- linux-gnu/libglib-2.0.so.0 (0x00007f486030a000) libm.so.6 =/lib/x86_64-linux-gnu/libm.so.6 (0x00007f4860004000 ) Libgcc_s.so.1 =/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f485fdee000)/lib64/ld-linux-x86-64.so.2 ( 0x00007f48627c6000) libicudata.so.51 =/home/sharon/qt/5.2.1/gcc_64/plugins/sqldrivers/./. /.. /lib/libicudata.so.51 (0x00007f485e6a5000) libpcre.so.3 =/lib/x86_64-linux-gnu/libpcre.so.3 ( 0x00007f485e467000)
2. View the version of MySQL installed:
$ dpkg--get-selections | grep mysql# Gets the following: Ctions | grep mysqllibdbd-mysql-perlinstalllibmysqlclient-devinstalllibmysqlclient18: Amd64installmysql-clientinstallmysql-client-5.5installmysql-client-core-5.5installmysql-commoninstallmysql-serverinstallm Ysql-server-5.5installmysql-server-core-5.5install
The error point is: libmysqlclient_r.so.16 = not found, requires a version of 16, the installed version is 18.
Solution: Recompile the sqldriver for it.
#进入到qt的源码目录下的此目录: $CD ~/qt/5.2.1/src/qtbase/src/plugins/sqldrivers/mysql# compile with Qmake: $ ~/qt/5.2.1/gcc_64/bin/qmake " Includepath+=/usr/local/include "" Libs+=-l/usr/local/lib-lmysqlclient_r "Mysql.pro$make$make install# Then go to the GCC directory to view libqsqlmysql.so dependencies: $CD ~/qt/5.2.1/gcc_64/plugins/sqldrivers$ldd libqsqlmysql.so# Dependencies successfully established: Linux-vdso.so.1 = (0x00007fff193b2000) libmysqlclient.so.18 =/usr/lib/x86_64-linux-gnu/ libmysqlclient.so.18 (0x00007fc367f2a000) libqt5sql.so.5 =/home/sharon/qt/5.2.1/gcc_64/lib/libqt5sql.so.5 ( 0x00007fc367cea000) libqt5core.so.5 =/home/sharon/qt/5.2.1/gcc_64/lib/libqt5core.so.5 (0x00007fc36761c000) libstdc++.so.6 =/usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc367318000) libc.so.6 =/lib/x86_64- Linux-gnu/libc.so.6 (0x00007fc366f53000) libz.so.1 =/lib/x86_64-linux-gnu/libz.so.1 (0x00007fc366d3a000) libdl.so.2 =/lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc366b36000) libpthread.so.0 =/lib/x86_64-linux-gnu/ libpthread.so.0 (0x00007fc3669180XX) libm.so.6 =/lib/x86_64-linux-gnu/libm.so.6 (0x00007fc366612000) libicui18n.so.51 =/home/sharon/qt/5.2.1 /gcc_64/lib/libicui18n.so.51 (0x00007fc3661f9000) libicuuc.so.51 =/home/sharon/qt/5.2.1/gcc_64/lib/ libicuuc.so.51 (0x00007fc365e73000) libgthread-2.0.so.0 =/usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 ( 0x00007fc365c71000) librt.so.1 =/lib/x86_64-linux-gnu/librt.so.1 (0x00007fc365a69000) libglib-2.0.so.0 =/ lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fc365761000) libgcc_s.so.1 =/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc36554b000)/lib64/ld-linux-x86-64.so.2 (0x00007fc368674000) libicudata.so.51 =/HOME/SHARON/QT/5.2.1/ gcc_64/lib/libicudata.so.51 (0x00007fc363e02000) libpcre.so.3 =/lib/x86_64-linux-gnu/libpcre.so.3 ( 0x00007fc363bc4000)
Recompile run, Success!
[Linux note]ubuntu14.04+qt5.2 configuration MySQL