View Full Version : How to connect MySql from Qt in CentOS

26th June 2013, 14:41
Hello Everyone,

Well i am working on CentOS and trying to create a simple program which requires to connect with MySql.

Qt : 5
OS : CentOS

#include <QApplication>
#include <QTableWidget>
#include <QMessageBox>
#include <QtSql>

int main(int argc,char* argv[])
QApplication app(argc,argv);
QTableWidget* table = new QTableWidget();
table->setWindowTitle("Connect to Mysql Database Example");

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
if (!db.open())
QMessageBox::critical(0, QObject::tr("Database Error"),

QSqlQuery query("SELECT * FROM test");


int index=0;
while (query.next())
table->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
table->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));

return app.exec();

Well the problem is MySql DRIVERS are not loaded.
Which drivers should i use in CentOS to connect my program with MySql?

26th June 2013, 14:54
If you have installed Qt with yum from a CentOS repos, check if you didn't forget to install mysql driver package too, database drivers are in individual packages. If you use your own compiled qt installation check if mysql driver has been selected during the configure.

27th June 2013, 07:04
Hei Nix,

I have searched on google and found many drivers for mysql.
Which one is suitable for Qt 5 and CentOS. ?


27th June 2013, 08:23
If you are using Qt version installed by your distrib package manager, just do :

yum search qt | grep -i mysql
It should return a few lines, in my REL the line you are looking for is :

qt4-mysql.x86_64 : MySQL drivers for Qt's SQL classes

But the package name change from one distrib to an onother, you have to find yours in the list.
Install the package :

yum install qt4-mysql
Then everything should works.

If you are using your own built Qt lib, check during the configure if the plugin for mysql is selected, if not check the package mysql is installed with yum (it's the client for mysql), and redo configure.