samedi 16 juin 2018

QMysql driver not loaded but available in qt - Ubuntu

I'm trying to connect mysql database to my qt application and my application is not giving any build errors or something but when I try to click on a button when the app is running which is gonna show a message of database connected if the database gets connected , shows an error message of "DRIVER NOT LOADED" and I have tried every possible method to find solution of this on internet but my problem is still not solved and yeah I've tried the solution on this link "QMYSQL driver not loaded but available-Ubuntu" too and many others plus one more point that I'm also not able to find a qt folder in opt/ directory also. Let me show you my source codes -->

this is my .pro file

#-------------------------------------------------
#
# Project created by QtCreator 2018-06-14T17:59:51
#
#-------------------------------------------------

QT       += core gui sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Login
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp


HEADERS  += mainwindow.h

FORMS    += mainwindow.ui \
            login.ui
QMAKE_CXXFLAGS += -std=gnu++11

this is my mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QMessageBox>
#include <QtSql>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_2_clicked()
{
    if(QSqlDatabase::drivers().contains("QMYSQL")){
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost:3306");
        db.setDatabaseName("qt_database");
        db.setUserName("root");
        db.setPassword("constems1");
        if (!db.open()) {
            QMessageBox::critical(0, QObject::tr("Database Error"),
                                  db.lastError().text());

        }else{
            QMessageBox::about(0,"My Title","Database opened");
        }
    }
}

this is my main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

this is error window

Starting /home/constems/build-Login-Desktop_Qt_5_7_0_GCC_64bit-Debug/Login...
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
/home/constems/build-Login-Desktop_Qt_5_7_0_GCC_64bit-Debug/Login exited with code 0

Now to tell you more on that I've tried this link too "http://adamcavendish.is-programmer.com/posts/40431.html" but nothing happened in my favor and i also tried installing the libraries and they installed well but the error still persist. If you guys can help me then I'll be grateful to you.

Aucun commentaire:

Enregistrer un commentaire