Last active
January 14, 2018 13:13
-
-
Save danmaina/fbc75ed004b5df362b4e2e830975696c to your computer and use it in GitHub Desktop.
Quick Qtwidget application connect to Mysql database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "dialog.h" | |
#include "ui_dialog.h" | |
#include <iostream> | |
#include <QSqlDriver> | |
#include <QSqlDatabase> | |
#include <QtSql> | |
Dialog::Dialog(QWidget *parent) : | |
QDialog(parent), | |
ui(new Ui::Dialog) | |
{ | |
ui->setupUi(this); | |
} | |
Dialog::~Dialog() | |
{ | |
delete ui; | |
} | |
void Dialog::on_pushButton_clicked() | |
{ | |
if(connectDatabase() == true){ | |
qDebug() << "Success"; | |
} | |
} | |
bool Dialog::connectDatabase(){ | |
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); | |
QSqlQuery query; | |
db.setHostName("localhost"); | |
db.setUserName("root"); | |
db.setPassword("root"); | |
db.setDatabaseName("inventory"); | |
if(db.open()){ | |
// QMessageBox :: warning(this,"login","Successful Database Connection"); | |
qDebug()<<"True"; | |
return true; | |
}else{ | |
//QMessageBox :: warning(this,"login","Failed Database Connection"); | |
qDebug()<<"Haiconnect Buda"; | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DIALOG_H | |
#define DIALOG_H | |
#include <QDialog> | |
namespace Ui { | |
class Dialog; | |
} | |
class Dialog : public QDialog | |
{ | |
Q_OBJECT | |
public: | |
explicit Dialog(QWidget *parent = 0); | |
~Dialog(); | |
private slots: | |
void on_pushButton_clicked(); | |
private: | |
Ui::Dialog *ui; | |
bool connectDatabase(); | |
}; | |
#endif // DIALOG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2017-04-06T16:12:35 | |
# | |
#------------------------------------------------- | |
QT += core gui sql | |
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | |
TARGET = Inventory_Remodeled | |
TEMPLATE = app | |
# The following define makes your compiler emit warnings if you use | |
# any feature of Qt which as been marked as deprecated (the exact warnings | |
# depend on your compiler). Please consult the documentation of the | |
# deprecated API in order to know how to port your code away from it. | |
DEFINES += QT_DEPRECATED_WARNINGS | |
# You can also make your code fail to compile if you use deprecated APIs. | |
# In order to do so, uncomment the following line. | |
# You can also select to disable deprecated APIs only up to a certain version of Qt. | |
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | |
SOURCES += main.cpp\ | |
mainwindow.cpp \ | |
dialog.cpp | |
HEADERS += mainwindow.h \ | |
dialog.h | |
FORMS += mainwindow.ui \ | |
dialog.ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "dialog.h" | |
#include <QApplication> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
// MainWindow w; | |
Dialog d; | |
d.show(); | |
// w.show(); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment