I have got this far (with check connection first only):
mysql.h:
#ifndef MYSQL_H
#define MYSQL_H
#include <QObject>
{
Q_OBJECT
public:
explicit MySQL
(QObject *parent
= 0);
Q_INVOKABLE void checkConnection();
signals:
public slots:
};
#endif // MYSQL_H
#ifndef MYSQL_H
#define MYSQL_H
#include <QObject>
class MySQL : public QObject
{
Q_OBJECT
public:
explicit MySQL(QObject *parent = 0);
Q_INVOKABLE void checkConnection();
signals:
public slots:
};
#endif // MYSQL_H
To copy to clipboard, switch view to plain text mode
mysql.cpp:
#include "mysql.h"
#include <QtSql>
#include <QObject>
{
}
void MySQL::checkConnection()
{
// Sets database
db.setHostName("localhost");
db.setDatabaseName("terminals");
db.setUserName("root");
db.setPassword("");
if(db.open()) { qDebug() << "MySQL: Connected"; db.close(); result = "true"; }
else { qDebug() << "MySQL ERROR: " << db.lastError().text(); result = "false"; }
}
#include "mysql.h"
#include <QtSql>
#include <QObject>
MySQL::MySQL(QObject *parent) :
QObject(parent)
{
}
void MySQL::checkConnection()
{
QString result;
// Sets database
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("terminals");
db.setUserName("root");
db.setPassword("");
if(db.open()) { qDebug() << "MySQL: Connected"; db.close(); result = "true"; }
else { qDebug() << "MySQL ERROR: " << db.lastError().text(); result = "false"; }
}
To copy to clipboard, switch view to plain text mode
main.qml:
import QtQuick 2.12
import QtQuick.Controls 2.0
import QtQuick.LocalStorage 2.0
import MySQL 1.0
import 'JavaScript.js' as JS
Item {
MySQL {
id: sql
}
MouseArea {
anchors.fill: parent
onClicked: { sql.checkConnection(); }
}
}
import QtQuick 2.12
import QtQuick.Controls 2.0
import QtQuick.LocalStorage 2.0
import MySQL 1.0
import 'JavaScript.js' as JS
Item {
MySQL {
id: sql
}
MouseArea {
anchors.fill: parent
onClicked: { sql.checkConnection(); }
}
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtSql>
#include <QtDebug>
#include "mysql.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<MySQL>("MySQL", 1, 0, "MySQL");
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
// Sets path for SQLite
engine.setOfflineStoragePath("C:\\Terminal");
return app.exec();
}
#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtSql>
#include <QtDebug>
#include "mysql.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<MySQL>("MySQL", 1, 0, "MySQL");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// Sets path for SQLite
engine.setOfflineStoragePath("C:\\Terminal");
return app.exec();
}
To copy to clipboard, switch view to plain text mode
¨
this works like a charm!
i would like to ad the return ability, so from QML i can do something like:
if(sql.checkConnection()===true) { something } else { somethingelse }
- so it will return result variable from mysql.cpp
how to do this?
Bookmarks