Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9264801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:54:50+00:00 2026-06-18T13:54:50+00:00

I am very new to Qt. And am trying to make MySQL Database connection

  • 0

I am very new to Qt. And am trying to make MySQL Database connection using Qt and show the table information QSqlTableModel. Here are my codes:

1) .pro file

QT       += core gui sql

TARGET = sri_qtdb
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

2) .h file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QtSql>
#include <QtGui>
#include <QtCore>


namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::MainWindow *ui;
    QSqlDatabase db;
    QSqlTableModel *model;
    //QTableWidget *table;
};

#endif // MAINWINDOW_H

3) .cpp files

a) main.cpp

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

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

b) mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setCentralWidget(ui->tableView);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("user");
    db.setUserName("root");
    db.setPassword("*********");
    if(!db.open()){ qDebug()<< "Failed"; }
    else
    {
       model = new QSqlTableModel(this);
       model->setTable("new_joiners");
       model->select();
       ui->tableView->setModel(model);
    }
}

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

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

Available drivers: QSQLITE QMYSQL3 QMYSQL

My output is always “Failed”. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:54:51+00:00Added an answer on June 18, 2026 at 1:54 pm

    First of all, recheck your DB at localhost and all parameters.


    Check if you really have needed driver:

    QStringList driverList;
    driverList = QSqlDatabase::drivers();
    
    if (!driverList.contains("QMYSQL", Qt::CaseInsensitive))
        qDebug()<<"No QMYSQL support! Check all needed dll-files!";
    else 
        qDebug()<<"QMYSQL support is avaiable.";
    

    Write your code to make it concrete and use debug capabilities. For example, if you got error on connection to DB, output last error in this way:

    if(!db.open())
    {
        qDebug()<<"Error while opening DB: " << db->databaseName()
                << db->lastError();
    }
    

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to make an application which has very rich Database mySQL with
I am very new to Swing, and I'm trying to make a GUI. Using
Very new mobile developer here... I am trying to retrieve a list of tweets
I am very new to NTLM/LDAP and trying to authenticate using NTML running on
Im new to PHP and trying to make a very simple api (good way
Hi everyone, I'm trying to make my application support Logging but I'm very new
I'm very new to F# and I'm trying to make a struct for storing
I'm very new to the websocket, I'm trying to make a simple websocket server
Hey I am very new to python but I am trying to make a
I am very new to Action script. I am trying to make a game

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.