Qt

Valgrind 3.16.0 for MacOS Mojave 10.14.6

After many unstable versions and unsuccessful tries found a working but still experimental solution for MacOS Mojave 1.14.6 using homebrew. This solution also working with Qt5.14.1 using QtCreator 4.11.2 on Mojave 10.14.6.

My conclusion after carry out some analysis on some of my cross platform projects, simple and more complicated once. This solution works on simple projects, however as the complexity increases the results are totally unpredictable.

I’d like to emphasise that after comparing my results, I still NOT recommend to rely on results running Valgrind on MacOS, found fairly high number of critical errors and false positives. My results based on comparing the two following development environments:

– MacOS Mojave 10.14.6, Valgrind 3.16.0, Qt5, gcc, clang
– Ubuntu 18.04, Valgrind 3.16.0, Qt5, gcc, clang

brew install --HEAD https://raw.githubusercontent.com/sowson/valgrind/master/valgrind.rb

QSqlDatabase connection testing tool

QSqlDatabase connection access

A simple command line tool to test connection between Qt client applications and a remote Mysql Server. The code includes Secure Sockets Layer (SSL) support to test secure connection. The QSqlDatabase class provides an interface for accessing a database through a connection. An instance of QSqlDatabase represents the connection. The connection provides access to the database via one of the supported database drivers (MySQL in our case), which are derived from QSqlDriver. Alternatively, we can subclass your own database driver from QSqlDriver. I’ll post an another article about how to write your own database driver in Qt for Unix, OSX and Windows platforms. It’s recommend to write your own database driver especially when you planning to deploy your application in different platforms. It’s simply because there could be surprises especially on client deploying your application that using one of the supported database drivers due to missing dependencies or environmental variables.

Software prerequisites:
Remote MySQL server version: 5.7 or above
Qt version: Qt_5_7_1 or above
OpenSSL 1.0.x or above the for SSL feature

How to use it:
1. I simply run the mysql_conn_test_database.sql SQL script file to create a test database on the remote server.
2. Set the hostname or IP, port number, username and password. You migth need to create a new database user for this purpose.
[You will need to create SSL certificates for the database server and client in order to test SSL Feature on the MySQL Server.]
3. If everthing is set just simple build and run the project in your QtCreator.

If everything goes well you will recieve the following feedback from your terminal:


QSqlDatabase connection test tool was created and written by Roland Ihasz and it is licensed under the GNU General Public License (GPL) Version 2 or above.

Check out on [wp-svg-icons icon=”github-2″ wrap=”I”] GitHub. [wp-svg-icons icon=”new-tab” wrap=”I”] https://github.com/lnrsoft/QSqlDatabase-connection-test-tool

[wp-svg-icons icon=”new-tab” wrap=”i”] mysql_db_connection_test.pro

# (c)  Roland Ihasz - https://github.com/lnrsoft   

QT += core
QT -= gui
QT += sql
QT += sql widgets

CONFIG += c++11
CONFIG += console       
#CONFIG -= console      
CONFIG -= app_bundle

TEMPLATE = app

TARGET = mysql_db_connection_test

SOURCES += main.cpp

QTPLUGIN += sqldrivers

QMAKE_CXXFLAGS += -std=c++11

[wp-svg-icons icon=”new-tab” wrap=”i”] main.cpp

// (c)  Roland Ihasz - https://github.com/lnrsoft

#include <QSqlDatabase>
#include <QApplication>
#include <QSqlRecord>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
#include <QtCore>
#include <QtSql>

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("192.168.1.100");        // your host or ip name
        db.setPort(3306);                       // port number
        db.setDatabaseName("mysql_conn_test");  // db name
        db.setUserName("username");             // db username
        db.setPassword("password");             // db password
        //----------Additional-SSL-Feature-for-secure-database-connection----------
        //  db.setConnectOptions(
        //    "SSL_KEY=/Users/lnrsoft/repository/mysql_db_conn_test/client-key.pem;"
        //    "SSL_CERT=/Users/lnrsoft/repository/mysql_db_conn_test/client-cert.pem;"
        //    "SSL_CA=/Users/lnrsoft/repository/mysql_db_conn_test/ca-cert.pem;"
        //    "SSL_CIPHER=DHE-RSA-AES256-SHA;");
        //----------Additional-SSL-Feature-for-secure-database-connection----------
        if(!db.open()) {
            qDebug() << "\nDATABASE CONNECTION UNSUCCESSFUL."
                     << "\nlastError: " << db.lastError().text() <<"\n";
            return 0;
        }
        if(db.open()) {
            qDebug() << "\nSUCCESSFULLY CONNECTED TO THE DATABASE SERVER.";
            QSqlQuery query(
                "SELECT "
                "dataTable.ID, "
                "dataTable.x1, "
                "dataTable.y1, "
                "dataTable.z1, "
                "dataTable.t "
                "FROM mysql_conn_test.dataTable");
            QSqlRecord rec1 = query.record();
            QSqlRecord rec2 = query.record();
            QSqlRecord rec3 = query.record();
            QSqlRecord rec4 = query.record();
            QSqlRecord rec5 = query.record();
            qDebug() << "NUMBER OF ROWS: " << query.size();   // Return number of rows
            int productCode1 = rec1.indexOf("ID");
            int productCode2 = rec2.indexOf("x1");
            int productCode3 = rec3.indexOf("y1");
            int productCode4 = rec4.indexOf("z1");
            int productCode5 = rec5.indexOf("t");
            qDebug() << "====================================================================================";
            while(query.next()) {
                qDebug() << "ROW NUMBER"
                         << query.value(productCode1).toString() << ""
                         << query.value(productCode2).toString() << ""
                         << query.value(productCode3).toString() << ""
                         << query.value(productCode4).toString() << ""
                         << query.value(productCode5).toString();
            }
            qDebug() << "====================================================================================";
            qDebug() << "COMPLETE...";
            return 0;
        }
    }
}

[wp-svg-icons icon=”new-tab” wrap=”i”] mysql_conn_test_database.sql

/*	(c)  Roland Ihasz - https://github.com/lnrsoft	*/

DROP DATABASE IF EXISTS `mysql_conn_test`;

CREATE DATABASE IF NOT EXISTS `mysql_conn_test` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `mysql_conn_test`;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

DROP TABLE IF EXISTS `dataTable`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dataTable` (
  `ID` int(1) NOT NULL,
  `x1` double NOT NULL,
  `y1` double NOT NULL,
  `z1` double NOT NULL,
  `t` int(16) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

LOCK TABLES `dataTable` WRITE;
/*!40000 ALTER TABLE `dataTable` DISABLE KEYS */;
INSERT INTO `dataTable` VALUES (1,0.3352925219759345,0.336742962207645,0.3117983819916844,2),(2,0.3351856799423695,0.334428142439574,0.3127960397861898,5),(3,0.3592007360234857,0.314406356215476,0.3792905477248132,9);
/*!40000 ALTER TABLE `dataTable` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

Alternatively, use the following shorter source code to check the connection without any sql query.
[wp-svg-icons icon=”new-tab” wrap=”i”] QSqlDatabase connection test tool without query

// (c)  Roland Ihasz - https://github.com/lnrsoft   

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
#include <QDebug>
#include <QtCore>
#include <QtSql>

int main(int argc, char* argv[])
{
    QCoreApplication a(argc, argv);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("192.168.1.100");        // host or ip name
    db.setPort(3306);                       // port number
    db.setDatabaseName("mysql_conn_test");  // db name
    db.setUserName("username");             // db username
    db.setPassword("password");             // db password
    if(!db.open()) {
        qDebug() << "\nDATABASE CONNECTION UNSUCCESSFUL."
                 << "\nlastError: " << db.lastError().text() <<"\n";
        return 0;
    }
    if(db.open()) {
        qDebug() << "\nSUCCESSFULLY CONNECTED TO THE DATABASE SERVER.\n";
        return 0;
    }
}

HOW TO RETURN A STRING EQUIVALENT OF THE NUMBER N USING QT.

QString QString::number(double n, char format = ‘g’, int precision = 6) returns a string equivalent of the number n, formatted according to the specified format and precision.

Let’s see the following example.
There are 3 double values x, y, z.
We want to return the result of the following equation:
(x / z) * (5.961 / y)
We also want to ask the user to set the desired precision of the result.

Here is how I do with Qt.
[wp-svg-icons icon=”github” wrap=”I”] You can also Fork the complete qt project from my GitHub.
* note = there are many ways to solve this issue, much simpler, but this time I want to demonstrate how we do in Qt.

// (c)  Roland Ihasz - https://github.com/lnrsoft

#include <QCoreApplication>
#include <iostream>
#include <QDebug>

int main(int argc, char* argv[])
{
    QCoreApplication a(argc, argv);
    double x = 1.190;
    double z = 6.634;
    double y = 3.398;
    int precision;
    double result = (x / z) * (5.961 / y);
    std::cout << "\nEnter the required precision of the result: ";
    std::cin >> precision;
    QString resultstring = QString::number(result, 'g', precision);
    std::cout << "\n1.In this case the output is a double value."
              << std::endl;
    std::cout << "The result by the compiler default setting is: "
              << result << "\n" << std::endl;
    std::cout << "2. Using the QString::number member function."
              << std::endl;
    std::cout << "The result when we set the precision to "
              << precision << " is: " << qPrintable(resultstring)
              << "\n" << std::endl;
    /*
     *  using qPrintable to remove qoutes from the output
    */
    return 0;
}

Argument Formats
In member functions where an argument format can be specified (e.g., arg(), number()), the argument format can be one of the following:

Format            Meaning

e                      format as [-]9.9e[+|-]999

E                      format as [-]9.9E[+|-]999

f                       format as [-]9.9

g                      use e or f format, whichever is the most concise

G                     use E or f format, whichever is the most concise

A precision is also specified with the argument format. For the ‘e’, ‘E’, and ‘f’ formats, the precision represents the number of digits after the decimal point. For the ‘g’ and ‘G’ formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).

How to return a string equivalent of the number n using Qt was written by Roland Ihasz>, you can modify or distribute. You can do anything you want with this software, just don’t say you wrote it.

More info on [wp-svg-icons icon=”new-tab” wrap=”i”]  doc.qt.io