Qt common wheel collection (with Demo)

Public wheels / API

It mainly stores global variables, configuration variables, some public method codes, user-defined logs, and redirects the output of Qt compiler information to local files.

Log Library

It is built to use mvlog library, cross platform general library and simple singleton mode. At the same time, it can redirect and output Qt compiler information (file name, function name, number of lines of code and other information). Logs include Debug, Warning, critical, Fatal, Info, etc.

/* How to output compiler logs directionally? Put the following code into the mian function
  * @brief Directional output compiler debugging information
  * Please add the following to the main function to judge whether the condition outputs debugging information directionally
  *    qInfo: essential information
  *    qDebug: debug information
  *    qWarning: Warning message
  *    qCritical: serious error
  *    qFatal: Fatal error
  *     if (App::isOutPutDebuginfo) {
  *     void MessageOutPut(QtMsgType type, const QMessageLogContext &context, const QString &msg);
  *      qInstallMessageHandler(MessageOutPut);
  *   }
  * @param
  *
  * @return
  * The debugging information will be output in the program startup path, and the restart program will be deleted automatically
  *
*/
//File size 1mb
const int FILE_SIZE = 1024000;

void MessageOutPut(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    mutex.lock();
    QString text;
    QString filename;
    switch(type)
    {
    case QtDebugMsg:
        text = QString("Debug:");
        filename = "_debug.log";
        break;

    case QtWarningMsg:
        text = QString("Warning:");
        filename = "_warning.log";
        break;

    case QtCriticalMsg:
        text = QString("Critical:");
        filename = "_critical.log";
        break;

    case QtFatalMsg:
        text = QString("Fatal:");
        filename = "_fatal.log";
        break;

    case QtInfoMsg:
        text = QString("Info:");
        filename = "_info.log";
        break;

    default:
        break;
    }
    //Log write to file
    QString current_dt = QDateTime::currentDateTime().toString("[yyyy-MM-dd-HH-mm-ss-zzz] ");
    //QString context_info = QString("File:(%1) Line:(%2) Function:(%3)").arg(QString(context.file)).arg(context.line).arg(context.function);
    QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString message = QString("%1%2  %3   %4").arg(current_dt).arg(text).arg(context_info).arg(msg);
    //QString LOG_FILE  =  "C:/"+filename;
    QString LOG_FILE  =  App::appPath+"/"+App::appName+filename;

    QFile debfile(LOG_FILE);
    debfile.open(QIODevice::WriteOnly | QIODevice::Append);
    //If the log file size is greater than 1m, it will be emptied
    if (debfile.size() > FILE_SIZE) {
        debfile.close();
        debfile.open(QIODevice::WriteOnly | QIODevice::Truncate);
    }
    QTextStream text_stream(&debfile);
    text_stream << message << "\r\n";
    text_stream.flush();   //refresh buffer 
    debfile.flush();
    //It is not recommended to open and close log files frequently. There is a big IO performance bottleneck
    debfile.close();
    mutex.unlock();
}

log4qt is an open source logging framework system. It's troublesome to use.

GPIO

Simply control the high / low level of GPIO port. The interface is a static function, which is very simple and convenient to call.

Serial port Library

You can use a third-party serial port library

Test case: see Example/comtool

Add the encryption module of the company's main control board

Network Library

TCP/UDP

/**
*@projectName MyNetwork
*@brief  version V1.0.0.1
* 1: There are four modes: tcp client, tcp server, udp client and udp server
* 2: Support concurrent connection of multiple clients
* 3: Support unicast, multicast, specified disconnection and all disconnection at the server
* 4: Single thread
* 2020-06-03 version V1.0.0.2 newly added:
* 5: TCP Transfer json object byte array custom structure
*
* 2020-11-20 version V1.0.0.3 newly added:
* 6: UDP Transfer byte array
* 7: TCP Use flush to refresh in time when sending data
*
*author zlozl
*date 2020-06-03
*/

/* [Commit]
 * 1.Add UDP broadcast
 *
 * Version:   V1.0.0.2
 * Date:      2021-05-08
 * Author:    zlozl
 */

Test case: see Example/MyNetwork(V1.0.0.4)

WebSocket

Simple Server and Client encapsulated by Qt QWebsocket.

Test case: see Example/websocket_demo

Http/Https

Qt's official HttpServer library doesn't seem to be very easy to use, so I use a third-party library, JQHttpServer. At present, this is the http library I personally use.

github: https://github.com/188080501/JQHttpServer

demo:

myHttpClient_v1.2: Using the client, the server first requests to download files and displays the progress of downloading files.
myHttpClient: JQHttpServer Client usage examples for
myHttpServer: JQHttpServer Service end usage example of

Multithreading

Qt several ways to realize multithreading, how to gracefully realize the start, pause, reset and exit of threads. Below this file are examples of multithreading in various poses.

link: [Qt] Qt multithreaded "those things" - Li Chungang - blog Garden (cnblogs.com)

Audio and video library

audio

Using QSoundEffect independent sound playback thread movetothread mode

1. Custom voice type playback,Sound playback has the highest priority,A single non loop will overwrite the playback
2. Custom voice type playback,Sound playback priority is the lowest,Single no loop will not overwrite playback
3. Play the voice name under the third-party folder,Sound playback has the highest priority,A single non loop will overwrite the playback
4. Pause playback

Mode of use

#include "audiothread.h"
...
   //Custom sound playback thread
    AudioThread audio_1;
    AudioThread audio_2;
..
    
    audio_1.play(VOICE_TYPE::VOICE_RECOPSAA);
	audio_2.play(VOICE_TYPE::VOICE_TOOFAR);

vedio

OpenCV version:

Pure OpenCV frame display + face algorithm / some applications of living body detection

OpenGL version:

Use the mvcamera Library of yuechao to take the YUV image after the frame of usb monocular / binocular lens and render and display the YUV image data through qoopenglwidget. The example demo is as follows

QWidget

Some common methods of QWidget

1.//Pop up class in the lower right corner
2.//Tray icon class
3.//Pop up message box class
4.//Pop up input box class
5.//The date selection dialog box pops up
6.//Graphic font processing class
7.//Global static UI method class
8.//widget style sheet

QML

Two open source QML UI component libraries are recommended

  1. Materialeui, Google open source UI Library

    link: GitHub - QML demo / QML material: QML material UI: an encapsulated frame material

  2. Toou-2D

    link: Toou 2D is ready to use and born for simplicity| Toou-2D

Keywords: Qt socket qml

Added by bbxrider on Tue, 01 Feb 2022 16:01:21 +0200